Batch Files Pull Variables from Text Document - variables

I'm in the process of remuxing a bunch of videos I have. Once I have them remuxed, they all have different audio offsets that I need to implement. I have a batch file that will go through every file in the folder and do one offset. However, I would like to have it go into a text document (Offsets.txt) and pull info from it.
This is my "Offsets.txt"
Bio Broly.mkv: +1.112450
Bojack Unbound.mkv: +1.034330
Broly Second Coming.mkv: -1.166504
Broly Legendary Super Saiyan.mkv: +1.3140975
Coolers Revenge.mkv: +.032810
Dead Zone.mkv: +0
Fusion Reborn.mkv: .944 Seconds
This is my "AV Sync Fix.bat"
#echo off
set /P Delay=What is the delay you would like to use?
set /P Track=What track would you like to change?
for %%i in (*.mkv) do "C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o output/"%%i" -y %track%:%delay% "%%i"
Echo Your files were moved to a folder called output. Press ENTER to quit:
pause
Exit
I would like it to run that "for" command for each video line, filling in the delay automatically. I still want to manually put in the track.
Just to make sure I said it right. I want to start the batch file, still put in the track to change, then it will run the "for" command on each mkv file and use the corresponding delay for each. Whether I have 3 files, or 80 files. And it will only run on the files in that list.
Thanks in advance!

And it will only run on the files in that list.
Okay, then you should enumerate the list, not *.mkv:
#echo off
set /P Track=What track would you like to change?
for /f "usebackq delims=" %%O in ("offsets.txt") do (
for /f "delims=: tokens=1,2" %%a in ("%%O") do (
"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" -o output/"%%a" -y "%Track%:%%b" "%%a"
echo:&echo:&echo:
)
)
pause
Sync offsets should be set in milliseconds (see mkvmerge documentation) unless you want to have a headache dealing with floats via the limited batch-file integer arithmetic.

Related

Remove section of filename

