考虑这些文件名。
; f - Copy.txt
;f.txt
f - Copy ;- Copy.txt
f - Copy.txt
我有这段代码可以用以下答案解析任何文件中的&
符号:Drag and drop batch file for multiple files?
@echo off
title %~nx0
setlocal enabledelayedexpansion
rem Take the cmd-line, remove all until the first parameter
set "params=!cmdcmdline:~0,-1!"
set "params=!params:*" =!"
set count=0
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
set /a count+=1
set "item_!count!=%%~G"
rem echo !count! %%~G
)
for /l %%c in (1,1,!count!) DO (
for /f "delims=;" %%A in ("!item_%%c!") do (
echo path: %%~dpA
echo file: %%~A
)
)
pause
rem ** The exit is important, so the cmd.exe doesn't try to execute commands after ampersands
exit
如果将文件拖到批处理文件中,它将使用;
作为分隔符,从而导致出现不良结果。
要使用for /f
解决此问题,您可以在下面执行以下操作,但是我不知道如何将此修复程序合并到上面的拖放代码中。
for /f
出现问题,它也使用;
作为定界符,但是可以通过以下方式解决(此技巧可以是found here)for /f tokens^=*^ delims^=^ eol^=^¬ %%A in ('dir "%cd%" /a-d /b') do ( echo %%~A )
将导致:
; f - Copy.txt
;f.txt
f - Copy ;- Copy.txt
f - Copy.txt
相对于:for /f "tokens=* delims=" %%A in ('dir "%cd%" /a-d /b') do ( echo %%~A )
将导致:
f - Copy ;- Copy.txt
f - Copy.txt
如何使用拖放代码解决此问题?
编辑:我对此有点了解。
@echo off
setlocal enabledelayedexpansion
for /f tokens^=*^ delims^=^ ^ eol^=^¬ %%A in (""!%*!"") do ( echo "%%~fA" )
Edit:另一个示例可以将放置的项目扩展到此链接上的任何项目:Extended Filename syntax to %* with the following workaround。这也可以三心二意。
for %%A in (%*) do ( call :FIX "%%~A" )
pause
:FIX
SET _params=!%*!
CALL :sub "%_params%"
GOTO :eof
:sub
:: Now display just the filename (not path)
ECHO "%~n1"
rem ; f - Copy.txt works
rem ;f.txt doesn't
rem Cool&stuff.png returns "Cool""
rem f - Copy ;- Copy.txt works
rem f - Copy.txt works
rem f - Copy1.txt works
rem filename with & in it!!.txt works
[如jeb所述,主要问题来自文件;f.txt
之类的文件没有用双引号引起来。考虑到这一点,只要您不希望拖放too