Single input variable for windows send to .bat - windows

I want to get a framemd5 via ffmpeg by right clicking a file, selecting "send to", then selecting my.bat file.
Using this allows me to make framemd5s for every file in a directory:
for %%a in ("*.*") do C:\Users\bla\Downloads\avanti-092\Avanti-ffmpeg-GUI-092\ffmpeg\ffmpeg-20150513-git-51f6455-win64-static\ffmpeg-20150513-git-51f6455-win64-static\bin\ffmpeg.exe -i "%%a" -f framemd5 "%%~na.framemd5"pause
I want to just run the bat file on a single file, the one I right clicked on to begin with. I do not know which variable to use.

A Send-To command (the batch file in your case) receives the selected file(s) as command line argument(s), which can be accessed in a batch file with %1, %2, %3 and so on.
%0 is the (path to the) batch file itself. Type call /? to get more information.
For applying that to a single file use %1. This syntax supports ~ modifiers like for variables. For instance, %~1 removes surrounding double-quotes, %~n1 extracts the file name only.
For your batch file, you actually don't need the for command (which executed one iteration only).
The following should therefore work:
C:\Users\bla\Downloads\avanti-092\Avanti-ffmpeg-GUI-092\ffmpeg\ffmpeg-20150513-git-51f6455-win64-static\ffmpeg-20150513-git-51f6455-win64-static\bin\ffmpeg.exe -i "%~1" -f framemd5 "%~n1.framemd5" & pause

Related

Trying to batch merge 2 .jpeg's horizontally and put them in a different folder after

