Passing quotes within quotes in a BATCH file - batch-file

I have been looking around for a solution to this one, but so far I have been unsuccessful. I am trying to open two programs using a batch file. The problem is that both of the paths to these files contain spaces, and one of these paths is already enclosed in quotation marks. Because of the the second file (.gh file) does not open. I have already tried putting the second path in quotation marks, and using the \ character to escape the quotation marks, but without any result. Any help is appreciated.
#ECHO OFF
cd C:\Users\\Google Drive\Rhino Werk\MT1458\WIP
"C:\Program Files\Rhino 6\System\Rhino.exe" /nosplash /runscript="-grasshopper editor load document open C:\Users\<username>\Google Drive\Rhino Werk\MT1458\WIP\MT1458_HullGenerator_V2.64_NAKIJKMODEL.gh _enter" "C:\Users\<username>\Google Drive\Rhino Werk\MT1458\WIP\MT1458_HullGenerator_V2.59_001.3dm"

#ECHO OFF
cd /d "C:\Users\\Google Drive\Rhino Werk\MT1458\WIP"
"C:\Program Files\Rhino 6\System\Rhino.exe" /nosplash /runscript="-grasshopper editor load document open \"%userprofile%\Google Drive\Rhino Werk\MT1458\WIP\MT1458_HullGenerator_V2.64_NAKIJKMODEL.gh\" _enter \"%userprofile%\Google Drive\Rhino Werk\MT1458\WIP\MT1458_HullGenerator_V2.59_001.3dm\""
This would be following C++ argv rules of escaping inner double quotes with a backslash. Whether this works or not is unknown as I do not have or use Rhino.

Related

How can I open a folder in explorer.exe with a comma in the folder name?

