3 Replies - 574 Views - Last Post: 18 April 2019 - 03:54 AM Rate Topic: -----

#1 AlbRodriguez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-April 19

Help with Windows Batch Scripting

Posted 17 April 2019 - 12:02 PM

Dear friends,

I am using a code to delete headers (line 1 to 82) from all .txt files inside one folder. The thing is this code works for relatively small files, but when I try to use on big files, the code does nothing. I think it is because it is using temporary files.

Can someone help me modify this code to not use temporary files and just delete on each one of .txt file from line 1 to 82? Thank you!

@echo off
for %%f in (*.txt) do (
    more +82 "%%f" > "%TEMP%\%%f"
    move /y "%TEMP%\%%f" "%%f" > nul
)
echo Done.



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Windows Batch Scripting

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7471
  • View blog
  • Posts: 15,493
  • Joined: 16-October 07

Re: Help with Windows Batch Scripting

Posted 17 April 2019 - 01:07 PM

Using "more" the way you are, you're kind of stuck using a file.

Considering what you're doing, using the same temp file might be easier and eliminate oddities with filenames.
Maybe:
for %%f in (*.txt) do (
    more +82 "%%f" > scratch.txt
    type scratch.txt > "%%f"
)
del scratch.txt



Alternately, you could do a couple of passes:
mkdir dump
for %%f in (*.txt) do (
    more +82 "%%f" > "dump\%%f"
)

move /y dump\* .



I'm honestly just guessing, though. DOS batching is a few decades out of date. You might consider using PowerShell. Python would also make short work of this. Or, if back in dos days, perl.
Was This Post Helpful? 0
  • +
  • -

#3 AlbRodriguez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-April 19

Re: Help with Windows Batch Scripting

Posted 17 April 2019 - 01:43 PM

Dear baavgai,

Thank you so much for your reply. I have tried the suggestions you have made and honestly I am facing similar problems with them.

You have referred as other coding suggestion to use python. Honestly my skills at python are very very poor.

Would a python code to do what I want be too much difficult to produce?

Thank you so much
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7471
  • View blog
  • Posts: 15,493
  • Joined: 16-October 07

Re: Help with Windows Batch Scripting

Posted 18 April 2019 - 03:54 AM

Any general purpose language will do what you're after. Frankly, I'm not sure why your dos solution is failing on you. Though you're maybe making "more" do something it mayn't be up to. Indeed, more is about interactive file browsing and it's possible your version balks at your usage.

Millennia ago, Perl was the tool of choice for the kind of manipulation you're after. However, the syntax was always wonky and the OOP iteration (5) was weirder still, to the point of alienating many perl devotees. Python comes along and can do all that Perl does with less pain. Python does, curiously, suffer a similar schism, with versions 2 and 3 existing side by side, but it's less dire than perl.

Right, I don't really want to write this for you. But, let's point you in the right direction.

First, you want to list some files. Here's some information on that: https://www.mkyong.c...in-a-directory/

The code isn't to my taste, so here's the same code a little cleaner:
import os

for filename in os.listdir('.'):
    print(filename)



You can google how to do a wild card filter. I'll offer simple way to d that:
for filename in os.listdir('.'):
    if filename.lower().endswith(".txt"):
        print(filename)



Now you want to read the lines from the file, skip some, and write them back.

At this point, I'll point you at the docs: https://docs.python....library/io.html

The nice thing about Python is that it has a huge programmer mind share. With this in mind, google "python read text file", "python write text file", etc.

See what you can come up with. If you choose to go with Python, post your work to that forum. Python enthusiasts are always happy to aid a convert. ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1