我不是 Windows 用戶,但我需要撰寫一個簡單的 .bat 腳本來自動從一個檔案和幾個其他檔案夾構建一個檔案夾。我想將一個檔案夾拖放到 .bat 腳本上以執行該腳本。
問題是許多檔案夾名稱都會在檔案夾名稱中帶有“^”符號,當我將這些檔案夾拖放到 .bat 腳本上時,腳本中的“%1”具有檔案夾名稱,但是'^' 字符由于某種原因被洗掉。
有沒有辦法在不丟失 '^' 字符的情況下獲取文字檔案夾名稱?
我正在通過示例添加更多資訊。我的 .bat 檔案是這樣的:
@echo off
echo %~1
mkdir USB
xcopy /s radiantUSB USB
move "%~1" USB\
echo "FINISHED"
@pause
我放在 .bat 檔案上的檔案夾的名稱是:
Duck^Donald^Quack
它正在提取的路徑是:
C:\Users\sscotti\Desktop\DuckDonaldQuack
'^' 被洗掉,移動“%~1”USB\ 失敗,因為要移動的檔案夾的路徑不正確。
uj5u.com熱心網友回復:
如果未參考,則無法^使用%1nor獲取單個插入符號。%*那是因為,cmd.exe使用插入符號作為轉義字符并將其從引數中洗掉。
但在隱藏變數中,cmdcmdline所有字符都存在。
這適用于幾乎所有特殊字符。
用Donald^Duck, Dagobert ^Duck,測驗Cat&Dog
它只對像Cat&dog(). 為了防彈,您需要一個額外的AutoRun 批處理檔案,它修復了拖放處理。
@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
uj5u.com熱心網友回復:
使用%~1,不使用%1。
處理批處理中字符的特殊含義是另一個問題。由于您沒有向我們展示您的批次,一根繩子有多長?
這是我的測驗批次
@ECHO OFF
ECHO ----%~nx0--%*
SETLOCAL
ECHO "%1"
ECHO "%~1"
ECHO "%*"
pause
GOTO :EOF
測驗檔案名是
U:\Test space^caret&ersand!exclam%percent.bat
這是結果
----qcifn.bat--"U:\Test space^caret&ersand!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&ersand!exclam%percent.bat"
""U:\Test spacecaret
'ampersand!exclam%percent.bat""' is not recognized as an internal or external command,
operable program or batch file.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/520300.html
標籤:视窗批处理文件
