user1868774
user1868774

Reputation: 113

How to rename a file according his folder name via batch script


I have this part of batch script which is doing following:
-There is a main folder, in the main folder are two files (movie & subtitle file) and one Sub-folder called 'Subtitles'

-This script takes the name of the movie file and renames with it the subtitle file + moves the subtitle file into the 'Subtitles' sub-folder and then renames the main folder. So at the end we have one movie name, which is used on the subtitle file and on the main folder as well.

@echo off
setlocal EnableDelayedExpansion


cd /D "%~DP0"
echo BASE FOLDER: %cd%
set n=0
for /D %%a in (*) do (
set /A n+=1
cd "%%a"
echo ==================================================================
echo Processing folder: %%a


for %%b in (*.avi *.mp4 *.mkv) do set movieName=%%~Nb
echo Movie name: !movieName!
for %%b in (*.srt *.sub) do (
   move "%%b" "Subtitles\!movieName!%%~Xb"
  echo File "%%b" moved and renamed to "Subtitles\!movieName!%%~Xb"
)


cd ..
ren "%%a" "!movieName!"
echo Folder "%%a" renamed to "!movieName!" 

)
echo ==================================================================
echo %n% FOLDERS PROCESSED
pause

!!!!! What I need is following: !!!!!
-I need to make it vice versa, so the name would be taken from the main folder and not from the movie file, so the name of the main folder would be used on the movie and on the subtitle file.

Thank you!

Upvotes: 2

Views: 4567

Answers (2)

Cathybeauth
Cathybeauth

Reputation: 1

I have written this python script, to move and rename the .srt file to the root folder. If it can help someone.

import os, shutil
directory= r'\\c:\path_to_main_folder'
working_directory = directory + "\Subs"
for file in os.listdir(working_directory):   
    for subtitle_file in os.listdir(os.path.join(working_directory,file)) :
        if subtitle_file.endswith(".srt"):
            name = os.path.join(directory, file + ".srt")
            if not os.path.exists(name): # sometimes there are multiple srt file in the subs, hence it checks if it already exist if yes it skip
                print(f"Moving {file} to folder {directory}")
                shutil.copy(os.path.join(os.path.join(working_directory, file),subtitle_file), os.path.join(directory, subtitle_file))
                print(f"Renaming {subtitle_file} to {file}")
                os.rename(os.path.join(directory, subtitle_file), os.path.join(directory, file + ".srt"))    
# shutil.rmtree(working_directory)      # to delete the folder subs afterwards
print("Done")

Upvotes: 0

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion


cd /D "%~DP0"
echo BASE FOLDER: %cd%
set n=0
for /D %%a in (*) do (
set /A n+=1
cd "%%a"
echo ==================================================================
echo Processing folder: %%a
set movieName=%%~a


for %%b in (*.avi *.mp4 *.mkv) do (
   ren "%%~b" "!movieName!%%~Xb"
   echo Movie file "%%b" renamed to "!movieName!%%~Xb"
)

for %%b in (*.srt *.sub) do (
   move "%%~b" "Subtitles\!movieName!%%~Xb"
   echo File "%%b" moved and renamed to "Subtitles\!movieName!%%~Xb"
)


cd ..

)
echo ==================================================================
echo %n% FOLDERS PROCESSED
pause

Upvotes: 4

Related Questions