处理;此拖放例程将文件名中的(分号) [英] Handling ; (semi-colon) in filenames with this drag and drop routine

查看:46
本文介绍了处理;此拖放例程将文件名中的(分号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这些文件名.

; f - Copy.txt
;f.txt
f - Copy ;- Copy.txt
f - Copy.txt

我有此代码可以解析任何文件中的& 符号,并具有以下答案:

I've got this code which resolves the & symbol in any files with this answer: 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

如果我将文件拖到批处理文件中,它将使用; 作为分隔符,从而导致不良结果.

If I drag the files to the batch file it uses ; as the delimiter resulting in undesired results.

要使用/f 的解决此问题,您可以在下面执行以下操作,但是我对如何将该修补程序合并到上面的拖放代码中感到困惑.

To get around this with the for /f you can do the following below but I'm at a loss as to how to incorporate that fix into the drag and drop code above.

/f 出现问题,即它也使用; 作为分隔符,但这可以通过解决(此技巧可以是

There's an issue with the for /f whereby it uses ; as the delimiter also but this can be resolved with (This trick can be 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

相对于:对于/f"tokens = * delims ="%dirA in('dir%cd%"/a-d/b')做(echo %%〜A)

这将导致:

f - Copy ;- Copy.txt
f - Copy.txt

如何使用拖放代码解决此问题?

How can I fix this issue with the drag and drop code?

我对此已经很了解了.

I'm getting kinda close with this.

@echo off
setlocal enabledelayedexpansion
for /f tokens^=*^ delims^=^ ^ eol^=^¬ %%A in (""!%*!"") do ( echo "%%~fA" )

(更多示例)另一个示例可以将放置的项目扩展到此链接中的任何项目:

(more examples) Another example that can extend the dropped items to any those at this link: Extended Filename syntax to %* with the following workaround. This also works in a half-hearted sort of way.

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

推荐答案

我在这里找到了一个可解决此问题的线程,据我所知,它可以与任何特殊字符一起使用.

I've found a thread here that addresses this issue and as far as I can see, works with any special characters.

非常感谢该论坛上的用户 aGerman ... 所有荣誉归功于他.

Big thanks to user aGerman on that forum... All credit goes to him.

链接到该线程:

已解决通过拖放操作在InputFile上转义特殊字符?

其他有用的链接:

对x个拖动的文件执行操作的最简单循环?

'漂亮的'windows%PATH%变量打印-如何分割';'在CMD shell中

拖放多个文件的批处理文件?

@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

这篇关于处理;此拖放例程将文件名中的(分号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