2

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 3
    The drag-and-drop interaction between Windows Explorer and Command Prompt is *terribly* designed, file names only become quoted in case they contain _spaces_, but other token separators (like `,`, `;`, `=`) or characters with special meaning (like `&`, `(`, `)`, `^`) do not become protected by quotes (unless there is at least a _space_ in addition). Perhaps you are interested in [this related post](https://stackoverflow.com/a/31358421)… – aschipfl Oct 21 '20 at 19:16
  • @aschipfl So that's it, it has to have at least a space, I forgot to remove one from "Pictures,photos, GIFS.zip" when I wrote that here, that's why it certainly worked on your side and not on mine. I'm going to check the link you provided, thanks. – Pedro Coimbra Oct 22 '20 at 00:02
  • @Pedro: did the link aschipfl provided help you? Let us know, so we can link your question to that one to help future searchers with the same problem. – Stephan Oct 24 '20 at 13:10
  • @Stephan, the code worked, I will adapt it to my script. Frankly I didn't think it was possible to do that since the problem was at the root of the parsing, but I guess there are indeed batch wizards, lol. Yes, it provided newinsights to me. Thanks aschipfl – Pedro Coimbra Oct 30 '20 at 02:16

0 Answers0