Let's say, the user drag and drops an file into my batch file, which causes that the batch file copies the file to a certain directory. the problem is the following command:
copy "%1" "C:\path\to\it"
The problem here is the quotes around%1. When you drag and drop something in a batch file, normally, it wouldn't put quotes, so everything's fine. But when i convert my batch file to EXE, it actually puts quotes there.
So, what i want to do is, checking if the file does have quotes, if not, than do the command above, otherwise, do the command without quotes.
Thanks.
Would the following work?
copy %1 "C:\Dir1\Dir2"
my few attempts to find a problem not quoting %1 have not resulted in adverse effects.
Not sure if the answer given here was what you were seeking, and I may be a couple of years late, but I would like to offer an alternate solution to your quoting problem as I have run into it something similar myself. Maybe it will benefit someone else.
You ARE able to strip the quotes from around variables, including the ones that are dragged and dropped. Here's how:
add a tilde to the %n variable (e.g., copy %~1 "C:\path\to\it")
For other variables within the batch file, use a similar technique, this time performing a substitution of the double-quote for nothing :"=, as in:
set filename="C:\path\to\it"
echo %filename% (will give you "C:\path\to\it")
set noquotefilename=%filename:"=%
echo %noquotefilename% (will give you C:\path\to\it without the quotes)
It is not my original solution, but I found it near the bottom of the following post:
Removing double quotes from variables in batch file creates problems with CMD environment
The substitution technique has other applications, as you could easily substitute any character for any other (e.g., :\=_ to substitute a back-slash for an underscore).
Hope this helps!
The problem is the process by which you are converting to EXE. Try a different BAT to EXE converter.
Related
So I'm trying to use bat scripts to get the information I need for my pipelines.
An example of that is the line below, where "path" is a variable that contains an absolute path to a file or folder.
String dirOutput = bat(script: "dir ${path}", returnStdout: true)
This works well, except when this path has spaces in it. At which point I will have an error.
I have found this answer: https://stackoverflow.com/a/11903788/20011929
But wasn't able to add the double quotes in the script argument.
Any idea how to add the quotes there?
Note: This has been an issue for other scripts as well, not just dir, so I would prefer to find a solution about the quotes and not specifically about the funtionality of the line.
Thanks all
I am trying to write a Doxygen file filter that lives in a subdirectory that works from Doxyfile on both Windows and Linux.
Doxyfile
/scripts
myfilter
I seem to be unable to specify the path using a forward-slash on Windows unless it is quoted:
"scripts/myfilter"
However, trying to quote the command in Doxyfile does not work.
FILTER_PATTERNS = *.glsl=""scripts/runpython" scripts/doxygen-glslfilter.py"
On Windows, you get an error that implies the quotes don't exist.
'scripts' is not recognized as an internal or external command, operable program or batch file.
Doxygen uses popen() to run these commands and will remove the wrapping quotes around the command, but it does not seem to remove all quotes.
popen() call:
https://github.com/doxygen/doxygen/blob/master/src/definition.cpp#L745
filter name quote strip:
https://github.com/doxygen/doxygen/blob/master/src/util.cpp#L2458
However, the result is the same as if there were no quotes.
Update
I was able to get command logging in Doxygen, and it appears the extra quotes are being stripped in an odd way. You can see how there is a space in front of the command.
Executing popen(` scripts/runpython scripts/doxygen-glslfilter.py "C:/dev/g3d/G3D10/data-files/shader/AlphaFilter.glsl"`)
Update
I submitted a bug report/feature request but I doubt it will be read.
Doxygen Bug Report
The issue was reported to the doxygen project, and they have provided a solution where any '/' in the command is replaced by '\' on Windows.
https://bugzilla.gnome.org/show_bug.cgi?id=792846
This was done to resolve a similar issue here:
What is the QHG_LOCATION path relative to for doxygen?
The pull request for the project on github here: https://github.com/doxygen/doxygen/pull/703
When using double quotes within double quotes in a single string, it sees the first double quote as the start string and the next double quote as the end of the string.
So in your example:
""scripts/runpython" scripts/doxygen-glslfilter.py"
The first 2 quotes are seen as open and close, then it sees scripts/runpython as the next command etc.
I do not have the same tool, but these 2 examples will probably sort out your issue.
This example wraps each set in double quotes, and the entire set in single quotes.
FILTER_PATTERNS = *.glsl='"scripts/runpython" "scripts/doxygen-glslfilter.py"'
Where this example wraps the first set in double quotes and the entire set in single quotes.
FILTER_PATTERNS = *.glsl='"scripts/runpython" scripts/doxygen-glslfilter.py'
NOTE!! I am unable to test this as I do not have the same environment as you. I am therefore not sure if the second option will work, as it might also need scripts/doxygen-glslfilter.py in double quotes, I am adding it to the answer regardless.
I have a problem with spaces in directory names in a batch script.
I store a base directory and then use that to make subdirectories and files, something like:
set basepath=c:\some\path
set logdir=%basepath%\log
set logfile=%logdir%\test.log
But the basepath on some servers have spaces in it. Earlier I used dir /x to get the shortened 8.3 names but I encountered one server where this doesn't work (apparently there is some setting to disable this, and I don't have privileges to turn it back on). So now I'm trying to figure this out. I need to concatenate filename/directories to basepath, which may have spaces in it. I tried using double quotes, but it didn't work.
At the command prompt, you can do things like cd "some path"\with\spaces using a combination of double quoted directories and non-double-quoted directories. But this doesn't work in a batch script.
Any suggestions?
set "basePath=c:\somewhere\over the rainbow"
set "logDir=%basePath%\logs"
set "logFile=%logDir%\kansas.log"
>> "%logFile%" echo This is a test
cd "%logDir%"
Don't insert quotes inside the variable values (unless it is necessary).
Use quotes surounding the set command to ensure no aditional spaces are stored in variables and to protect special characters.
Place quotes in the correct places in the final commands that make use of the variables.
Put double quotes around the environment variable only when you need to actually use it.
set basepath=c:\some\path with spaces
set logdir=%basepath%\log
xcopy *.log "%logdir%"
Then reference it as "%logdir%" and it will expand to "c:\some\path with spaces\log". This works because set puts everything after the = except for including trailing white-space into the environment variable.
I've been having quite an unusual problem. In my .bashrc file, I have set a variable to a path name with spaces in it. I had a feeling this would cause problems, but I played around with setting an alias in a similar way and got it to work like so:
alias npp="\"/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe\""
Now, I thought I could use the same trick for my environment variable -
export PRO="\"/cygdrive/c/Program Files (x86)\""
This worked. Kind of.
[myName]
$ echo $PRO
"/cygdrive/c/Program Files (x86)"
[myName]
$ cd $PRO
bash: cd: "/cygdrive/c/Program: No such file or directory
I've tried placing an escape before the space with and without removing the double quotes, I've tried single quotes with and without the escape. I've tried using grave accents as quotes. I've tried just the escape, I've tried
export PRO=/cygdrive/c/Program\\\ Files\\\ \\\(x86\\\)
None of this has worked. The only thing that has was -
export PRO="/cygdrive/c/Program Files (x86)"
$ cd "$PRO"
Ultimately, I'm trying to find a way to make my variable work without placing quotes around it every single time I type the variable. Having run out of ideas entirely I came here hoping for someone to be able to help me.
I got around this with wildcards:
export PRO="/cygdrive/d/Program*Files/"
Due to Word Splitting if you do not quote $PRO that white space breaks your path into multiple words.
There's no way to use a variable without quoting it in your case.
Technically, I cannot answer your question, but a good workaround is to create a link to the folder you want, create a variable for the link, and then cd to that variable. It's an annoying second step and a pointless link but if its worth putting in your bash profile it might be worth the extra hassel.
Here's what I did:
ln -sf '/cygdrive/c/Users/Mic/Desktop/PENDING - Pics/' '/cygdrive/c/Users/Mic/mypics'
Then I put this in my bash_profile and now I can cd to $pic
pic=/cygdrive/c/Users/Eric/mypics/
Just enclose the program file in double quotes in this way
/cygdrive/c/"Program Files (x86)"
It works for me
I ran into a similar issue with a shell script evaluating a directory path in a variable without escaping the space in "Program Files". I got around this by running Cygwin as an administrator and creating a symlink. None of the answers here worked.
ln -s "/cygdrive/c/Program Files" /cygdrive/c/ProgramFiles
I have a simple bash script running on OS X that removes specific files and directories and copies new ones in their place. One Mac .app directory contains a space, and when I run the script there is a "No such file or directory" error.
Here is the relevant code. A good amount has been cut out, so simply hard coding these differently isn't possible (the variables must be broken up as they are):
CP="cp -Rf"
INSTALL_DIR="/Applications/"
APP="The App.app"
NEWAPP="$HOME/Downloads/The App.app"
$CP "$NEWAPP " "$INSTALL_DIR$NEWAPP"
I've tried escaping The\ App.app with no luck, as well as trying single quoting, double quoting, and double escaping, but nothing has worked. I imagine there is a simple way to do this.
Thanks for your help.
You have an extra space there, it's simply
"$NEWAPP"
enclose the whole final line in quotes:
"$CP $NEWAPP $INSTALL_DIR$NEWAPP"
it's unnecessary to keep opening and closing the quotes here, and the $CP must be contained in quotes as CP has a space in it.