I am needing a batch file to remove a certain part from multiple filenames in the same directory.
Example:
I have over 80,000+ files with the title like so:
Test Title, The - Conspiracy.zip
I am needing ", The" removed from file names leavin the titles like so:
Test Title - Conspiracy.zip
PS, I am needing this in Batch file only!
Any help is much appreciated!
THANX!!!
I found what I needed to use and thank you all for the quick replies and help!
#echo off &setlocal
set currentDirectory="%CD%"
for /f "delims=" %%a in ('dir /b /a-d *, The*.*') do (
set "fname=%%~a"
setlocal enabledelayedexpansion
set "nname=!fname:, The=!"
ren "!fname!" "!nname!"
endlocal
)
If you can use a Unix command shell, you could use the mv command in a loop.
You could download cygwin or Git Bash, or if you have Windows 10, you could do this right in the command line (assuming you've updated):
Creating a file like this
#!/bin/bash
for file in *.zip
do
removedPart=", The"
mv "${file}" "${file/removedPart/}"
done
You might want to test the command on a single file first to be sure it does what you want. i.e.
file=Test Title, The - Conspiracy.zip
removedPart=", The"
mv "${file}" "${file/removedPart/}"
You can loop through the contents of the file directory in something like this loop. Batch script loop
Then when you're looping through you can replace the contents of the file name. Look at this: String replacement in batch file
Sorry not more specific as Batch scripting isn't my thing. But this logic should prove to at least be helpful. Someone my post something better.

How do I use command prompt to identify derivative generation drop off?

I have one folder of approximately 7500 pdfs and a second folder with approximately 7300 tiff derivatives. Somewhere over the past 4 days of processing, intermittent tiff derivative generation failure occurred. How do I identify which files dropped off?
So far Ive tried:
diff -rq folder_pdfs folder_tiffs
However that reports all files as different given the difference in file extensions.
How do I identify which files dropped off?
Use the following batch file.
MissingFiles.cmd:
#echo off
setlocal
for /f %%f in ('dir /b folder_pdfs') do (
if not exist folder_tiffs\%%~nf.tiff (
echo folder_tiffs\%%~nf.tiff
)
)>>MissingFiles.txt
endlocal
Notes:
MissingFiles.txt will contain the list of missing files.
Example:
F:\test>dir /b folder_pdfs
1.pdf
2.pdf
3.pdf
4.pdf
5.pdf
F:\test>dir /b folder_tiffs
1.tiff
3.tiff
5.tiff
F:\test>MissingFiles.cmd
F:\test>type MissingFiles.txt
folder_tiffs\2.tiff
folder_tiffs\4.tiff
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.
parameters - A command line argument (or parameter) is any value passed into a batch script.

DOS Batch File Variable Modifier Returns Blank

I have a DOS batch file that will create a report listing files contained within a folder tree. The code below produces the desired output for over 115,000 files. However, 13 records are produced with blank date/time and file size. When I manually execute the DIR command (without the /b option), the desired file information is presented. Can this be corrected without adding considerable workaround code?
FOR /f "tokens=*" %%A IN ('DIR "<Path>" /a:-d /b /s') DO (
ECHO %%~tA %%~zA %%~dpA %%~nA %%~xA >> test.txt
)
(FOR /f "tokens=*" %%A IN ('DIR "<Path>" /a:-d /b /s') DO (
if exists "%%~A" ECHO %%~tA %%~zA %%~dpA %%~nA %%~xA
)) >> test.txt
The main reason for not obtaining a date/filesize is that the file can not be found.
How does your code work?
The for /f starts a separate cmd instance that runs the dir command.
When all the data has been retrieved and loaded into memory (that is, the cmd/dir command finished), then the for will start to iterate over the retrieved lines. Some time have passed between the information retrieval and the information processing.
In this time, "maybe" the problematic files have been moved/deleted/renamed and they can no be accessed to retrieve their properties. So, first check if the file still exists
The aditional parenthesis and redirection change are to avoid having to open the target file for each echo operation. This way, the file is opened at the start of the for command and closed at the end.

Can I control a variable from one batch file with another?

I'm using two batch files, and I need to control variables in one of them, from the other. Is this possible?
You cannot directly influence one process' environment from another process. You know, we've kinda outgrown ye olde days of real mode by now :-)
This all depends a bit on what you're trying to achieve here. If you're calling one batch file from the other, as in
call second.cmd
then the called one »inherits« the environment of the parent batch. So any variable you defined earlier will continue to exist in the child batch. You cannot propagate changes up to the parent, though and you cannot change a variable in the child batch after it has been started, too. It might still be a viable option if all you need is to perform some one-time initialization before starting the child batch.
What you could do is to agree on a file used by both batch files that they will use as a means of communicating with each other, likely located in the temporary directory. Each batch file would need to regularly check for the file to be present and if so, read it and update its variables accordingly. For that to succeed you need points in the batch files where they can look for that file. The simplest would be two files that simply do a bit communication with each other:
The code for that is here:
callchat.cmd:
#echo off
set SENDFILE=%TEMP%\1.out
set RCVFILE=%TEMP%\1.in
start call chat.cmd
ping -w 5000 -n 1 123.45.67.89 >nul 2>&1
set RCVFILE=%TEMP%\1.out
set SENDFILE=%TEMP%\1.in
start call chat.cmd
chat.cmd:
#echo off
setlocal enabledelayedexpansion
rem Prevent direct use
if not defined SENDFILE goto :eof
if not defined RCVFILE goto :eof
set MESSAGE0=I don't know what to say ...
set MESSAGE1=Foo
set MESSAGE2=Bar
set MESSAGE3=Hey there!
set MESSAGE4=Meow.
:loop
rem wait a bit
ping -n 1 -w 1000 123.45.67.89 >nul 2>&1
rem look whether we need to show something
if exist %RCVFILE% (
for /f "delims=" %%l in (%RCVFILE%) do echo Received message at %TIME% - %%l
del "%RCVFILE%"
)
rem randomly send out messages. Roughly ever three times we try this
set /a msg=%random% %% 5
set msg=!MESSAGE%msg%!
if %RANDOM% LSS 10000 (
>>%SENDFILE% echo(%msg%
echo(Sent message "%msg%"
)
goto loop
The batch file is started twice, with different input/output files – in fact, the reversed role of the files from the first invocation. Then it's little more than an endless loop that looks into its input file and read what's in there and writing stuff to its output file (which is the input file for the other batch).
I had to introduce a delay in starting both of them to avoid the PRNGs for both being exactly the same. It also reduced the cases where access to the file failed (this could probably be alleviated by renaming it before reading from it – or, if writing longer content, renaming it to its final name only after being done writing). It's just a simple demo application to show you that it might be possible that way.
To set environment variables you wouldn't print out what's in the file but call it as a batch file, for example:
if exist %RCVFILE% call %RCVFILE%
It would need the proper extension for that, though. You can also read it line by line and have each line contain a VARIABLE=VALUE pair:
if exist %RCVFILE% call for /f "tokens=1* delims==" %%a in (%RCVFILE%) do set %%a=%%b
The techniques mentioned above for improving reliability when accessing the same file from two different programs still apply.
As mentioned, this is only a rough idea how you could operate, iff I understood your question correctly.

XP Batch scripting - zipping with rinrar looping through a directory *.csv

I have read numerous articles now and it's not clear and there's lots of versions and this that and the other and I have been piecing things together and have got so far, my problem is the 'rar' command doesn't seem to accept my substition variable and instead reads it as a string.
But this is what I have
#echo off
SETLOCAL
set path=%path%;"C:\TEMP\Output"
set _sourcedir=C:\TEMP\Output
set _logfile=c:\temp\Output\zip_log.txt
set _rarpath=C:\Program Files (x86)\WinRAR
echo Starting rar batch > %_logfile%
:: Set default directory
pushd %_sourcedir%
echo Scan Directory is %_sourcedir%
FOR %%f IN (*.txt) DO (
echo %%f
%_rarpath\rar.exe a test
)
popd
ENDLOCAL
#echo on
I have cut some out and chopped it so you only get the essence, I haven't omitted any commands though.
I am trying to loop through the directory and locate all .txt files and zip them into a .rar file.
The echo writes out the correct filenames.
Any ideas?
I think this is your problem:
set _rarpath=C:\Program Files (x86)\WinRAR
In batch files, the environment variable delimiter is a space, so it thinks _rarpath is C:\Program
Enclose the path in double quotes and see if that helps:
set _rarpath="C:\Program Files (x86)\WinRAR"
Also, in your FOR loop change
%_rarpath\rar.exe a test
to
%_rarpath%\rar.exe a test
(or,perhaps this was a typo?)
I don't see where you're asking winrar to do anything with your files? %%f needs to be on the winrar command line somewhere.
Also, you shouldn't need a loop at all for this: rar.exe a test.rar %yourpath%*.csv or similar.