EDIT: Thank you all, Magoo tips fix it , I using the following code:
echo off
cls
setlocal enabledelayedexpansion
for %%f in (*.mov) do (
set name=%%~nf
set new_name=!name:*_= !
echo File renamed from: !name!.mov to:!new_name!.mov
rename %%f !new_name!.mov
)
This only works if the file name is something_file_name.mov. If original file is only file_name.mov, I end up it only name.mov So using REGEX is the best option. I'm using a tweaked version of Ben Personick and Squashman suggestions.
@echo off
cls
SET "_Regex=^[0-9]*_"
FOR %%F IN (*.mov) DO (
ECHO.%%~nF | findstr /R "%_Regex%" >nul && (
FOR /F "Tokens=1* Delims=_" %%f IN ("%%~nxF") DO (
MOVE /Y "%%F" "%%g"
)
)
)
I have some files named 3424_file_name.mov and need to remove the numbers until the first _ so to get file_name.mov Even better would be to set a range to remove like [0-9] Like to do it in cmd windows 7. This is what i got so far, but not working.
cls
setlocal enabledelayedexpansion
for %%f in (*.mov) do (
set name=%%~nf
set new_name=%name%:*_=x
echo %new_name%)
rename %%f %new_name%.mov
)
Thanks Alex
delayed expansionthe #1 FAQ in this tag. Since you've invokeddelayed expansion, you need to access the changed value of a variable using!varname!instead of%varname%setcommand is incorrect. You needset var1=!var2:*_=x!(note the position of the!s)%%fvarcmd.exescripting is riddled with arcane syntax and odd quirks. PowerShell is consistent and more readable (see my answer).