3

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!

3
  • Don't do this. Drag&drop isn't implemented correctly in explorer: the command line arguments are broken on special characters (like &) that are valid in quoted file names (explorer misses to quote them). Better invest your time in a solution with [vbscript] that is mostly available where .bat and .cmd is...
    – Wolf
    Feb 7, 2017 at 11:33
  • Possible duplicate of Batch file Copy using %1 for drag and drop
    – phuclv
    Aug 16, 2018 at 11:24
  • I highly recommend this answer from jeb stackoverflow.com/a/5192427/4172159 It works perfectly for files with & and ! characters in them.
    – JG1212
    Mar 16, 2021 at 16:13

2 Answers 2

7

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.

3
  • 4
    Just a few notes. Any files you drop onto the batch file will be quoted if they have spaces in the path. Also if you drop enough files onto the batch file that have really long paths you can exceed the line limit. It is 4096 in XP and 8192 in Windows 7. Also, any file names that have an ampersand in them without a space will not process correctly. Another option to process all the arguments is too use SHIFT and then check if %1 is not empty and loop back to a label. Here is a link to Jeb's take on how to do this a different way. stackoverflow.com/a/5370380/1417694
    – Squashman
    Nov 24, 2015 at 1:47
  • Didn't work in my system. Regarding %%i, that defines a variable within tshark that is used to filter out certain portions of the file I am dumping. %%i has no effect on the batch file since its in double quotes.
    – Midimistro
    Nov 24, 2015 at 19:29
  • 1
    if you will use set inside loop, then don't forget !variable_name! instead of %variable_name% and use delayed expansion.
    – T.Todua
    Sep 14, 2018 at 8:15
5

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
4
  • This method worked and the %%a & %* didn't. Thanks for the help. The comments also helped and clarified what I needed and what was happening. I will probably include better versions of the rems in my version for programming reference later if we need to make more batch files like this.
    – Midimistro
    Nov 24, 2015 at 19:26
  • This answer does not work on any file names that contain a & ampersand and no spaces. For example This&that.txt Does anyone have any solution for this?
    – JG1212
    Mar 13, 2021 at 3:45
  • @JG1212, if you are typing such arguments in Command Prompt, use quotation ("This&that.txt"); if you are dragging-and-dropping (so there are no quotes since there is no space), follow Squashman's comment
    – aschipfl
    Mar 13, 2021 at 9:23
  • @aschipfl thanks. I ended up using a revised answer from jeb here: stackoverflow.com/a/5192427/4172159 It works perfectly for both & and ! in file names.
    – JG1212
    Mar 16, 2021 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.