Batch editing of file and filenames

Caporegime
Joined
26 Aug 2003
Posts
37,508
Location
Leafy Cheshire
Hi guys,

I have a folder that contains a bunch of subfolders, each containing a single file (formatted in plain text). I need to edit the contents of all of these files, and rename them (the original file is un-needed).

Basically, each file is a list of domains, but our firewall can only accept them in FQDN form, so apple.com would need to be *.apple.com. There are literally thousands of records in each file (probably around 20 files), and they all need to be edited so that each line now starts with *.

Does anyone have any ideas?
 
Any editor with a macro recorder should do the trick if all you need to do is add "*." to each line. I use Med to do this sort of thing.

All you do is record a macro that does:

Type *.
Down arrow
Home

Assign that to a shortcut key, put the cursor at the start of the first line, hit the shortcut key and let the keyboard repeat do the hard work...
 
Something like this should work in a batch file...

Code:
FOR /F "usebackq delims=" %%I IN (`dir ^"c:\new folder^" /a-d /s /b`) DO (
   FOR /F "usebackq delims=" %%J IN ("%%I") DO (
      ECHO *.%%J>>"%%~dpnI2.%%~xI"
      DEL "%%I"
   )
)

I've only tested it quickly but if I've understood what you are after it should do it. Though I strongly recommend copying the files to a temp location to test on first.

You need to have the batch file outside of where the other files are, otherwise it will run against itself. So in the first line change...

c:\new folder

... to be the location of where the files/folders are that you wish to run against.

In the line...

ECHO *.%%J>>"%%~dpnI2.%%~xI"

the 2 is what will be appended to the file name and can be changed to what you want. so...

myfile becomes myfile2
myfile.txt becomes myfile2.txt

The

DEL "%%I"

line can be removed if you don't want the original file being deleted.

Give it a go and let me know if it works or not, and again PLEASE TEST IT FIRST. :)
 
Back
Top Bottom