Solved

ms-dos variable @path doesn't include & symbol of path - workarounds?

Posted on 2008-10-17
7
734 Views
Last Modified: 2012-05-05
On any folders that have an & character in the folder name, the above (or below?) command seems to stop reading the name at that symbol.  For example, I have a folder called "A & A businees, inc."  When I run the forfiles command, the resultant command only shows "md c:\test\A " instead of md "c:\test\"A & A businees, inc."\TY08\Tax Returns"
It drops the & symbol and the rest of the name.  Ideas on how to overcome this?  I included the slashes so that I could get it to make folders with a space in the name - however, I'm not getting any folders with & in the name now.

Here's the catch:  If I run the second command included, the resultant command has the path correct (but isn't creating the directory as I wanted with a space.  I had to include the underscore.
1. forfiles /p c:\test /c "cmd /c if @isdir==TRUE md \"@path\TY08\Tax Returns\""
 

2. forfiles /p p:\ /c "cmd /c if @isdir==TRUE echo md @path\TY08\Tax_returns" > c:\fileorig.txt

Open in new window

0
Comment
Question by:bruderbell
7 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 400 total points
Comment Utility
This may work better using the FOR command instead of FORFILES.

Paste the script below into a text file with a .cmd extension.  Running it will create the subfolders.

If you are going to run this from the command prompt rather than from a batch script, replace each instance of %%G with %G.


@echo off

setlocal
 

set folder=c:\test
 

for /F "tokens=*" %%G in ('dir "%folder%" /A:D /B') do md "%folder%\%%G\TY\Tax Returns"

Open in new window

0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
If shift-3's comment doesn't work, it may be because & is used to join two commands on one command line.  For example:
C:\Temp\scripts>@echo firstline & @echo secondline
firstline
secondline

if you were not putting the & in a variable, you could probably escape it using ^ - for example,
C:\Temp\scripts>@echo firstline ^& @echo secondline
firstline & @echo secondline

Given this behavior and the possibility for problems, I would suggest your best solution is to search and replace every ampersand with the word "and"

For example, instead of using 'dir "%folder%" /A:D /B', make it two separate commands and do some editing:
Command 1:
dir "%folder%" /A:D /B>folderlist.txt

Then using notepad, do a quick search and replace, then run an if command similar to the one suggested above:
for /F "tokens=*" %%G in (folderlist.txt) do md "%folder%\%%G\TY\Tax Returns"

0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
You can probably also get around this using the for command alone:

@echo off
setlocal
 
set folder=c:\test
 
for /F "tokens=1* delims=&" %%G in ('dir "%folder%" /A:D /B') do (
   If "%%H"=="" (
      md "%folder%\%%G\TY\Tax Returns"
   ) Else (
      md "%folder%\%%G ^& %%H\TY\Tax Returns"
      REM The next line replaces the & with the word and in the folder name but is commented out at the moment.
      REM md "%folder%\%%G and %%H\TY\Tax Returns"
   )
)
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
And as I think about it and re-read the question, you cannot replace the & without renaming existing folders... so scratch that.  There's another way you can probably get around this:



@echo off

setlocal
 

set folder=c:\test
 

Rem create sub folders for all clients with ampersands in their folder names

for /F "tokens=1* delims=&" %%G in ('dir "%folder%" /A:D /B ^| Find /i "&"') do md "%folder%\%%G ^& %%H\TY\Tax Returns"
 

Rem create sub folders for all clients without ampersands in their folder names

for /f "tokens=*" %%G in ('dir %folder% /ad /b ^| Find /i /v "&"') do md "%folder%\%%G\TY\Tax Returns"

Open in new window

0
 
LVL 38

Expert Comment

by:Shift-3
Comment Utility
Is there some problem with my method?
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 100 total points
Comment Utility
Here give this a go.

If you're running it from the command line then use this:
forfiles /p c:\test /c "cmd /c if @isdir==TRUE for %a in (@path) do md \"%~a\TY08\Tax Returns\""

forfiles /p c:\test /c "cmd /c if @isdir==TRUE for %%a in (@path) do md \"%%~a\TY08\Tax Returns\""

Open in new window

0
 

Author Closing Comment

by:bruderbell
Comment Utility
Both of your solutions fix the problem, apparently through the use of different variables than those that are available simply in the /c cmd format.  Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now