Home > Software design >  "^" stripped from filename in .bat script on Windows when file is dropped onto .bat script
"^" stripped from filename in .bat script on Windows when file is dropped onto .bat script

Time:10-19

I'm not much of a Windows user, but I have need to write a simple .bat script to automate building a folder from a file and a couple of other folders. I want to drag and drop a folder onto the .bat script to execute the script.

The problem is that many of the folder names will have the "^" sign in the name for the folder, and when I drag and drop such folders onto the .bat script the '%1" in the script has the folder name, but the '^' character is stripped out for some reason.

Is there a way to get the literal folder name without losing the '^' characters ?

I'm adding more info with the example. My .bat file is like:

@echo off
echo %~1
mkdir USB
xcopy /s radiantUSB USB
move "%~1" USB\
echo "FINISHED"
@pause

and the name of the folder I am dropping on the .bat file is:

Duck^Donald^Quack

and the path that it is extracting is:

C:\Users\sscotti\Desktop\DuckDonaldQuack

The '^' is removed and move "%~1" USB\ fails because it the path to the folder to move is incorrect.

CodePudding user response:

Use %~1, not %1.

Dealing with the special meaning of characters within your batch is another question. Since you don't show us your batch, just how long is a piece of string?

Here's my test batch

@ECHO OFF
ECHO ----%~nx0--%*
SETLOCAL

ECHO "%1"
ECHO "%~1"
ECHO "%*"
pause
GOTO :EOF

And the test filename was U:\Test space^caret&ampersand!exclam%percent.bat

Here's the result

----qcifn.bat--"U:\Test space^caret&ampersand!exclam%percent.bat"
""U:\Test spacecaret
'ampersand!exclam%percent.bat""' is not recognized as an internal or external command,
operable program or batch file.
"U:\Test space^caret&ampersand!exclam%percent.bat"
""U:\Test spacecaret
'ampersand!exclam%percent.bat""' is not recognized as an internal or external command,
operable program or batch file.

CodePudding user response:

You can't fetch a single caret ^ with %1 nor %*, if it isn't quoted. That's because, cmd.exe use the caret as an escape character and remove it from the arguments.

But in the hidden variable cmdcmdline all characters are present.

This works with nearly all special characters.
Tested with Donald^Duck, Dagobert ^Duck, Cat&Dog

It only fails for filenames like Cat&dog(). To be bullet proof, you need an additional AutoRun batch file, that fixes the drag&drop handling.

@echo off
setlocal DisableDelayedExpansion
set index=0
setlocal EnableDelayedExpansion

rem *** Take the cmd-line, remove all until the first parameter
rem *** Copy cmdcmdline without any modifications, as cmdcmdline has some strange behaviour
set "params=!cmdcmdline!"
set "params=!params:~0,-1!"
set "params=!params:*" =!"
echo params: !params!
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
    for %%# in (!index!) do (
        endlocal
        set /a index =1
        set "item_%%#=%%~G"
        setlocal EnableDelayedExpansion
    )
)

set /a max=index-1

rem list the parameters
for /L %%n in (0,1,!max!) DO (
  echo %%n #!item_%%n!#
)
pause

REM ** The exit is important, so the cmd.exe doesn't try to execute commands after ampersands
exit
  • Related