Help with long filenames in batch loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stugreene
Posts: 3
Joined: 19 May 2010 13:52

Help with long filenames in batch loop

#1 Post by stugreene » 19 May 2010 14:13

I want to create a batch file to zip several files into separate archive files.

The files in question look like:
weekly status update 20080229.xls
weekly status update 20080321.xls
etc.

I want to preserve the long filename but just make it a .zip file

my batch file reads:
for /f %%a in ('dir /b "weekly status update*.*"') do zip %%~na.zip %%a

The problem is that %%a is being set to "weekly", i.e. it is only getting up to the first space in the long filename. I can test this by changing the command to read:
do echo %%a

Everything I've read says that as long as I enclose the long filename in double-quotes it should be fine, so what am I missing? I'm using XP, start/run/cmd

Thanks,
Stu Greene

Graffic
Posts: 5
Joined: 19 May 2010 14:28

Re: Help with long filenames in batch loop

#2 Post by Graffic » 19 May 2010 15:33

I'm a total noob (in fact I just made my first post and it's also a question) but I think this will do what you need it to do, or at least get you going in the right direction:

Code: Select all

for %%a in ("weekly*.*") do zip "%%a" "%%~na.zip"

Part of the problem is that the last two arguments were reversed and they need to be in quotes too since you're dealing with the same long filenames there..

I hope it's of some help.

graf

stugreene
Posts: 3
Joined: 19 May 2010 13:52

Re: Help with long filenames in batch loop

#3 Post by stugreene » 19 May 2010 15:53

Thanks for the reply, but when I do that:
for %%a in ("weekly status update*.*") do zip "%%a" "%%~na.zip"

I get:
zip "weekly" "weekly.zip"
zip warning: name not matched: weekly.zip

If I leave off the double-quotes in the do command, I get:
zip weekly weekly.zip

the problem appears to be in assigning the long filename to the %%a variable - I'm only getting the first word.

PS - the destination archive file does come first in the zip command

Graffic
Posts: 5
Joined: 19 May 2010 14:28

Re: Help with long filenames in batch loop

#4 Post by Graffic » 19 May 2010 16:02

Hmm.. sorry. :(

And yeah, you're right.. it's been a long time since I've used command line zip, hehehe.

graf

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with long filenames in batch loop

#5 Post by aGerman » 19 May 2010 16:08

I'm not familar with the syntax of zip, but using quots is the right way.
try
[EDIT You have to replace the standard delimiter by "nothing"]

Code: Select all

for /f "delims=" %%a in ('dir /a-d /b "weekly status update*.*"') do zip "%%~na.zip" "%%a"


Regards
aGerman

stugreene
Posts: 3
Joined: 19 May 2010 13:52

Re: Help with long filenames in batch loop

#6 Post by stugreene » 20 May 2010 07:12

aGerman,

Thanks, that worked perfectly!

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Help with long filenames in batch loop

#7 Post by avery_larry » 21 May 2010 08:49

And for the record -- you have to use the usebackq option in order to use double quotes as part of the filename/wildcard.

Your for command treated "weekly status update*.*" as a STRING, not as a filename/wildcard -- so "weekly" is the first token -> %%a.

Code: Select all

for /f "usebackq" %%a in ("weekly status update*.*") do zip "%%a" "%%~na.zip"

Post Reply