Force quotes to protect DnD arguments
Posted: 05 Aug 2009 01:49
Hi,
I want to do a DOS script that do some stuff with the files that are passed as arguments (by drag and drop files to the script), but I have encountered special cases where the script cannot retrieve the argument correctly : short file name with special character like & (ampersand).
Imagine the simple script :
@echo %*
Here are the results when I Drag & Drop some files to this script
--> DnD C:\file.txt
C:\file.txt
--> DnD C:\a&b.txt
C:\a
--> DnD C:\a & b.txt
"C:\a & b.txt"
The problem is when the filename doesn't have space but has some ampersand (second example). In that case, it is not automatically surrounded by quotes and the ampersand is incorrectly interpreted.
If I force a space to the filename (third example) , then DOS will protect my argument by quotes and the ampersand is correctly kept in the filename.
Is there a trick to work with any filename passed to my script ?
I want to do a DOS script that do some stuff with the files that are passed as arguments (by drag and drop files to the script), but I have encountered special cases where the script cannot retrieve the argument correctly : short file name with special character like & (ampersand).
Imagine the simple script :
@echo %*
Here are the results when I Drag & Drop some files to this script
--> DnD C:\file.txt
C:\file.txt
--> DnD C:\a&b.txt
C:\a
--> DnD C:\a & b.txt
"C:\a & b.txt"
The problem is when the filename doesn't have space but has some ampersand (second example). In that case, it is not automatically surrounded by quotes and the ampersand is incorrectly interpreted.
If I force a space to the filename (third example) , then DOS will protect my argument by quotes and the ampersand is correctly kept in the filename.
Is there a trick to work with any filename passed to my script ?