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

删除回忆录丶 提交于 2019-12-02 01:18:12

问题


I am trying to process several files by running them through a batch file. I want the batch file to be able to take all the files its given (aka dumped; or dragged and dropped) and process them.

Currently I can process the files individually with the following batch command:

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

I am looking to do the same thing as above, but with the ability to simply drag and drop the files onto the batch file to be processed.

For those confused about the above batch file:
-r Reads the input file, whose full file address (including extension) is captured by %1
-Y Filters out certain parts of the dragged & dropped file
-o Sets preferences (defined by stuff in the ""s) for running the executable: tshark.exe
- > redirects the results to stdout
- %1".filter.txt" outputs the results to a new file called "draggedfilename.filter.txt"

Please refrain from using this code anywhere else except helping me with this code (due to the application it is being used for). I changed several flags in this version of the code for privacy sake. Let me know if you have any questions!


回答1:


You could go for a loop using goto and shift like this (see rem comments for details):

: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



回答2:


Use %* instead of %1.

Example :

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

Replace %%i with the right variable.



来源:https://stackoverflow.com/questions/33881845/creating-a-batch-file-that-can-process-a-drag-and-drop-of-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!