Search code examples
batch-filecmdcd

cmd, batch cd variable space excaping


So i was making a batch file for creating a folder with HTML content. The batch file basically executes a python file, and, as I'm going to make the batch file global, the python file runs with a parameter which is the directory I want to create the folder on.

The python file is working correctly because I've tested it many many times!

It works like this:

C:/Users/Tiago^ Oliveira/programming/python/random/HTML_Folder/HTML_Folder.py here_the_directory_we_want

and my batch file is like this:

C:/Users/Tiago^ Oliveira/programming/python/random/HTML_Folder/HTML_Folder.py %cd%

Whenever I execute the batch file in the C: drive it gets me this error:

FileNotFoundError: [WinError 3] O sistema não conseguiu localizar o caminho especificado: 'C:\\Users\\Tiago\\html\\'

Which basically means the parameter is C:\\Users\\Tiago but in reality when I do ECHO %cd% it prints: C:\Users\Tiago Oliveira\programming\python\random\HTML_Folder, so this means there is a problem with my user name, (which, BTW, I cannot change).

I've tried to replace the space between the names with the "^ " to escape the space and a lot of different options and I can't figure this thing out.

So I would like a little help for the ones that know what I can do to make this work.


Solution

  • The path separator for cmd.exe is a REVERSE SOLIDUS (backslash).

    No CARET is needed.

    Paths need to use QUOTATION MARK characters.

    "C:\Users\Tiago Oliveira\programming\python\random\HTML_Folder\HTML_Folder.py" "%cd%"
    

    If this should always be under the current user's account directory, it would be better to use:

    "%USERPROFILE%\programming\python\random\HTML_Folder\HTML_Folder.py" "%cd%"