Filenames or foldername with spaces issue in a FOR loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
balu2011
Posts: 2
Joined: 20 Oct 2011 19:29

Filenames or foldername with spaces issue in a FOR loop

#1 Post by balu2011 » 20 Oct 2011 21:29

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Filenames or foldername with spaces issue in a FOR loop

#2 Post by dbenham » 20 Oct 2011 22:22

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 down
You 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
Last edited by dbenham on 21 Oct 2011 05:47, edited 1 time in total.

balu2011
Posts: 2
Joined: 20 Oct 2011 19:29

Re: Filenames or foldername with spaces issue in a FOR loop

#3 Post by balu2011 » 20 Oct 2011 22:57

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Filenames or foldername with spaces issue in a FOR loop

#4 Post by dbenham » 20 Oct 2011 23:36

balu2011 wrote:I tried all the 3 option you provided. The first 2 worked, but the last one didn't.
Sorry :oops: 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:

Code: Select all

set strX="%%J"


Dave Benham

Post Reply