Renaming my video files as per the resoltuion using batch - windows

I would like to rename my video files as per the resolution they are in, e,g for a video 'bla bla.mp4' in 1080p, I would like to rename it to 'bla bla [H.264 1080p]. The script should automatically be able to detect the resolution of the video, and also if the file has been already renamed it should not rename it.I wasn't able to find a way to check for the resolution, so I tried to use this for 1080p files:
FOR /r %%a in (*.mp4) DO (IF EXIST *[H.264*.mp4 (
ECHO Already done)
ELSE (
REN "%%~a" "%%~na [H.264 1080p].mp4"))
But what it does is it checks for the same file again and again which has already been renamed and therefore the reply always is 'Already done'.

This question is beyond the scope of an SO question. Nevertheless I will answer it, because today is sunday.
download and install mediainfo command line version
add the path to the mediainfo binaries to your system or user PATH environment variable
copy the rename script, replace the path to your video folder, there is a safety echo before the rename command, remove it if the output looks good
the script tests for already-exists and already-processed files (suggested by Peter Wright)
rename script:
#echo off & setlocal
cd X:\video\path
for /f "delims=" %%i in ('dir /b /a-d *.mp4') do (
set "fnameo=%%~ni"
set "fnamee=%%~xi"
set "Height="
for /f "delims=" %%j in ('mediainfo "--Inform=Video;%%Height%%" "%%~i"') do set "Height=%%j"
setlocal enabledelayedexpansion
call set "selftest=%%fnameo:[H.264 !Height!p]=%%"
if "!selftest!" equ "!fnameo!" if not exist "!fnameo! [H.264 !Height!p]!fnamee!" (
echo rename "!fnameo!!fnamee!" "!fnameo! [H.264 !Height!p]!fnamee!"
)
endlocal
)
output example:
rename "Handbrake.0.9.8.mp4" "Handbrake.0.9.8 [H.264 800p].mp4"
rename "Hybrid.2012.10.21.1.mp4" "Hybrid.2012.10.21.1 [H.264 800p].mp4"
rename "Womble.mp4" "Womble [H.264 1080p].mp4"

There was a very similar question here:
Windows batch file renames filename unexplainably in FOR loop
Peter Wright had posted a solution with a very good explanation:
Try
for /f "delims=" %%a in (' dir /b /a-d *.mp3') do ( The problem is
that your original syntax finds the next filename - which finds your
renamed file since the new filename (with the prefix) is logically
'greater than' the old.
Using dir builds a list of filenames, and processes the list once it
has been completely built - hence the list doesn't vary as the files
are renamed.
The "delims=" clause ensures that the default parsing of the filename
is ineffective - otherwise, the filename would be interpreted as a
series of [space-separated] tokens.
answered Jun 20 at 4:28 Peter Wright

The IF EXIST conditional will always pass after the first file is renamed, since as written it will check for the existence of any file in the directory that contains "[H.264" in the file name, not just %%a.
Consult Batch file: Find if substring is in string (not in a file) to learn how to find "[H.264" in %%a, which would cause the intended result.

Related

Bat script to find and replace files in multiple subfolders - replace .java files with .class files from specific folder

I am new to windows batch scripting .. please help on this scenario
I have file structures as below ::
dir1:
c:\workspace\changeset\com\folder
subfolder1
one.java
subfolder-2
two.java
dir2:
c:\workspace\target\class\com\folder
subfolder1
one.class
subfolder2
two.class
Subfolder3
three.class
Need to find and replace dir1 files in respective subfolders i.e one.java and two.java from dir2 files i.e one.class and two.class ( need to replace certain .java files with .class files from specific folder )
much appreciated for your help.
Thanks
Arjun
for /f "delims=" %%a in ('dir /b /a-d "c:\workspace\changeset\com\folder\*.java"') do (
if exist "c:\workspace\target\class\com\folder\%%~na.class" (
echo copy "c:\workspace\target\class\com\folder\%%~na.class" "c:\workspace\changeset\com\folder\%%a")
)
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
Note that execution directly from the prompt and as lines with a batch file are different. The metavariable (loop-control variable) %%x must be referenced as %%x for a batch line and %x if executed from the command prompt. Since it makes little sense to repeatedly execute a line containing a for from the prompt (it's easier to create a batch file), I post the batch-file version. User's responsibility to adjust for direct-from-prompt if desired.
Read each filename in /b basic form /a-d without directories and assign the filename+extension to %%a.
If a file in the other directory called thenamepartofthefile.class exists, then copy that file to the first directory.
Please post the entire problem to be solved. The approach can change radically, as in this case.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir1=U:\sourcedir\changeset"
SET "sourcedir2=U:\sourcedir\target"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir1%\*.java" '
) DO (
SET "javadir=%%~dpa"
SET "classfile=!javadir:%sourcedir1%=%sourcedir2%!%%~na.class"
IF EXIST !classfile! ECHO COPY "!classfile!" "%%a"
)
GOTO :EOF
You would need to change the settings of sourcedir1 and sourcedir2 to suit your circumstances.
Essentially, the same approach and the same comments re messages. The difference is that this procedure uses files and subdirectories in the dir list and substitutes the first part of the directoryname in deriving the expected name of the .class file.

Batch Script to Grab File Name and Add to Beginning of Each Line

I'm trying to grab the file name from a .LOG file and append it to the beginning of each line in the file, sans extension, with a space between the file name and the line contents. There are many such files in a folder and the script should be performed for each .LOG file in the folder. An example file name looks like this:
20160201.log
I want to add the file name to each of the lines in the file which look like this:
00:00:23 Left 4EEFD9 6.59418mA 0.00003mA OK
00:00:23 Right 4EEFEF 6.85377mA 0.00001mA OK
00:00:44 Left 4EEFDC 6.32207mA 0.00004mA OK
To give a result of this:
20160201 00:00:23 Left 4EEFD9 6.59418mA 0.00003mA OK
20160201 00:00:23 Right 4EEFEF 6.85377mA 0.00001mA OK
20160201 00:00:44 Left 4EEFDC 6.32207mA 0.00004mA OK
I saw this question here but it references pulling a filename from a list of filenames and placing at the beginning of each entry respectively, so that doesn't work for me. I also looked at this question, but since I need to use the file name instead of my current system time that won't work either. I don't have any code to offer...I couldn't use anything conclusive from the questions I found and I'm totally new to batch scripts.
EDIT 1: Magoo's code
Here is the current code I'm trying to run using Magoo's answer (Note the comments are my addition for troubleshooting):
#ECHO Off
SETLOCAL
SET "sourcedir=C:\Users\ckemmann\Desktop\Current"
SET "destdir=C:\Users\ckemmann\Desktop\Current\New"
::echo %sourcedir% <I check what I've assigned to sourcedir.
::echo %destdir% <And to destdir.
::pause
FOR %%f IN ("%sourcedir%\*.log") DO >"%destdir%\%%~nf.out" (
FOR /f "usebackqdelims=" %%a IN ("%%f") DO (
ECHO %%~nf %%a
)
)
::pause <I catch the batch script telling me that the system cannot find the path specified.
GOTO :EOF
As shown in the code, I get a complaint from windows saying that it can't find the file path...even though that's the one I copied from the folder properties.
EDIT 2:
Since I didn't hear back on my edit 1 I went with Arescet's answer. I sadly don't seem to have any way around the slowness of the script. My final code is the same as what he provided in his answer.
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR %%f IN ("%sourcedir%\*.log") DO >"%destdir%\%%~nf.out" (
FOR /f "usebackqdelims=" %%a IN ("%%f") DO (
ECHO %%~nf %%a
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
Decoding...
Take the files matching "*.log" from the specified directory, assign their names to %%f
create a new file %%~nf.out in the destination directory. Read each line from %%f to %%a and echo that line, prefixed by %%~nf and a few spaces.
%%~nf means the name part only of %%f.
The redirector > createas a new file and in the position it's been placed, accepts the output of the echo into that file instead of the normal echo-to-console.
#echo off
for /r "C:\PATH TO WHEREVER\" %%G in (*.log) do (
ren "%%~G" "reWrite.temp"
for /f "tokens=*" %%H in (reWrite.temp) do (
echo %%~nG %%H >> %%~nG.log
)
del "reWrite.temp"
)
pause
This takes all .log files in "C:\PATH TO WHEREVER\", renames them, and writes the filename and data line by line into the original name, then deletes the file when done. #Magoo's answer clearly tops mine, but a different solution should help you understand different ways to solve future desires.

Batch renaming files after certain character

I have a bunch of video files with names like so:
6592110904-Ivory-2.mp4
6592280588-Cornflower.mp4
6592321696-Ballet Pink.mp4
I want to rename them to get rid of everything after the first hyphen so they end up like:
6592110904.mp4
6592280588.mp4
6592321696.mp4
How do I go about doing this?
Please put the code below in a bat file, place it in directory with mp4 files. Before running real renaming, please remove "echo" before "move". please be carefull with renaming bacause (theoretically) it is possible to have same name for different files.You'll be prompted to confirm if you want to override the old one.
Code splits each filename after dash and renames the file taking first item. Good luck.
#echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.mp4"') do (
echo move "%%a-%%b" "%%a%%~xb"
)

Windows batch file loop running one extra iteration [duplicate]

This question already has an answer here:
At which point does `for` or `for /R` enumerate the directory (tree)?
(1 answer)
Closed 3 years ago.
I'm using a windows batch file to rename a very large number of files in one go. The renaming simply adds a prefix to the file name. The file executes fine except for the last file, to which the prefix is somehow applied twice. I'm not sure what I'm doing wrong. Can anyone point out the problem to me?
for %%i in (*.csv) do ren %%i myprefix_%%i
#echo off
SETLOCAL
DEL *.csv
XCOPY ..\*.csv .
(
for %%i in (*.csv) do (
echo ====================
echo %%i
dir *.csv
ren %%i x%%i
dir *.csv
echo ====================
)
)>u:\junk.txt&edit u:\junk.txt
I ran the above batch in a clean directory below a directory that contained a few .CSVs. All it does is zap the current directory's .csvs, copy those from the parent directory and then rename by prefixing. It revealed quite a bit.
(U: is a RAMDRIVE, EDIT executes EDITPLUS; NOTEPAD would do as well here)
The operation depends on quite what prefix is added. A prefix starting "a" works differently from a prefix starting "x". AND the result depends on the filesystem being used. NTFS presents the filenames alphabetically but the FAT system on the RAMDRIVE doesn't sort the names.
I believe the problem is caused by the 'findnext' filename mechanism not dealing as expected with a changing list of filenames. As it changes one, that name may move in the list and hence the file may be reprocessed as its new name is encountered again - possibly multiple times.
A cure is to replace the for selection with
for /f "delims=" %%i in ('dir /b *.csv') do (
where the DIR list is created and THEN processed, so the changes don't affect it.
You may be having some spaces in your filenames. Use double-quotes around filenames. Try the following:
for %%i in (*.csv) do ren "%%I" "myprefix_%%I"
This will execute as:
ren "tmp - Copy.csv" "myprefix_tmp - Copy.csv"
Also, you should look for any hidden files.

For loop in batch file reading a file of File Paths

I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)

Resources