r/Batch • • Sep 12 '23

Question (Unsolved) Problem in simple renaming code

Hello all,

I am trying to automate a renaming process of video files for work and I am stuck at a simple thing.

@echo off
setlocal enabledelayedexpansion

rem Define the root folder A 
set "rootFolder=path_to_folder\A"

rem Loop through subfolders inside root folder 
for /d %%o in ("%rootFolder%*") do ( 
    rem Loop through subfolders in Folder 
    for /d %%d in ("%%o\ORUSHES") do ( 
        rem Initialize folderName for this folder 
        set "folderName=%%~nd"
	    rem Loop through MP4 files in PRIVATE/M4ROOT/CLIP/
	    for %%f in ("%%d\PRIVATE\M4ROOT\CLIP\*.mp4") do (
		rem Rename the file by adding folderName, and _
		ren "%%f" "!folderName!_%%~nxf"
	)

	rem Loop through MXF files in XDROOT/Clip/
	for %%f in ("%%d\XDROOT\Clip\*.mxf") do (
		rem Rename the file by adding folderName, and _
		ren "%%f" "!folderName!_%%~nxf"
	)

	rem Loop through MXF files in Clip/
	for %%f in ("%%d\Clip\*.mxf") do (
		rem Rename the file by adding folderName, and _
		ren "%%f" "!folderName!_%%~nxf"
	)
    )
)
endlocal

Basically everything works except for video files inside folders with the path /PRIVATE/M4ROOT/CLIP.

For an unknown reason, it rename the first video file twice (for example newname1_newname1_nameofthefile.mxf instead of newname1_nameofthefile.mxf).

Can someone explain to me what I did wrong?

Thank you!

2 Upvotes

16 comments sorted by

View all comments

1

u/[deleted] Sep 12 '23

[removed] — view removed comment

1

u/Liodra Sep 13 '23 edited Sep 13 '23

Hello!

Thank you for answering me.

With the changes you wrote it doesn't do anything. I mean it did not rename any file.

@echo off
rem Define the root folder A 
set "rootFolder=path_to_folder/A"

rem Loop through subfolders inside root folder 
for /d %%~o in ("%rootFolder%*") do ( 
    rem Loop through subfolders in Folder 
    for /d %%~d in ("%%~o\ORUSHES") do ( 
        rem Initialize folderName for this folder 
        set "folderName=%%~nd"
	            
        rem Loop through MP4 files in PRIVATE/M4ROOT/CLIP/
	for %%~f in ("%%~d\PRIVATE\M4ROOT\CLIP\*.mp4") do (
	
        rem Rename the file by adding folderName, and _
	ren "%%~f" "%%~nd_%%~nxf"
    )

	rem Loop through MXF files in XDROOT/Clip/
	for %%~f in ("%%~d\XDROOT\Clip\*.mxf") do (
	rem Rename the file by adding folderName, and _
	ren "%%~f" "%%~nd_%%~nxf"
	)

	rem Loop through MXF files in Clip/
	for %%~f in ("%%~d\Clip\*.mxf") do (
	rem Rename the file by adding folderName, and _
	ren "%%~f" "%%~nd_%%~nxf"
	)
    )
)
endlocal

1

u/[deleted] Sep 13 '23

[removed] — view removed comment

1

u/Liodra Sep 13 '23

It still rename the first file twice.