Batch changing titles of mkv files in multiple directories - windows

Ok, I'm total new at this...
Basically I'm using a tool call mkvpropedit to edit the title of my .mkv files
My aim is to create a batch the goes through all sub directories and replace the mkv file titles with their file name.
I have made the following progress...
for %%A in (*.mkv) do "C:\mkvpropedit.exe" "%%A" --edit info --set title="%%A"
Issue with [1]: It works fine but does not affect all sub directories and I would have to use the batch in all the sub directories one by one which will be time consuming.
for /R "C:\whatever" %%I in (*mkv) do "C:\whatever\mkvpropedit.exe" "%%I" --edit info --set title="%%I"
Issue here, It affects all sub directories but my .mkv file titles end up with the entire directory pathway instead of the file name.
Could anyone help me here? Thanks a lot in advance.
BTW if anyone know how to set a long directory pathway into a short form to be use repeated throughout the script (eg. "C:\whatever\whatever...\mkvpropeditexe into mkvpropedit", that would be helpful.

Whether you use %%~nI or %%~nxI (as suggested by Gerhard Barnard) depends on how you want the title: "name" only or "name.extension".
for how to set a long directory pathway into a short form to be use repeated throughout the script; set a variable with the full path\name and use the variable:
set "mkv=C:\whatever\mkvpropedit.exe"
for /R "C:\whatever" %%I in (*.mkv) do "%mkv%" "%%I" --edit info --set title="%%~nI"

Using the help from this thread, here's a little more elaborate batch script I developed:
rem This Bat file will take MKV filenames and apply them to MKV info titles
#echo off
rem Modify next line to path where mkvpropedit.exe !!!!!!!!!
cd "C:\Program Files\MKVToolNix"
set /A errors1=0
rem Modify next line to path where MKV files are. This will also modify MKV's in subdirectories. !!!!!!!!!
for /R "X:\Move" %%X in (*.mkv) DO CALL :loopbody %%X
echo.
echo.
echo Total Errors = %errors1%
echo.
pause
GOTO :EOF
:loopbody
set title0=%*
set "title1=%title0:.mkv=%"
set "title2=%title1:\=" & set "title2=%"
rem The following two lines are to remove additional info I use in the filenames.
set "title3=%title2: 1080p=%"
set "title4=%title3: 720p=%"
set command1=mkvpropedit "%title0%" --edit info --set "title=%title4%"
for /f "delims=" %%a in ('%command1%') do #set response1=%%a
echo %title2%
echo %response1%
echo.
echo.
if /i "%response1:~0,5%"=="Error" (set /A errors1=%errors1% + 1)
GOTO :EOF

Related

Sorting files into folders, according to a part of filename using Windows Batch file

I have around 12,000 .jpg files in a data-set folder in G:\train with names such as
0002_c1s1_000451_03.jpg, 0002_c1s1_000551_01.jpg...
up to
...1500_c6s3_086542_02.jpg , 1500_c6s3_086567_01.jpg
I want to move them to new folders with their initial filename such as 0002, 0005, 0007,... 1496, 1500
What I need is a Windows batch file to create new folders & move files quickly without a hassle. I've tried few other answers to no avail.
Something like this should do, as a .cmd/.bat script file:
#rem The DEBUG_RUN variable enables a step-by-step mode, it will cause the
#rem script to pause after processing every file. Remove the DEBUG_RUN (and
#rem the pause instruction) when you are confident it does what you want,
#rem restart the script and enjoy :-)
:main
#setlocal
#set DEBUG_RUN=1
#pushd "G:\train"
#for %%f in (*.jpg) do #(
call :mvToSubDir "%%~nxf"
if defined DEBUG_RUN pause
)
#popd
#pause
#endlocal
#goto:eof
:mvToSubDir
#set fn=%~1
#set dn=%fn:~0,4%
#if not exist "%dn%" mkdir "%dn%"
move "%fn%" "%dn%"
#goto:eof
you can try a series of for loops to accomplish this
for /l %%f in (1,1,12000) do (
mkdir %%f
for %%d in (c:\sourcedir) do (
move %%d %%f
)
)
i didn't fully understand how you wanted to sort them but i hope this helps. You can use the "for /?" command and feel free to ask for more help if this isn't enough. I wish you luck with your endeavors.

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 file to move many listed files to one specific folder

