Reputation: 11
I have been trying to find a way to rename any/all subtitle files (.srt, .idx, and .sub) by putting a prefix (ZZSubs_) to all the files matching this case. I also want the batch file not to execute/skip renaming if it is run accidentally a second time. With the code below, every time I run the batch file it keeps prepending "ZZSubs_" to the already renamed files even though I am checking for the "ZZSubs_" with the IF NOT EXIST and the asterisk * symbol.
@ECHO OFF
CD /D "%~dp0"
for /F "delims=" %%i in ('dir /b *.SRT *.IDX *.SUB') do (
REM Rename Subtitle files with a ZZSubs_ filename prefix
IF NOT EXIST "ZZSubs_*%%i" RENAME "%%i" "ZZSubs_%%i"
)
PAUSE
This is very simliar to the question asked below, but I am also looking to perform this recursively in all sub directories of a given folder and not to a specific, hard-coded folder path: Batch rename: skipping already renamed files
Any help would be greatly appreciated.
Upvotes: 1
Views: 365
Reputation: 16226
This should work in a windows
batch-file
run by cmd
. If you are on a supported Windows system, PowerShell will be available. When you are confident that the files will be renamed correctly, remove the -WhatIf
from the Rename-Item
command.
powershell -NoLogo -NoProfile -Command ^
"Set-Location -Path %~dp0;" ^
"Get-ChildItem -Path *.SRT,*.IDX,*.SUB |" ^
"ForEach-Object { if ($_.Name -notmatch '^ZZSubs_') { Rename-Item -Path $_.FullName -NewName ('ZZSubs_'+$_.Name) -WhatIf } }"
Upvotes: 1
Reputation: 79982
for /F "delims=" %%i in ('dir /b *.SRT *.IDX *.SUB ^|findstr /v /b "ZZSubs_"') do (
should be the change you need. This sends the dir
output to findstr
(the caret is required to tell cmd
that the pipe is part of the command to be executed, not of the for
). The findstr
command finds all the lines that do NOT (/v
) begin (/b
) with the string.
Suppose with your code, you executed it with a file named ZZSubs_one.srt
. Now - does ZZSubs_ZZSubs_one.srt
exist?
Upvotes: 2