this is for my doctoral thesis in medicine. So please excuse my noobishnis in programing.
I have a bunch (about 4000 files) of scans from patients. There is a front and a back .jpg for each patient. And there where multiple patients each day.
The folder structure looks like this:
\images
\2017-08-21
\pa_102165.jpg
\pa_10216500001.jpg
\2017-06-14
\pa_101545.jpg
\pa_10154500001.jpg
\pa_104761.jpg
\pa_10476100001.jpg
\pa_107514.jpg
\pa_10751400001.jpg
\2017-03-73
\pa_109631.jpg
\pa_10963100001.jpg
\pa_108624.jpg
\pa_10862400001.jpg
Where in the first example 2017-08-21 is the date the patient came in, pa_102165.jpg is the front and pa_10216500001.jpg is the back. So the front is always pa_10XXXX.jpg and the back is pa_10XXXX00001.jpg. I had no hand in the nameing scheme.
My goal is to make a batchscript that merges the 2 corresponding .jpgs of each patient horizontally and automatically puts them in a different folder, so that I don't have to do it manually with something like MS Paint.
For example like this:
\images
\merged
\2017-08-21
\pa_102165_merged.jpg
\2017-06-14
\pa_101545_merged.jpg
\pa_104761_merged.jpg
\pa_107514_merged.jpg
\2017-03-73
\pa_109631_merged.jpg
\pa_108624_merged.jpg
I'm working on Windows 10 and found two promising methods so far but fail to comprehend how to make this into a batch file or something like it.
IrfanView Thumbnails
1. Mark the 2 corresponding .jpgs
2. File>Create contact sheet from selected files...
3. Create
4. File>Save as... in destination folder which i have to create for every day
which is faster than merging them by hand but would consume multiple workdays to do for all the pairs
and...
ImageMagic in Windows cmd
C:\Users\me\doctor\Images\test\images\2016-03-31>convert pa_102165.jpg pa_10216500001.jpg +append pa_102165_merged.jpg
This produces the merged .jpeg in the same folder the input images are in. This looks more promising but I fail to grasp how I could automate this process given the nameing scheme and the folder structure.
Thanks for taking the time to read this! I'm happy for every input you have!
This should get you fairly close. Essentially it is using the power of the FOR command modifiers to extract the base file name and file extension. The FOR /F command is capturing the output of the DIR command that is piped to the FINDSTR command. We are doing that so we only grab files with the file mask of pa_######.jpg
Once we have that we use the command modifiers with the IF command to make sure the 00001 file exists. If it does exist then it will execute the convert command. For the sake of making sure the code is performing correctly I am just ECHOING the output to the screen. If the output on the screen looks correct then remove the ECHO so that the CONVERT command executes.
#echo off
CD /D "C:\Users\me\doctor\Images\test\images"
FOR /F "delims=" %%G IN ('DIR /A-D /B /S PA_*.jpg ^|findstr /RIC:"pa_[0-9][0-9][0-9][0-9][0-9][0-9]\.jpg$"') DO (
IF EXIST "%%~dpnG00001%%~xG" (
ECHO convert "%%G" "%%~dpnG00001%%~xG" +append "%%~dpnG_merged%%~xG"
)
)
This task could be done with IrfanView with the following batch file stored in the directory containing the folder images.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "IrfanView=%ProgramFiles(x86)%\IrfanView\i_view32.exe"
set "SourcePath=%~dp0images"
set "TargetPath=%~dp0merged"
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe /R "%SourcePath%" pa_10????.jpg 2^>nul') do for %%J in ("%%~dpI.") do (
if not exist "%TargetPath%\%%~nxJ\%%~nI_merged%%~xI" if exist "%%~dpnI00001%%~xI" (
if not exist "%TargetPath%\%%~nxJ\" md "%TargetPath%\%%~nxJ"
if exist "%TargetPath%\%%~nxJ\" (
echo Merging "%%~nxJ\%%~nxI" and "%%~nxJ\%%~nI00001%%~xI" ...
"%IrfanView%" /convert="%TargetPath%\%%~nxJ\%%~nI_merged%%~xI" /jpgq=95 /panorama=(1,"%%I","%%~dpnI00001%%~xI"^)
)
)
)
endlocal
There must be customized the fully qualified file name of IrfanView in the third line. There can be modified also the percent value of option /jpgq which defines the quality of the output JPEG file.
The command WHERE searches recursive in subdirectory images of the directory containing the batch file for files matching the wildcard pattern pa_10????.jpg with ignoring all other files. The found file names are output with full path and this list of file names is captured by FOR and processed line by line after WHERE finished. WHERE is executed in this case by one more cmd.exe started in background with option /c and the command line within ' as additional arguments and not by cmd.exe processing the batch file.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded where command line with using a separate command process started in background.
Each image file with full name (drive + path + name + extension) is assigned one after the other to the loop variable I. For each file name one more FOR loop is used which processes just the full path to the current image file to assign this path with a dot appended to loop variable J. The dot at end means current directory, i.e. the directory containing current image file to process.
There is next checked with the first IF condition if for that image file does not exist already a matching pa_10????_merged.jpg file in which case there is nothing to do for the current image file. That means the batch file can be executed on same folder as often as wanted because of it runs IrfanView only for the source JPEG files for which the appropriate target JPEG file does not exist already.
The second IF condition checks if the back image exists also in the directory of current front image as otherwise nothing can be merged at all.
There is next checked with the third IF condition if the target directory exists already and this directory is created if that is not the case.
The last IF condition checks once again the existence of the target directory and if that exists now as expected, IrfanView is called with the appropriate options to create the merged image file in the target directory with the appropriate file name.
The closing round bracket ) on IrfanView command line must be escaped with ^ to be interpreted literally by cmd.exe to pass this closing parenthesis to IrfanView instead of interpreting it as end of one of the command blocks opened with ( above.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file path always ending with a backslash
echo /?
endlocal /?
for /?
if /?
md /?
setlocal /?
where /?
Double click on the text file i_options.txt in program files folder of IrfanView for the description of the IrfanView options as used in the batch file.

Windows batch file with loop through subfolders

I didnt succeed writing an approriate batch file and would appreciate some help.
Let's say I have an exe in D:\Test\ called script.exe.
To execute this script.exe it requires an additional argument of a .bin files (e.g. bin01.bin, bin02.bin).
Therefore it would be like: script.exe -i bin01.bin, script.exe -i bin01
I want the script to execute with all .bin files from all subfolders
D:\Test\script.exe
D:\Test\Folder01\bin01.bin
D:\Test\Folder01\bin02.bin
D:\Test\Folder02\bin01.bin
D:\Test\Folder03\bin01.bin
anyone could help me here?
Thanks a lot in advance.
For direct execution from within a command prompt window:
for /R "D:\Test" %I in (*.bin) do #D:\Test\script.exe -i "%I"
And the same command line for usage in a batch file:
#for /R "D:\Test" %%I in (*.bin) do #D:\Test\script.exe -i "%%I"
The command FOR searches recursive in directory D:\Test and all subdirectories for files matching the wildcard pattern *.bin.
The name of each found file is assigned with full path to case-sensitive loop variable I.
FOR executes for each file the executable D:\Test\script.exe with first argument -i and second argument being the name of found file with full path enclosed in double quotes to work for any *.bin file name even those containing a space or one of these characters: &()[]{}^=;!'+,`~.
# at beginning of entire FOR command line just tells Windows command interpreter cmd.exe not to echo the command line after preprocessing before execution to console window as by default.
# at beginning of D:\Test\script.exe avoids the output of this command to console executed on each iteration of the loop.
Most often the echo of a command line before execution is turned off at beginning of the batch file with #echo off as it can be seen below.
#echo off
for /R "D:\Test" %%I in (*.bin) do D:\Test\script.exe -i "%%I"
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?

Mass Renaming (Adding Suffix/Prefix) of files recursively in Windows

I have a folder with many sub-folders which have further sub-folders. each folder has a number of files.
I want to rename all the file by adding some suffix to them
for ex:
Original: FileName1.ext
Final : Suf_FileName1.ext
To carry out this function, found this command online
`FOR /R %x IN (*) DO ren "%x" Suf_*`
but this replaces the initial characters in the original file name
like
Original: FileName1.ext
Final : Suf_Name.ext
(please note it has removed initial characters in the initial file name )
Please suggest changes/modifications in the above command,
or another command to carry out the function.
Thanks in Advance.
When you use a for command to iterate over a set of files, the for replaceable parameter (%x in your sample) contains a reference to the file.
When you use %x in the commands contained in the do clause, this parameter is replaced by information of the file being processed, by default the name and extension of the file (when using a simple for) or the file with full path (when using a recursive for /R), but you can decide and changee what information you want to use. The replaceable parameter allows the usage of some modifiers (you can see the full list if you execute for /?)
In your case, for your rename mask you need the name and extension of the file being referenced (I've changed your %x with %F so the syntax is clearer)
%~nxF
^^^^.... replaceable parameter
||+..... extension of the file referenced
|+...... name of the file referenced
+....... operator for modifier usage
Your command could be something like
for /R %F in (*) do echo ren "%F" "Suf_%~nxF"
note: The echo in the command is just a debugging habit. Before changing anything, first show to console the commands that will be executed. If the output seems correct then we only have to remove the echo and run the command again.
Remember that if you want to use the command inside a batch file, you need to escape the percent signs in the for replaceable parameters by doubling them. Inside a batch file the previous command will be
for /R %%F in (*) do echo ren "%%F" "Suf_%%~nxF"

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

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!
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.
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

Trouble with renaming folders and sub folders using Batch

So I'm trying to set up a template file structure for projects that can be modified in name to suit each project. I have created the example directory containing example folders i.e. "Template project" contains "template hardware" , "template software" etc. , and have a simple batch program that copies the "template project" folder and all contained subfolders, however I would like to change the word 'template' with what ever I choose to call the project. I was wondering if this is possible to do? Ideally I could just edit the batch file with the name of the project and then run it to copy the template and rename it.
Any help is greatly appreciated!
To start learning type help at the command prompt. Then anything on that list add /? for more help.
Set /p NewName=Enter project name
md "c:\somewhere\%newname%project\%newname% software
md "c:\somewhere\%newname%project\%newname% hardware
or use xcopy (and use/l to have it do a test without copying)
xcopy "c:\tempate" "d:\%newname%" /e /h /q /i /c
See set /?, md /?, and xcopy /?. Type just set to see a list of variables.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
\\ (\\servername\sharename\folder\file.ext) access files and folders via UNC naming.
: (win.ini:streamname) accesses an alternative steam. Also separates drive from rest of path.
. (win.ini) the LAST dot in a file path seperates the name from extension
. (dir .\*.txt) the current directory
.. (cd ..) the parent directory
\\?\ (\\?\c:\windows\win.ini) When a file path is prefixed with \\?\ filename checks are turned off.
< > : " / \ | Reserved characters. May not be used in filenames.
Reserved names. These refer to devices eg,
copy filename con
which copies a file to the console window.
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4,
COM5, COM6, COM7, COM8, COM9, LPT1, LPT2,
LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
CONIN$, CONOUT$, CONERR$
Maximum path length 260 characters
Maximum path length (\\?\) 32,767 characters (approx - some rare characters use 2 characters of storage)
Maximum filename length 255 characters
Starting a Program
===============
See start /? and call /? for help on all three ways.
Specify a program name
--------------------------------
c:\windows\notepad.exe
In a batch file the batch will wait for the program to exit. When
typed the command prompt does not wait for graphical
programs to exit.
If the program is a batch file control is transferred and the rest of the calling batch file is not executed.
Use Start command
--------------------------
start "" c:\windows\notepad.exe
Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.
Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog). Try
start shell:cache
Use Call command
-------------------------
Call is used to start batch files and wait for them to exit and continue the current batch file.
.
--

Resources