gpt4 book ai didi

batch-file - 处理;使用此拖放例程的文件名中的(分号)

转载 作者:行者123 更新时间:2023-12-04 07:46:35 25 4
gpt4 key购买 nike

考虑这些文件名。

; 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" )

编辑:(更多示例) 另一个可以将掉落元素扩展到此链接中的任何元素的示例: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...所有功劳都归于他。

该主题的链接:

Solved How to escape special characters on InputFile with Drag and Drop ?

其他有用的链接:

Simplest loop to perform actions on x dragged files?

'Pretty print' windows %PATH% variable - how to split on ';' in CMD shell

Drag and drop batch file for multiple files?

@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

关于batch-file - 处理;使用此拖放例程的文件名中的(分号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59930635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com