Question

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

Was it helpful?

Solution

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.

OTHER TIPS

%%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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top