Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batchfile To Drag Folders Onto And Rename Certain File Inside To Folder Name  (Read 42880 times)

0 Members and 1 Guest are viewing this topic.

Thaum2u

    Topic Starter


    Rookie
    • No You!
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 11
Hi.

I have a bunch of folders with badly named files inside. Each folder has one of two types of files in it.  I have renamed the folders to the exact name I want the file inside that folder to be named (minus the file type extension), but rather than copy the folder name, go into the folder, rename the file by pasting the folder name in the renaming operation, then repeating that process for each and every folder, I want a batchfile to do it all for me.

I want to be able to drag a bunch of folders onto the batchfile and have it go one by one through the folders renaming the file inside each folder.

I tried playing with a for loop, but I am having a very hard time grasping an understanding of how to do it properly, so I did it as posted below in my current code listing...

I need to have each folder processed as follows, I assume:

1) The folder name is assigned to a variable.
2) Change Directory into that folder.
3) Check for one of two file types, (in my case .mkv and .mp4) and rename the file to the name of the folder it is in, adding the file type extension it finds in that folder to the end, completing the renaming function for the file in that folder.
4) Do loop again until all of the folders dragged and dropped onto the batchfile have been processed.

Code: [Select]
@echo off
cls
echo.

:LOOP
set EXTENSION==
if _%1==_ goto :END
pushd "%1"

:CHECK4MP4
if not exist *.mp4 goto CHECK4MKV
set EXTENSION==.mp4
rename *.mp4 %1%.mp4
goto REPORT

:CHECK4MKV
if not exist *.mkv goto ERROR
set EXTENSION==.mkv
rename *.mkv %1%.mkv
goto REPORT

:ERROR
echo Neither a .MKV nor a .MP4 has been found in the %1% folder.
echo.
pause
goto CONTINUE_LOOP

:REPORT
echo The video file has been renamed to %1%.%EXTENSION%
echo.
pause
goto CONTINUE_LOOP

:CONTINUE_LOOP
popd
shift
goto LOOP

:END


As it processes each folder, it informs me that my syntax is incorrect, and fails to rename the files.

Any help is greatly appreciated.

Thaum2u

    Topic Starter


    Rookie
    • No You!
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 11
Anyone?  Please?

erobby



    Beginner

    • Experience: Experienced
    • OS: Linux variant
    Your explanation doesn't mean anything, list examples of what you are trying to accomplish

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    2) Change Directory into that folder.
    3) Check for one of two file types, (in my case .mkv and .mp4) and rename the file to the name of the folder it is in, adding the file type extension it finds in that folder to the end, completing the renaming function for the file in that folder.
    You cannot rename a folder or file that is currently in use. Don't do the change directory.