Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming Files 3

Status
Not open for further replies.

dik

Technical User
Jul 18, 2001
217
MD
Is there a DOS rename command that allows you to rename a file such as 'Some.File.Name.S06e01.Sdtv.X264-Sonarr-1.m4v' to 'S06E01.mkv'? The *.mkv is the same as *.m4v.

Is it possible to do it in parts? Like 'Some.File.Name.S06e01.Sdtv.X264-Sonarr-1.m4v' to 'S06e01.Sdtv.X264-Sonarr-1.m4v' and 'S06e01.Sdtv.X264-Sonarr-1.m4v' to 'S06e01.m4v' and 'S06e01.m4v' to 'S06e01.mkv' The latter part is easy... ren *.m4v to *.mkv...

Thanks Dik
 
Easy to do, if:
[ul]
[li]you establish the rules that will apply to all files you want to rename, and[/li]
[li]renaming your files will not violate the Window's rules, like: you cannot have multiple file names in the same folder, etc.[/li]
[/ul]
Also, it would be good to know if your files to be renamed are all in the same folder or spread all over the folders' structure?

You can do it in many different languages, but I would do it in Excel's VBA

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
They are in the same folder... I was hoping that there were a couple of switches or variables that could be used with the REN function. The rules are as established in the example... there might be 20 files with S01Exx name but the beginning of the file name is the same, and the end of the file name is the same.
Is there a way to strip the first 10 characters from a file name and the last 12 characters?
 
What you could use is the Bulk Rename Utility. It's efficient and free, as in beer.
You can find it here.

I've got nothing to hide, and I demand that you justify what right you have to ask.
 
I have these files in [tt][blue]C:\Test7\[/blue][/tt] folder:[pre][red]
0 1 2 3 4 5 6[/red]
Some.File.Name.S06e01.Sdtv.X264-Sonarr-1.m4v
Some.File.Name.S06e02.Sdtv.X264-Sonarr-1.m4v
Some.File.Name.S06e03.Sdtv.X264-Sonarr-1.m4v
Some.File.Name.S06e04.Sdtv.X264-Sonarr-1.m4v[/pre]

And you want to end up with just:[pre]
S06e01.mkv
S06e02.mkv
S06e03.mkv
S06e04.mkv[/pre]

OK, a simple VBA code in Excel:
Code:
Option Explicit

Sub RenameFilesDik()
Const MyPath As String = [blue]"C:\Test7\"[/blue]
Dim strFName As String
Dim strNewFile As String

strFName = Dir(MyPath)
Do While Len(strFName) > 0
    strNewFile = Split(strFName, ".")([red]3[/red])
    Name MyPath & strFName As MyPath & strNewFile & ".mkv"
    strFName = Dir
Loop

End Sub

If you want me to, I can explain the code line-by-line, but it simply: takes one file at the time from a specific folder, splits file name by a period, takes the [red]3rd[/red] element of that file name and renames it with the extension of [tt].mkv[/tt]
The number of characters/numbers between the periods in the file names does not matter.
[wiggle]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Thanks, gentlemen...
 
You can do that as well with a self written BAT script. Here is an example:

rename_files.bat
Code:
@echo off
setlocal EnableDelayedExpansion
for %%f in (*.m4v) do (
  set fname=%%f
  for /f "tokens=4 delims=." %%s in ("!fname!") do set fname_part=%%s
  set fname_new=!fname_part!.mkv
  echo renaming "!fname!" to "!fname_new!"
  rename !fname! !fname_new!
)

On the beginning I have placed in my directory these 4 files and my BAT file (see above).
Code:
c:\tmp\renaming_example>dir /b *
rename_files.bat
Some.File.Name.S06e01.Sdtv.X264-Sonarr-1.m4v
Some.File.Name.S06e02.Sdtv.X264-Sonarr-1.m4v
Some.File.Name.S06e03.Sdtv.X264-Sonarr-1.m4v
Some.File.Name.S06e04.Sdtv.X264-Sonarr-1.m4v

Then I run the BAT file, which echoes following output:
Code:
c:\tmp\renaming_example>rename_files.bat
renaming "Some.File.Name.S06e01.Sdtv.X264-Sonarr-1.m4v" to "S06e01.mkv"
renaming "Some.File.Name.S06e02.Sdtv.X264-Sonarr-1.m4v" to "S06e02.mkv"
renaming "Some.File.Name.S06e03.Sdtv.X264-Sonarr-1.m4v" to "S06e03.mkv"
renaming "Some.File.Name.S06e04.Sdtv.X264-Sonarr-1.m4v" to "S06e04.mkv"

After that I have in my direcory 4 renamed files
Code:
c:\tmp\renaming_example>dir /b *
rename_files.bat
S06e01.mkv
S06e02.mkv
S06e03.mkv
S06e04.mkv
 
Other option on Windows would be to use VBscript or Power Shell.
By the way, does anybody know whether VBscript is still available on Windows 11 or no more ?
 
For the sake of completeness I tried to write it in PowerShell which is nowadays a preferred scripting language of Microsoft. I'm not new to it (I wrote something in it a couple of years ago), but looking at it now I have to say that PowerShell is such an ugly language unlike Linux shell (Bash) or any other scripting language that I no longer enjoy dealing with it at all. IMO, if Windows is going to survive these days, it needs to be shipped with a regular command shell, not that s**t.
 
Thanks mikrom... I'm just in the process of reinstalling the apps and files on my repaired machine (had a ransomware virus); I'll do that later today or on the weekend.
 
Not struggling with it... just busy and getting around to it. I just reinstalled Windows and will load up my apps. I've already done a restore point and once I get my programs loaded will do another and once my data is loaded, I'll do a Macrium clone. Something to do this weekend... I'm really happy that I had swapped out the one 10TB drive a week earlier, else could have been a real problem... could have lost 30 years of engineering work. Thanks...

Dik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top