Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Ignore/Include Commas in string

Status
Not open for further replies.

jmarkus

Technical User
Oct 15, 2002
124
CA
I am writing a cmd script for XP. The script runs from a particular subdirectory and needs to determine the project directory it is in which is in the form of D:\Main\ProjectNum.
Once it does that it needs to list all the subdirectories of that project and create a string of the form:
"-psub1 -psub2 -psub3" (etc)

The problem is some of the subdirectories have commas in the name (e.g. "Pics,Photos,Etc").

When I run the code below it only grabs the subdirectory name up to the first comma.

How can I tell it to take the entire line and not just up to the comma?

======================
Code:
REM To find the project directory you are in:
for /f "tokens=1,2,3 delims=\ " %%a in ("%cd%") do set toppath=%%a\%%b\%%c
REM To list subdirectories: 
dir /ad /b /s %toppath% > directories.txt
REM This is stripping the line after the comma
for /f %%D in ('type directories.txt') do (set pathlist=-p%%D %pathlist%)
echo %pathlist%
========================

Thanks,
JM
 
Okay, I figured out the comma problem, but now even though I can see the script echo the whole value of the variable to the screen, it doesn't seem to set the variable:

Code:
REM To find the project directory you are in:
for /f "tokens=1,2,3 delims=\ " %%a in ("%cd%") do (set toppath=%%a\%%b\%%c)
REM To list subdirectories: 
dir /ad /b /s %toppath% > directories.txt
for /f "delims=" %%D in ('type directories.txt') do (set /p pathlist=-p"%%D" <NUL)
echo %pathlist%

Maybe now someone can shed some light?

Thanks,
JM
 
Well, one issue is the "/p" on your SET command. That tells SET to expect input from the keyboard. I don't think that's what you want. The second issue is that you are overwriting the value of pathlist each time. I think what you need is in your first example, and your line should look something like this:
Code:
for /f "delims=" %%D in ('type directories.txt') do (set pathlist=-p"%%D "%pathlist% <NUL)
 
Okay, so I changed my strategy a little and dumped it into a file. It seems there is some limit to the string size.

Code:
REM To find the project directory you are in:
for /f "tokens=1,2,3 delims=\ " %%a in ("%cd%") do (set toppath=%%a\%%b\%%c)
REM Use SUBST to try to reduce string size - not good enough
subst W: %toppath%
REM To list subdirectories: 
dir /ad /b /s W: > directories.txt
for /f "delims=" %%D in ('type directories.txt') do (set /p pathlist=-p"%%D" %pathlist% < directories.txt) >> pathlist.txt

So this works now. My problem now is I need to use the contents of that file as an argument in a command that doesn't read files to take arguments, and makes the string too long if I echo it into the command line.

I think this is a brick wall.
JM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top