0

I have a list of en.srt files in my folder. I need to convert them to .srt extension.

For example Criminal Minds - 1x01 - Extreme Aggressor.en.srt to Criminal Minds - 1x01 - Extreme Aggressor.srt

Tried the below command and it didn't work,

ren *.en.srt *.srt

Renaming extension like ren *.srt *.srv works. (Changing all files' extensions in a folder with one command on Windows)

Would like to know if there is a workaround for this?

naveen
  • 53,448
  • 46
  • 161
  • 251
  • 3
    Not tested: `for /F "eol=| delims=" %I in ('dir *.en.srt /A-D /B 2^>nul') do for %J in ("%~nI") do @echo ren "%I" "%~nJ%~xI"` Modify `@echo ren` to `@ren` if the output list of `ren` commands looks right. – Mofi Apr 27 '20 at 15:36

3 Answers3

2

A simply though clumsy method is to rename the files twice – first remove .srt, then change .en to .srt (given that there are no other files *.en):

ren "*.en.srt" "*." & ren "*.en" "*.srt"

A more elegant solution is the one provided by user Mofi in his comment:

@for /F "eol=| delims=" %I in ('dir "*.en.srt" /B /A:-D 2^> nul') do @for %J in ("%~nI") do @ren "%~I" "%~nJ%~xI"

In a this code would look similar to this (note the necessarily doubled %-signs):

@echo off
rem // Loop through all matching files:
for /F "eol=| delims=" %%I in ('dir "*.en.srt" /B /A:-D 2^> nul') do (
    rem /* There are `~`-modifiers for `for` meta-variables that allow to split file names:
    rem    `~n` returns the base name, so the (last) extension becomes removed;
    rem    `~x` returns the extension (including the leading `.`);
    rem    therefore, `%%~nI` is the original file name with `.srt` removed, hence
    rem    ending with `.en`, and `%%~xI` is the original extension `.srt`;
    rem    another loop is used to also split off `.en` from `%%~nI`: */
    for %%J in ("%%~nI") do (
        rem /* Now `%%~J` returned the same as `%%~nI`, but `%%~nJ` removes `.en`;
        rem    so finally, rename the file to `%%~nJ` plus the original extension `.srt`: */
        ren "%%~I" "%%~nJ%%~xI"
    )
)

Following the thorough thread How does the Windows RENAME command interpret wildcards? on Super User, I found out that there is a way using a single ren command:

ren "*.en.srt" "?????????????????????????????????????????.srt"

However, you need to make sure to have enough ?, namely as many as there are characters in longest matching file name without .en.srt; otherwise, file names become truncated. You can avoid truncation by replacing the same sequence of ? instead of *, so longer file names are not renamed at all:

ren "?????????????????????????????????????????.en.srt" "?????????????????????????????????????????.srt"

Anyway, this only works when the original file names do not contain any more . besides the two in .en.srt; otherwise, everything behind the first . becomes removed and (finally replaced by srt).

aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

Not difficult in PowerShell to identify the files and replace the end of the filename with a regex. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Move-Item command.

powershell -NoLogo -NoProfile -Command ^
    "Get-ChildItem -File -Path '.' -Filter '*.en.srt' |" ^
    "ForEach-Object {" ^
        "Move-Item -Path $_.FullName -Destination $($_.FullName -replace 'en.srt$','srt') -WhatIf" ^
    "}"

Of course, it would be easier if the command shell were PowerShell. BTW, this exact same code would work on Linux and Mac without change.

Get-ChildItem -File -Path '.' -Filter '*.en.srt' |
    ForEach-Object {
        Move-Item -Path $_.FullName -Destination $($_.FullName -replace 'en.srt$','srt') -WhatIf
    }
lit
  • 14,456
  • 10
  • 65
  • 119
  • both worked. thanks. do you the reason why `ren` didn't work. I suspect its cos of the `.` in between. Is there a way to encode it? – naveen Apr 28 '20 at 13:38
0

I don't have a cmd at my disposition but I would guess that

ren *.en.srt *.tmp
ren *.tmp *.srt

works

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84