0

I have batch file which is prompting user to provide file name. File can or can't have space. My batch is failing if my file name has space in it. For example:

SET TDSX=.txt    
SET /p DS=%1    
move C:\Working\YAHAMA\DEV\Script\%DS%%TDSX% C:\Working\YAHAMA\NEW_FO
1

2 Answers 2

2

Put quotes around variable assignment like this:

set /p "DS=%~1"

The squiggly ~ thing removes any quotes from the argument. The above assumes that the file name on the command line has quotes around, otherwise, it's not a single argument as far as cmd is concerned, you may need:

set /p "DS=%*"
Sign up to request clarification or add additional context in comments.

2 Comments

Hey i just tried the way you said but it is not liking it SET TDSX=.txt SET /p "DS=%~1" move C:\Working\DEV\Script\%DS%%TDSX% C:\Working\NEW_FO
@SaketKrishna each single parameter that contains spaces must be surrounded with quotes move "C:\Working\DEV\Script\%DS%%TDSX%" C:\Working\NEW_FO
1

You need to protect the spaces in the resultant file and directory names using doublequotes too:

@Echo Off
CD /D "C:\Working\YAHAMA" 2>Nul || Exit /B
Set "TDSX=.txt"
Set /P "DS=%~1"
If Exist "DEV\Script\%DS%%TDSX%" (
    If Not Exist "NEW_FO\" MD "NEW_FO"
    Move "DEV\Script\%DS%%TDSX%" "NEW_FO"
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.