How to parse multiple file name arguments passed to a Windows batch (.BAT) file with file name containing commas?

52 views Asked by At

I'm trying to echo files dropped on a batch file using command FOR, but this method is broken if a file has commas , in it's name.

Example of files being dropped:

Testfile.txt
Pictures,photos,GIFS.zip
Personal documents.7z

Code:

for %%a in (%*) do echo ---  "%%a"

The resulting echos should be the previous file list, but instead it gives me something like this:

testfile.txt
Pictures
photos
GIFS.zip
Personal documents.7z

It separated Pictures,photos,GIFS.zip by it's commas.

How do I solve this?

Another thing: I noticed that if instead type the batch in a command line with those example files as arguments, it behaves correctly by maintaining the zip filename whole IF I put it between "". So I guess the solution should be to make sure every %* comes with surrounding "", but how do I do that if they are already wrongly separated at the source, meaning a echo %1 & echo %2 & echo %3 will show pictures,photos,GIFS.zip as already separated strings?

I can't believe that I can't retrieve filenames dropped in a batch file if those have commas in their names.

0

There are 0 answers