Try_Learning
Try_Learning

Reputation: 31

Batch file to write filenames from a directory into a text file with specific format

Wanted to write a batch file that reads file names from a directory and puts them into a text file in the same directory. Twist is that the filenames should have specific format explained below: Original file names are: ThisThat.shp, HellNono.shp, LifeGood.shp.... and so on. So the batch file should grab ThisThat, HellNono, LifeGood and put it into a text file like this:

ThisThat|ThisThat
HellNono|HellNono
LifeGood|LifeGood

filenames should be separated with a separator and each filename in a different line.

This is what I have come up with.

cd %1
if exist 8characters.txt del 8characters.txt
for /F "delims=" %%j in ('dir /A-D /B /O') do echo %%~nj >> 8characters.txt

This script just grabs filenames and I don't know how to store it in the format explained above.Any help will be greatly appreciated. Thanks

Upvotes: 1

Views: 293

Answers (1)

user330315
user330315

Reputation:

You are nearly there as %%~nj will output the filename as you want it. You just need to output it twice to get the format you want:

for /F "delims=" %%j in ('dir /A-D /B /O') do echo %%~nj^|%%~nj>> 8characters.txt

Note the ^ character in front of the |. The | is used to pipe the output to another command so it needs to be escaped and that is done using ^|

Upvotes: 2

Related Questions