简体   繁体   中英

CMD: Check for quotes

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 (eg, 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 (eg, :\\=_ 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM