Why would this batch have issues when filenames have spaces?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
trclocke
Posts: 3
Joined: 25 Jun 2014 20:01

Why would this batch have issues when filenames have spaces?

#1 Post by trclocke » 29 Jun 2014 02:36

I added quotes where necessary - at least as far as I can tell. Can someone take a look at this and see where I'm going wrong?

There's more to it of course (user prompts for the variables, etc) but this is the meat of it. This line takes the source file %startfile% and uses it to replace all .png files in target folder %pathy% keeping the target filenames intact. %%a represents each individual file in the target folder as it goes through the for loop.

Code: Select all

for /f %%a in ('dir /b "%pathy%\*.png"') do COPY /Y "%startfile%" "%pathy%\%%a"


This works splendidly, unless the target files have spaces (spaces in the source or target folder don't seem to be an issue, but spaces in the filenames are). Say the target folder contains file "AppIcon 29x29.png" - the batch doesn't return errors, but makes a file named "AppIcon" (no extension) without affecting the intended file.

I'd love any help anyone can provide. I'm making an iphone icon theme, and this batch is going to save me a ridiculous amount of time if it works correctly :)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Why would this batch have issues when filenames have spa

#2 Post by Squashman » 29 Jun 2014 07:39

You should read the help on what FOR /F does. Open a cmd prompt and type: for /?

By default FOR /F will break apart your output using the default delimiters which means you need to tell it not to use a delimiter.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Why would this batch have issues when filenames have spa

#3 Post by Squashman » 29 Jun 2014 07:41

You do realize how dangerous this line of code is. This wouldn't be very funny if you told one of your friends to run it and he didn't have a backup of all his files.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Why would this batch have issues when filenames have spa

#4 Post by Aacini » 29 Jun 2014 08:29

Squashman wrote:You should read the help on what FOR /F does. Open a cmd prompt and type: for /?

By default FOR /F will break apart your output using the default delimiters which means you need to tell itnot to use a delimiter.

This is one of the (multiple) reasons because I don't recommend beginners to use for /F until they really need it. I don't understand why the pure and simple for command seems to be forgotten, taking into account that it is simpler and run faster...

EDIT: I added the missed "~NX" modifiers.

Code: Select all

for %%a in ("%pathy%\*.png") do COPY /Y "%startfile%" "%pathy%\%%~NXa"

Antonio
Last edited by Aacini on 29 Jun 2014 17:07, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Why would this batch have issues when filenames have spa

#5 Post by Squashman » 29 Jun 2014 08:36

I agree Antonio.
Was just trying to teach the OP how to fish.
Still think this code is dangerous. I see no reason to overwrite a bunch of files with the same data.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Why would this batch have issues when filenames have spa

#6 Post by foxidrive » 29 Jun 2014 13:45

Antonio, your command should not have the path added to the target:

Code: Select all

for %%a in ("%pathy%\*.png") do COPY /Y "%startfile%" "%%a"

trclocke
Posts: 3
Joined: 25 Jun 2014 20:01

Re: Why would this batch have issues when filenames have spa

#7 Post by trclocke » 29 Jun 2014 16:30

foxidrive wrote:Antonio, your command should not have the path added to the target:

Code: Select all

for %%a in ("%pathy%\*.png") do COPY /Y "%startfile%" "%%a"

(I noticed that myself, surprisingly ;)

Ah, I see. For one thing, I had no reason to define %%a as the file without the path, then tie it back into the path later. You're also right that I misunderstood the difference between for and for /f and didn't realize it was treating %%a as a string of tokens with space delimiters.

Antonio, huge thanks for including an actual answer for context instead of just the boilerplate RTFM reply. I haven't had to code anything in years, and I doubt it will come up again any time soon - I just needed this specific script to do its job. The "why" also makes a lot more sense with a real-world example.

Couple other replies:

Squashman wrote:I agree Antonio.
Still think this code is dangerous. I see no reason to overwrite a bunch of files with the same data.

Why would I write a script to accomplish this if there wasn't a need? I of course include user prompts confirming the sourcefile and target folder before allowing the batch to run, and am hugely careful to disclaim what this does and why. As far as the "dangerous" for /f loop itself, I lifted it from someone who I knew was able to use it for something nearly identical to my intended purpose (I only had to revise the "do" section) and used echo to carefully test what it intended to do before actually adding the copy section. Incidentally, the fact that the end-user has a backup of the target files is implicit here. They've extracted them from a zip file.

If you must know, when creating an icon theme for a jailbroken iOS device, each target icon to theme contains a folder full of multiple files, sometimes upwards of 20 or more, the only difference between each file being their resolution, which long story short has no bearing on an icon theme other than filesize (I'll probably use a command-line tool to read and account for the resolution issue to save a little space at some point, but that's a topic for another day). The quickest solution, then, is to make your newly themed image then make copies of it to replace everything in an icon's folder. I can do that one at a time through photoshop, but why? Anyway, hopefully this satisfies your curiosity a bit.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Why would this batch have issues when filenames have spa

#8 Post by Compo » 30 Jun 2014 11:37

There is also appears to be no need for the /Y switch with the COPY command!

trclocke
Posts: 3
Joined: 25 Jun 2014 20:01

Re: Why would this batch have issues when filenames have spa

#9 Post by trclocke » 30 Jun 2014 12:56

Compo wrote:There is also appears to be no need for the /Y switch with the COPY command!


The user would have to manually confirm each file replacement without it, unless i'm missing something.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Why would this batch have issues when filenames have spa

#10 Post by Compo » 30 Jun 2014 15:31

trclocke wrote:
Compo wrote:There is also appears to be no need for the /Y switch with the COPY command!


The user would have to manually confirm each file replacement without it, unless i'm missing something.


You are using a batch script therefore; from COPY /?:
Default is to prompt on overwrites unless COPY command is being executed from within a batch script.

Post Reply