"点滴QUOT;批处理脚本 - 包含&符号的文件名 [英] "Droplet" batch script - filenames containing ampersands

查看:350
本文介绍了"点滴QUOT;批处理脚本 - 包含&符号的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以有其他的文件拖放到它的批处理文件。具体来说,我使用的ffmpeg 编辑由手持录音机制作音频文件。使用带&符号的文件名时的问题是(安培)。引用后,与放大器的输入端,任何即使;是下车,但只有当文件被删除到它;如果文件名输入命令行中键入时,脚本工作正常。在 CMD 窗口关闭之前,我简要看到一个错误的文件名的其余部分称它不会被识别为一个有效的命令。

I'm trying to create a batch file that can have other files dropped onto it. Specifically, I'm using ffmpeg to edit audio files produced by a handheld voice recorder. The problem is when using filenames with ampersands (&). Even when quoting the input, anything after the & is dropped off, but only when files are dropped onto it; if the filename input is typed on the command line, the script works fine. Before the cmd window closes, I briefly see the rest of the filename with an error saying it is not recognized as a valid command.

下面是我的脚本:


rem Change to drive and directory of input file
%~d1
cd %~p1

rem ffmpeg: mix to one channel, double the volume
%HOMEDRIVE%%HOMEPATH%\ffmpeg.exe -i "%~nx1" -ac 1 -vol 1024 "%~n1 fixed%~x1"

pause

下面是在命令行上出现的内容,下探后CH17&安培; 18.mp3

Here's what appears on the command line, after dropping "ch17&18.mp3":


C:\Users\computergeeksjw\Desktop>C:\Users\computergeeksjw\ffmpeg.exe -i "ch17" -ac 1 -vol 1024 "ch17 fixed"
[...]
ch17: No such file or directory

在的情况下它的事项:我使用Windows 8开发preVIEW。难道这导致我的问题呢?确实发生在Windows 7同样的错误或更早?

In case it matters: I'm using the Windows 8 Developer Preview. Is this causing my problem? Does the same error occur on Windows 7 or earlier?

推荐答案

有是Windows拖一个长期存在的BUG和拖放功能就包含文件路径&安培; ^ 但不包含<空方式&g​​t;

There is a long-standing bug in Windows drag and drop functionality regarding file paths that contain & or ^ but don't contain a <space>.

如果文件路径至少包含一个&lt;空&GT; ,那么Windows会自动使其得到正确解析包围在引号的路径。 Windows应做同样的事情,如果文件路径中包含&放大器; ^ ,但它没有。

If a file path contains at least one <space>, then Windows automatically encloses the path in quotes so that it gets parsed properly. Windows should do the same thing if the file path contains & or ^, but it does not.

如果您创建下面这个简单的批处理文件,并拖动文件到它,你可以看到这个问题。

If you create the following simple batch file and drag files onto it, you can see the problem.

@echo off
setlocal enableDelayedExpansion
echo cmd=!cmdcmdline!
echo %%1="%~1"
pause
exit

的!CMDCMDLINE!变量包含推出的批处理文件的实际命令。
批处理文件打印出命令行和第一个参数。

The !cmdcmdline! variable contains the actual command that launched the batch file. The batch file prints out the command line and the first parameter.

如果你拖放一个名为a.txt中你得到的文件

If you drag and drop a file named "a.txt" you get

cmd=cmd /c ""C:\test\drag.bat" C:\test\a.txt"
%1=C:\test\a.txt
Press any key to continue . . .

如果你忽略周围的整个命令,你看到周围有文件参数没有引号引号。没有特殊的字符,所以没有问题。

If you disregard the quotes around the entire command you see that there are no quotes around the file argument. There are no special characters, so there is no problem.

现在拖放一个B.txt,并且你获得

Now drag and drop "a b.txt" and you get

cmd=cmd /c ""C:\test\drag.bat" "C:\test\a b.txt""
%1="C:\test\a b.txt"
Press any key to continue . . .

您可以看到的Windows如何检测在名称空间,并且包围在引号中的文件。再次没有问题

You can see how Windows detects the space in the name and encloses the file in quotes. Again there is no problem.

现在拖放A和B.txt,并且你获得

Now drag and drop "a&b.txt" and you get

cmd=cmd /c ""C:\test\drag.bat" C:\test\a&b.txt"
%1=C:\test\a
Press any key to continue . . .

Windows无法找到名称中有空格,因此它不会用引号括起来。大问题!视窗通过C:\\测试\\一个批处理文件和对待b.txt作为第二个文件的批处理文件完成后执行。在批处理文件$ P $硬盘EXIT命令从批后执行pvents任何分裂文件名。当然b.txt永远无法执行。但是,如果该文件被命名为A和b.bat和b.bat存在,那么这可能是麻烦,如果硬EXIT在批处理文件中并没有

