Page 1 of 1
Filenames or foldername with spaces issue in a FOR loop
Posted: 20 Oct 2011 21:29
by balu2011
In a batch script, I am trying to return a directory listing of all files recursively in a folder, but the files or folders with spaces get truncated. Here's the code I am trying out:
Code: Select all
@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
for /f %%J in ('"dir /A-D /S /B H:\Script\test\batch"') do (
echo %%J
REM do_something...
)
gives something like....
G:\scripts\New (whereas the folder name is "New Folder")
G:\scripts\my (whereas the filename is "my file.txt")
G:\scripts\script.bat (works fine when there is no space in file or folder)
What do I need to change to get the entire filename or folder name?
I am trying this on Windows Vista Enterprise edition. Running just the "dir" command gives all folders and files with spaces, too.
Your help is greatly appreciated. Thank you.
Re: Filenames or foldername with spaces issue in a FOR loop
Posted: 20 Oct 2011 22:22
by dbenham
The HELP FOR documentation describes the FOR /F parsing behavior a bit, as well as the options you can use to control the parsing. But it is not very well written.
You have two simple choices to get the behavior you want with your FOR /F loop
1) Set tokens to All using "tokens=*"
Code: Select all
for /f "tokens=*" %%J in ('"dir /A-D /S /B H:\Script\test\batch"') do (
echo %%J
REM do_something...
)
2) Disable delimiters using "delims="
Code: Select all
for /f "delims=" %%J in ('"dir /A-D /S /B H:\Script\test\batch"') do (
echo %%J
REM do_something...
)
This 3rd option is wrong - Correction 2 posts downYou can also get your desired result using the simple FOR with the /R (recursively apply to sub-directories) option:
Code: Select all
for /r %%J in ("H:\Script\test\batch\*") do (
echo %%J
REM do_something...
)
Dave Benham
Re: Filenames or foldername with spaces issue in a FOR loop
Posted: 20 Oct 2011 22:57
by balu2011
Thank you very much, Dave. That was very helpful. I tried all the 3 option you provided. The first 2 worked, but the last one didn't.
Also, I went thru' the HELP for FOR, and tried using the backquotes too, and that worked.
Code: Select all
for /f "usebackq delims==" %%J in (`dir /A-D /S /B H:\Script\test\batch`) do (
echo %%J
)
The command I need to run on the files requires double quotes (due to the spaces). Is there a simple concatenation code for this? I tried a few things, but they didn't work...like:
Code: Select all
for /f "usebackq delims==" %%J in (`dir /A-D /S /B H:\Script\test\batch`) do (
set "strX="%%J""
echo.%strX%
)
I am not much of a windows scripting guy...more of a Perl guy. So, my apologies in advance.
Re: Filenames or foldername with spaces issue in a FOR loop
Posted: 20 Oct 2011 23:36
by dbenham
balu2011 wrote:I tried all the 3 option you provided. The first 2 worked, but the last one didn't.
Sorry

Sloppy mistake on my part. I got the syntax wrong
Here is the correct form for option 3
Code: Select all
for /r "H:\Script\test\batch" %%J in (*) do (
echo %%J
REM do_something...
)
balu2011 wrote:The command I need to run on the files requires double quotes (due to the spaces). Is there a simple concatenation code for this?
The syntax is even simpler:
Dave Benham