Win7 - cmd /c rename - Problem with ampersand in file name.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
apaugust
Posts: 2
Joined: 17 Jan 2014 11:45

Win7 - cmd /c rename - Problem with ampersand in file name.

#1 Post by apaugust » 17 Jan 2014 12:41

I just discovered that my batch file does not work properly for estName input that includes an ampersand.

e.g. 11th & Jefferson Apartments

The result of this input results in the file being renamed to "Concrete Price Proposal - 11th" with no extension.

I am inexperienced with batch files and it took me a little while manipulating quotation marks to get the batch file working properly when spaces were included in estName so I thought I would seek assistance on this one.

Thanks.

NEW ESTIMATE.bat (code outside the scope of the issue has been removed)

@echo off

:estInput
set /p estNum= ENTER NUMBER: E14-
set /p estName= ENTER NAME:

:createFolder
robocopy "S:\Estimating\Master Estimate Files" "S:\Estimating\Estimate Archive\2014\E14-%estNum% %estName%" /E /xc /xn /xo

:renameProposal
forfiles /p "S:\Estimating\Estimate Archive\2014\E14-%estNum% %estName%" /m "Concrete Price Proposal Master - Fax.doc" /c "cmd /c rename @path """Concrete Price Proposal - %estName%.""@ext""

:exit
exit

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Win7 - cmd /c rename - Problem with ampersand in file na

#2 Post by foxidrive » 17 Jan 2014 23:32

Try this cmd tail:

Code: Select all

/c "cmd /c rename @path 0x22Concrete Price Proposal - %estName%.@ext0x22"

apaugust
Posts: 2
Joined: 17 Jan 2014 11:45

Re: Win7 - cmd /c rename - Problem with ampersand in file na

#3 Post by apaugust » 20 Jan 2014 13:02

That worked! Why is 0x22 processed differently than quotation marks? If the answer is too complex don't worry about answering :D

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Win7 - cmd /c rename - Problem with ampersand in file na

#4 Post by foxidrive » 20 Jan 2014 21:36

Each character has a code number, and in this case a double quote is Hex 22 and forfiles represents that as 0x22

The reason why a lot of languages use a code to represent some characters is because some characters already have a special purpose,
and here forfiles (sometimes) has problems with double quotes, as it uses them to surround the cmd /c term.

Post Reply