Windows doesn't find a space in the name, so it does not enclose it in quotes. Big problem! Windows passes "C:\test\a" to the batch file and treats "b.txt" as a second file to be executed after the batch file completes. The hard EXIT command in the batch file prevents any split filenames from executing after the batch. Of course b.txt could never execute. But if the file were named "a&b.bat" and "b.bat" existed, then that could be trouble if the hard EXIT were not in the batch file.

有可能拖动多个文件到一个批处理文件,每一个应作为参数传递。

It is possible to drag multiple files onto a batch file, and each one should be passed as a parameter.

的!CMDCMDLINE!是能够可靠地访问拖放参数的唯一途径。但是,如果文件是在一个正常的调用批处理文件正常参数传递,将无法正常工作。

The !cmdcmdline! is the only way to reliably access drag and drop arguments. But that will not work if files are passed as normal arguments in a normal call to the batch file.

下面是可以使用拖动检测是否它被称为拖放与正常呼叫的批处理文件。 (这不是防弹的,但我认为它应该在大多数情况),它会处理每个文件参数,一次一个,无论呼叫的类型。 (这个过程只是回声文件名,但您可以替换你想要的任何处理。)如果该批次使用拖放然后它会做一个严出,以防止分裂的文件名叫做

Below is a batch file that can detect if it was called using drag and drop versus a normal call. (It is not bullet proof, but I think it should work in most situations) It will process each file argument, one at a time, regardless of the type of call. (The process simply echos the file name, but you can substitute whatever processing you want.) If the batch was called using drag and drop then it will do a hard exit to protect against split file names.

@echo off
setlocal disableDelayedExpansion
::
:: first assume normal call, get args from %*
set args=%*
set "dragDrop="
::
:: Now check if drag&drop situation by looking for %0 in !cmdcmdline!
:: if found then set drag&drop flag and get args from !cmdcmdline!
setlocal enableDelayedExpansion
set "cmd=!cmdcmdline!"
set "cmd2=!cmd:*%~f0=!"
if "!cmd2!" neq "!cmd!" (
  set dragDrop=1
  set "args=!cmd2:~0,-1! "
  set "args=!args:* =!"
)
::
:: Process the args
for %%F in (!args!) do (
  if "!!"=="" endlocal & set "dragDrop=%dragDrop%"
  rem ------------------------------------------------
  rem - Your file processing starts here.
  rem - Each file will be processed one at a time
  rem - The file path will be in %%F
  rem -
  echo Process file "%%~F"
  rem -
  rem - Your file processing ends here
  rem -------------------------------------------------
)
::
:: If drag&drop then must do a hard exit to prevent unwanted execution
:: of any split drag&drop filename argument
if defined dragDrop (
  pause
  exit
)

它看起来像你的现有批次的设计只处理一个文件。如果您需要进行修改来电来支持多个文件,我不能告诉。我修改了上面的批处理只处理第一个参数,并取代你的过程中进入参数处理循环。这是未经测试,但我认为它应该为你工作。

It looks like your existing batch is only designed to handle one file. I can't tell if you need to make modifications to the calls to support multiple files. I modified the above batch to only process the first argument, and substituted your process into the argument processing loop. This is untested, but I think it should work for you.

@echo off
setlocal disableDelayedExpansion
::
:: first assume normal call, get args from %*
set args=%*
set "dragDrop="
::
:: Now check if drag&drop situation by looking for %0 in !cmdcmdline!
:: if found then set drag&drop flag and get args from !cmdcmdline!
setlocal enableDelayedExpansion
set "cmd=!cmdcmdline!"
set "cmd2=!cmd:*%~f0=!"
if "!cmd2!" neq "!cmd!" (
  set dragDrop=1
  set "args=!cmd2:~0,-1! "
  set "args=!args:* =!"
)
::
:: Process the first argument only
for %%F in (!args!) do (
  if "!!"=="" endlocal & set "dragDrop=%dragDrop%"
  rem ------------------------------------------------
  rem - Your file processing starts here.
  rem - Use %%F wherever you would normally use %1
  rem
  rem Change to drive and directory of input file
  %%~dF
  cd %%~pF
  rem ffmpeg: mix to one channel, double the volume
  %HOMEDRIVE%%HOMEPATH%\ffmpeg.exe -i "%%~nxF" -ac 1 -vol 1024 "%%~nF fixed%%~xF"
  rem
  rem - Your file processing ends here
  rem -------------------------------------------------
  goto :continue
)
:continue
if defined dragDrop (
  pause
  exit
)

这篇关于&QUOT;点滴QUOT;批处理脚本 - 包含&符号的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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