创建可以处理多个文件拖放的批处理文件

Creating a Batch File that can Process a Drag and Drop of Multiple Files

我正在尝试通过批处理文件 运行 处理多个文件。我希望批处理文件能够获取所有给定的文件(也称为转储;或拖放)并处理它们。

目前我可以使用以下批处理命令单独处理文件:

"C:\Program Files\Wireshark\tshark.exe" -r %1 -Y "filter" -o "uat:user_dlts:\"User 8 (DLT=155)\",\"pxt\",\"0\",\"\",\"0\",\"\"" -o "gui.column.format:\"Info\",\"%%i\""> %1".filter.txt"

我希望做与上述相同的事情,但能够简单地将文件拖放到要处理的批处理文件中。

对于那些对上述批处理文件感到困惑的人:
-r 读取输入文件,其完整文件地址(包括扩展名)被%1捕获
-Y 过滤掉拖放文件的某些部分
-o 为 运行 可执行文件设置首选项(由“”中的内容定义):tshark.exe
-> 将结果重定向到标准输出
- %1".filter.txt" 将结果输出到名为 "draggedfilename.filter.txt"

的新文件中

请不要在其他任何地方使用此代码,除非帮助我使用此代码(由于它被用于应用程序)。出于隐私考虑,我在此版本的代码中更改了几个标志。 如果您有任何问题,请告诉我!

使用 %* 而不是 %1

示例:

@echo off 

for %%a in (%*) do  (
"C:\Program Files\Wireshark\tshark.exe" -r "%%a" -Y "filter" -o "uat:user_dlts:\"User 8 (DLT=155)\",\"pxt\",\"0\",\"\",\"0\",\"\"" -o "gui.column.format:\"Info\",\"%%i\""> "%%a"".filter.txt"
)

用正确的变量替换 %%i

您可以像这样使用 gotoshift 进行循环(有关详细信息,请参阅 rem 评论):

:LOOP
rem check first argument whether it is empty and quit loop in case;
rem `%1` is the argument as is; `%~1` removes surrounding quotes;
rem `"%~1"` therefore ensures that the argument is always enclosed within quotes:
if "%~1"=="" goto :END
rem the argument is passed over to the command to execute (`"%~1"`):
"C:\Program Files\Wireshark\tshark.exe" -r "%~1" -Y "filter" -o "uat:user_dlts:\"User 8 (DLT=155)\",\"pxt\",\"0\",\"\",\"0\",\"\"" -o "gui.column.format:\"Info\",\"%%i\""> "%~1.filter.txt"
rem `shift` makes the second argument (`%2`) to be the first (`%1`), the third (`%3`) to be the second (`%2`),...:
shift
rem go back to top:
goto :LOOP
:END