I have a Master folder which have multiple sub folders. All the subfolders have lot images with different extensions (jpg,tif and png). The total number of images in all the subfolder are around 90000 images.
The thing is, I need to search some 500 images in Master folder and its subfolders and move the images to specified folder.
I tried a below batch script to use the text file to search through a Master folder and all subfolders and move all the files from the list and paste them into a Specified single folder.
The text file which contains file names without extension.
But my batch script is not working. It didnt throw me any error.. but nothing happens when I run it.
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f %%X in (%FIILELIST%) do call :MOVE_FILES "%%X"
goto :eof
:MOVE_FILES
for /r %FILESPATH% %%I in (%~1) do echo move /qvs "%%I" "%DESTPATH%%%~pnxI"
I am very new to batch script and in learning stage. Kindly help me in this. Im very much thankful if anyone provide the correct batch script to do this.
Can you try this?
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f "delims=" %%x in (%FIILELIST%) do (forfiles /p %FILESPATH% /s /m %%x.* /c "cmd /c move /y #path %DESTPATH%\#file" 2>>failed_temp.txt)
for /f "tokens=5 " %i in (failed_temp.txt) do (echo.%~i)>>failed_list.txt
del failed_temp.txt
Cheers, G
#gbabu's answer was super helpful for me.
I needed to edit it to handle file names that were absolute (full) instead of relative. And I needed to handle that they contained spaces.
Unfortunately I couldn't figure out how to log the errors like #gbabu did.
#echo off
REM See https://stackoverflow.com/a/25325529/470749
REM See https://stackoverflow.com/a/163873/470749
REM See https://stackoverflow.com/a/155950/470749
set FILELIST="K:\F\Users\my_user\Documents\My Music\JUKEBOX\5.m3u_list.txt"
set DESTPATH=C:\temp\cdrw
for /f "usebackq tokens=*" %%x in (%FILELIST%) do (copy "%%x" %DESTPATH%)
pause
These articles helped me:
https://stackoverflow.com/a/25325529/470749
https://stackoverflow.com/a/163873/470749
https://stackoverflow.com/a/155950/470749

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
)

Removing a "sample.ext.ext2" file that has no "sample.ext" file associated with it

The application creates .mta files (with exactly same name) of all multimedia files in my HDD. What I want to do is to check all sub-folders of root folder if there is no multimedia file associated with some .mta then delete it.
Detailed example. Lets say we have files
01.mp3
01.MP3.mta
02.mkv
02.MKV.mta
03.jpg
03.JPG.mta
04.MP4.mta <<==
As you see the last .mta has no original file. I want to delete last file.
I don't know if it's possible with cmd. But following function doesn't work. Please take a look
For /r %%i in (*.mta) do call :nomta %%i
pause
goto end
:nomta
set stem=%1:.mta=%
set original=%stem%.mta
if not exist %original% do exit /B
if not exist %stem% do del /a /Q %1
goto :EOF
:end
echo done
PAUSE
You can use a for command to do this with dir /a feeding it both hidden and non-hidden filenames. Here's an example:
C:\temp\z\z>attrib *
A C:\temp\z\z\foo.bar.mta
A H C:\temp\z\z\h2.mp4.mta
A C:\temp\z\z\hid.mp4
A H C:\temp\z\z\hid.mp4.mta
A C:\temp\z\z\zoo.bar
A C:\temp\z\z\zoo.bar.mta
C:\temp\z\z>for /f %F in ('dir /b/a *.mta') do if not exist "%~nF" echo %F >> z
C:\temp\z\z>type z
foo.bar.mta
h2.mp4.mta
so replacing the echo with a del should achieve your target.

Resources