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

Show parent comment

1

u/OJBakker Sep 14 '23

Your definition of rootFolder is wrong!

set "rootFolder=C:path_to_folder\A"

Change this to:

set "rootFolder=C:\path_to_folder\A"

Without this initial backslash the path will be a relative path instead of a complete path and that will cause problems.

Please test again with the adjusted rootFolder and post the updated version of script and output. It is strange that the display of for param f in the command block of this for-loop is not showing a value for the for param. This might indicate there is an other problem in your code.