I have made a shortcut for opening folders based on job numbers for work. It looks through the job directory for the job number and then opens that folder. Unfortunately, this fails when the folder name has a comma "," in it:
IF %JOBNUM%==!JOBTEST! %SystemRoot%\explorer.exe %%G
Where, for example:
%%G = X:\A12300-12399\A123456 - Job with, comma
Instead of opening the job folder, it just opens My Documents (which, I assume, is the default location for Explorer.
Is there a way to let it know that the comma is part of the folder name and not some sort of delimiter?
Try adding the path inside quotes like "path"
Batch files are essentially shell commands executed as a batch. Commands are delimited by space, "," , "=" etc. As shown Here
When you use a pathname with a delimiter, windows takes the first string to be the actual value and discards the rest. Using quotes is explicitly instructing the shell to treat everything inside the quotes as a single unit. That goes the same for %%G.

batch file to automate tree command > pdf file

I am new to batch files but was looking to create one that would use the tree command to write a .txt file of the directory in which the batch file is located and then convert that .txt file to a Word file or PDF that the every day user could view.
This was my attempt at the first part of the process;
tree %~dp0 > %~dp0/"Folder Contents.txt" /A /F
but this gave me an "Invalid Path" message in the .txt file
If anyone could point me in the right direction it would be greatly appreciated.
Thanks in advance for your help.
JosefZ has placed the correct command in the comments but I thought I would add a bit of an explanation here:
Think of how you path would look like with your command. It would be like C:\PathTo\Batch\/"FolderContent.txt" which actually is not a valid path.
%~dp0 will end with a \ so the / in the command from the question is syntactically incorrect.
Further the double quotes should be placed around the total path as JosefZ did in his comment. If you do not do that the path will contain these quotes and will be invalid.
JosefZ further added double quotes and a single . around and at the end of %~dp0 after your tree command. The quotes are there to include possible whitespaces into your folder-path and the . to include folders with subfolders as well. You will get an error message if you do not.
Last thing is that he placed the output file at the beginning of the command and got rid of spaces. You can basically place your output file anywhere however. Although you should notice that you should not contain spaces between > and your output-file-path.

Batch %~f1 works but %~dp1 fails because of spaces

I am trying to create a batch script to send a shortcut of a file to my start menu but I have ran into trouble setting the "Start in" option. I am using Shortcut from Optimum X to do it and this may be an error from their decade old program and not my code.
%~f1 = C:\Program Files (x86)\Example\Example.exe
%~dp1 = C:\Program Files (x86)\Example\
Running
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%~f1" /a:c /t:%1
works as intended and creates a shortcut with the "Start in" set to the dir of the file. But running
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%~dp1" /a:c /t:%1
fails when it encounters a space, for example if the path is in Program Files the error is
"The Parameter "Files" is invalid
The Syntax of the command is incorrect"
Sorry if this is not enough information I know very little Batch.
Your problem are not the spaces, but how the standard arguments are handled.
In your case, you are not passing the data to a internal command, but to a external executable that will use its own parser (the default C startup code). To this parser a backslash followed by a quote is a escaped quote that does not end the argument.
As your %~dp1 ends with a backslash, it escapes the quote, so the argument continues until the first unquoted space, in your case after Program in the /t switch. So, Files (in the /tswitch) is not recognized as a valid argument.
The usual approach is to remove the ending backslash with something like
for %%a in ("%~dp1.") do (
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%%~fa" /a:c /t:"%~1"
)
Here the for command is used to process the path to the file with an added dot. This why when we retrieve the full path to the referenced element (%%~fa) the ending backslash will not be included.
But, if the target folder is the root of the drive, this approach will not work. The ending backslash can not be removed. So, another approach is to avoid the quote scape by simply escaping the backslash
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%~dp1\" /a:c /t:"%~f1"
Now the quote is preceded by two backslashes that will be seen as a escaped backslash followed by the closing quote of the argument.

Double quotes is not working for calling a folder with spaces from Command Prompt

I am trying to create a bat file to run testng from command prompt. Problem is that my path contains many folders with spaces in its name. I followed the step mentioned in How to write a full path in a batch file having a folder name with space? and added double quotes for the folder with space in its name but the issue is that the application is also considering the folder name with quotes and hence unable to find the given folder.
Here is my command.
set ProjectPath="C:\Test\ABC Online"
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\Framework Jar Files\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testng.xml
but everytime getting an error "Error: Could not find or load main class org.testng.TestNG". In the above code when I echo Projectpath, I am getting something like this.
"C:\Test\ABC Online" (Double quotes)
If I change the folder name to ABC_Online and run the same command without double quotes, it work pretty fine.
Am I missing something. Please suggest.
In your set command line, the quotation marks are part of the value, and there are trailing spaces at your command line:
set ProjectPath="C:\Test\ABC Online"SPACESPACESPACESPACE
So the command line java org.testng.TestNG "%ProjectPath%\testng.xml" expands to this:
java org.testng.TestNG "C:\Test\ABC Online" \testng.xml
Use the following syntax so the spaces and the quotation marks do not become part of the variable value:
set "ProjectPath=C:\Test\ABC Online"
When using the value later, put quotes like this:
java org.testng.TestNG "%ProjectPath%\testng.xml"

Batch: % in file name

I think this is a simple batch programming question, but after much searching, I can't find the answer.
I'm trying to figure out how to refer to files whose names contain difficult characters.
It seems that double quotes have the effect of treating most enclosed characters literally. For example, for a file named ^^.txt, dir "^^.txt" will find the file, while dir ^^.txt won't.
However, I don't know how to escape %. For example, for a file named %ERRORLEVEL%.txt, none of these find the file:
dir "%ERRORLEVEL%.txt"
dir "%%ERRORLEVEL%%.txt"
dir "^%ERRORLEVEL^%.txt"
Any suggestions would be appreciated.
The escape character for the Windows command prompt is ^. It works without the quotes.
dir ^%errorlevel^%.txt
For your other example ^^.txt, use:
dir ^^^^.txt
Always avoid % and ^ in filenames. Rename % to 'percent' in a global file manager.
They are next to impossible to handle properly in many batch commands.
In a batch script you must double the %, eg.:
#echo off
dir %%errorlevel%%.txt
Btw. you should not use cmd default variable or command names for other things (errorlevel).

Resources