Wednesday, June 14, 2017

batch - Creating a text file inside a folder then rename folder to match a filename inside it


I've got a folder called "Movies" with several subfolders inside it, each having the title of a movie + a description as a name, let's say: "Blue Velvet (1986) - The Criterion Collection".


Inside each movie subfolder there's a number of files, typically a video file (with extensions such as .mkv, .mp4, .avi, .wmv etc.), a subtitle file (.srt extension) and sometimes a .jpg image. Those files have an alternative format of the movie title as a name, different than the name of the folder they are into e.g "Blue Velvet - David Lynch (1986)".


What I want to achieve is:


1. Create a text file inside each movie subfolder. The text file should have the exact same name with the subfolder in question.


then (I don't mind the order but I'm not sure the steps could be performed in the opposite direction)


2. Rename the movie subfolder, having it adopting the same name with the video (without the extension of course).


Or to put it in another way, since AFAIK there's not such thing as renaming folders when it comes to cmd and bat files but moving files between folders instead, I'd like to create a text file named after the movie subfolder then move it in a new folder named after the video filename, along with all the other files from the initial folder.


So


Movies/Blue Velvet (1986) - The Criterion Collection/
Blue Velvet - David Lynch (1986).mkv
Blue Velvet - David Lynch (1986).srt
Blue Velvet - David Lynch (1986).jpg

becomes


Movies/Blue Velvet - David Lynch (1986)/
Blue Velvet - David Lynch (1986).mkv
Blue Velvet - David Lynch (1986).srt
Blue Velvet - David Lynch (1986).jpg
Blue Velvet (1986) - The Criterion Collection.txt

Ideally I'd like the process to initiate by selecting folder(s) and fire up a bat. file either by a context menu action or "send to" the bat file, I know how to do both, I just want help with the command in the bat file. That way, using the previous example, I could process only the "Blue Velvet" and other selected movie subfolders and not necessarily everything in the "Movies" root folder in case some don't need sorting but if that's not possible, no problem. Special characters/symbols, numbers and spaces could be included in the folder and file names though.
I've searched around and I couldn't find an answer to apply in my situation, I guess the trickiest parts are the subfolder "rename" and a way to perform the action only with selected movie subfolders and not every subfolder in the "Movies" root folder.


Any help would be greatly appreciated, thanks a lot!


Answer



Hm, I guess it would be something like this:


@echo off
Title Movie Folder Renamer
:Start
Rem Put the path to the movie folders here:
set MovieFolders=%CD%
cls
echo.
set /p Pattern=Please inform the pattern:
if /i "%Pattern%"=="*.*" set "Pattern=|Find /i ".""
pushd %MovieFolders%
echo.
Echo List of affected Folders:
echo.
dir /ad /b /p %Pattern%
echo.
echo What would you like to do? (C)ontinue ^| C(a)ncel ^| (N)ew Pattern
Choice /c "CAN"
if "%errorlevel%"=="2" Goto Exit
if "%errorlevel%"=="3" Goto Start
for /f "Delims=" %%a in ('dir /ad /b ^%Pattern%') do (
echo.> "%%a\%%~nxa.txt"
pushd "%%a"
for %%b in (*.mp4 *.mkv *.avi *.wmv *.asf *.mpg *.mpeg *.fla) do (
popd
ren "%%a" "%%~nb"
)
)
if exist "%~pd0\NoSrt.txt" del "%~pd0\NoSrt.txt"
for /d %%c in (%MovieFolders%\*) do if not exist "%%c\*.srt" echo %%~nc >> %~dp0NoSrt.txt
:Exit

Before:


enter image description here


After:


enter image description here


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...