batch-file - 处理;(分号)在文件名中使用这个拖放例程
问题描述
考虑这些文件名。
; 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
,它;
也用作分隔符,但这可以解决(这个技巧可以在这里找到)
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" )
编辑:(更多示例)另一个可以将删除的项目扩展到此链接中的任何项目的示例:Extended Filename syntax to %* with the following workaround。这也以一种半心半意的方式起作用。
setlocal enabledelayedexpansion
for %%A in (%*) do ( call :FIX "%%~A" )
pause
:FIX
SET _params=!%*!
CALL :sub "%_params%"
GOTO :eof
:: Now display just the filename and extension (not path)
:sub
ECHO "%~nx1"
:: All of these returns are when each file (one at a time) is dragged to the batch file.
rem folder name , goes = here... works returns "folder name , goes = here..."
rem ; f - copy.txt works returns "; f - copy.txt"
rem ;f.txt doesn't returns "." ""
rem a.txt works return "a.txt"
rem b.txt works return "b.txt"
rem c.txt works return "c.txt"
rem cool&stuff.png doesn't returns "Cool""
rem copy = copy.txt works returns "copy = copy.txt"
rem f - copy - copy.txt works returns "f - copy - copy.txt"
rem f - copy ,- copy - copy.txt works returns "f - copy ,- copy - copy.txt"
rem f - copy ;- copy.txt works returns "f - copy ;- copy.txt"
rem f - copy.txt works returns "f - copy.txt "
rem f - copy1.txt works returns "f - copy1.txt"
rem FILENAME WITH & IN IT!! - COPY.TXT doesn't returns "FILENAME WITH & IN IT - COPY.TXT""
rem FILENAME WITH & IN IT!!.TXT doesn't returns "FILENAME WITH & IN IT.TXT""
解决方案
我在这里找到了一个解决此问题的线程,据我所知,它适用于任何特殊字符。
非常感谢那个论坛上的用户aGerman ...所有的功劳都归功于他。
链接到该线程:
解决了如何通过拖放来转义 InputFile 上的特殊字符?
其他有用的链接:
'Pretty print' windows %PATH% 变量 - 如何在 ';' 上拆分 在 CMD 外壳中
@echo off &setlocal DisableDelayedExpansion
setlocal EnableDelayedExpansion
set "params=!cmdcmdline:~,-1!"
set "params=!params:*" =!"
if "!params!"=="" exit
endlocal&set "params=%params:"=""%"
set "params=%params:^=^^%"
set "params=%params:&=^&%"
set "params=%params: =^ ^ %"
set params=%params:""="%
set "params=%params:"=""Q%"
set "params=%params: ="S"S%"
set "params=%params:^ ^ = %"
set "params=%params:""="%"
setlocal EnableDelayedExpansion
set "params=!params:"Q=!"
for %%i in ("!params:"S"S=" "!") do (
if "!!"=="" endlocal
REM only files ...
if not exist "%%~i\" (
set "file_folder=%%~i"
call :proc
)
)
echo ready.
pause
exit
:proc
echo process "%file_folder%" here ...
goto :eof