tags:

views:

237

answers:

4

I am trying to batch extract some rar's that are in some zip in some directories. Long story short this is my loop through the rar files:

for %%r in (*.rar) do (

unrar x %%r
)

Problem is that %%r gets the wrong value. If the files name is "file name.rar" then %%r gets the value "file" - it stops at the first space in the file name.

How do I get this loop to work file files with spaces in names?

Thank you

+1  A: 

unrar x "%%r"

ssg
A: 

The problem is that 'for' uses space as the default delimited. You can set this using the delims = xxx. look here for syntax. Or you can use ForFiles.

Jeremy
cmd is also not a UNIX shell which does globbing wherever it sees an asterisk. For every rar file you will have its filename completely contained in %%r, regardless of it having spaces or not.
Joey
+1  A: 

Try this:

for /f "usebackq delims==" %i in (`dir /b *.rar`) do unrar x "%i"

If you are using it in a batch file, remember you will need to double the percent signs to escape them.

Goyuix
Thank you very much.
Bojan
+1  A: 

%%r will contain the complete file name including spaces. It's your call to unrar which has the problem. If the file name contains spaces you have to enclose it in quotation marks, otherwise unrar won't be able to see that the two (space-separated) parameters file and name.rar are actually a single filename with a space.

So the following will work:

for %%r in (*.rar) do unrar "%%r"

Also, if you aer curious where the problem lies, it's sometimes very helpful to simply replace the program call with echo:

for %%r in (*.rar) do @echo %%r

where you will see that %%r includes the spaces in file names and doesn't rip them apart.

Joey
I thought of that, but it still wouldn't work like that. It seams that %%r really had a wrong value and quotes would do much.Maybe I got something else wrong... I don't know.
Bojan