Jump to content

Stupid command line question ...


dr.zick

Recommended Posts

Sorry, if this is off-topic but I tried googling it and couldn't find an answer. Is there a way to use the windows command prompt to mass rename a bunch of files, mainly photos from a vacation, but I have other things that I would like to do. My camera names the files DSC-xxxx.jpg ... Can I remove the DSC-x and keep the last 3 digits and the .jpg at all. I don't want to have to manually rename every file if I don't have to. Thanks for the help.

Link to comment
Share on other sites


Try this vb script, copy all this code into notepad and save as renamer.vbs or whatever (have "all files" selected in the save dialogue).

Run the script and choose the folder where your pictures are, if they are on the desktop you will need to navigate in the folder dialogue to the full path i.e

C:\Documents and Settings\Owner\Desktop\New Folder etc... or whatever

So this script only works on jpg files or ".jpg" to be precise..... And will only remove the first 5 characters in the filename i.e DSC-x and leave the rest..

dim fso
dim folder
dim file
dim fname
dim ObjFSO
dim folderpath

set fso = createobject("Scripting.FilesystemObject")
set objShell = CreateObject("Shell.Application")

set objFolder = objShell.BrowseForFolder(0, "Example", 0, ssfAll)

if objFolder is nothing then

Wscript.Echo "Script Error: Please select a folder!"
Wscript.Quit

end if

folderpath = objFolder.ParentFolder.ParseName(objFolder.Title).Path & "\"


set folder = fso.GetFolder(folderpath)

for each file in folder.files
if right(file.name,3) = "jpg" then

fname = mid(file.name,6,(len(file.name)-5))

fso.movefile folderpath & file.name,folderpath & fname

end if
next

Link to comment
Share on other sites

Here is my script, you have to Drag and drop the folder with the files you want to rename.

This replace the first 5 characters, dsc-x, then copies the file with the new name then deletes

the old file. I also included a text report of what files where changed.

Save As ReName.vbs

Option Explicit 
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Dsk :Dsk = Act.SpecialFolders("DeskTop") & "\"
Dim A1, C1, F1, F2, F3, F4, PF, Ts
'-> Make Sure We Drag And Drop Some Thing
If Wscript.Arguments.Count = 0 Then
MsgBox "This Needs A Folder To Be Drop" & vbCrLf & _
"And Drop On To This Script.",4128, "Error 1"
WScript.Quit(0)
Else
For Each F1 In WScript.Arguments
'-> Make Sure It A Folder We Are Using
If Right(InStr(F1,"."),5) Then
MsgBox "Error This Is A Single File, Please" & vbCrLf & _
"Drag A Folder On To This Script",4128, "Error 2"
WScript.Quit(1)
Else
'-> Get The Folder Path
Set PF = Fso.GetFolder(F1)
'-> Collect The Files In The Folder
For Each F2 In PF.Files
A1 = 0
'-> Filter Out The Files To Be Renamed
If InStr(LCase(F2.Name),LCase("dsc")) Then
A1 = 1
C1 = Left(F2.Name,5)
C1 = Replace(F2.Name,C1,"")
F4 = F4 & _
" Old File Name " & F2.Path & vbCrLf & _
" New File Name " & Replace(F2.Path,F2.Name,C1) & vbCrLf & _
" Copy Time " & Time & vbCrLf
Fso.CopyFile F2.Path, Replace(F2.Path,F2.Name,C1), True
Fso.DeleteFile(F2.Path),true
End If
Next
End If
Next
'-> Build The Text Report
If A1 = 1 Then
Set Ts = Fso.CreateTextFile(Dsk & PF.Name & "_Rename.txt")
Ts.WriteLine " Folder Path " & PF.Path & vbCrLf
Ts.WriteLine F4
Ts.Close
Act.Run(Chr(34) & Dsk & PF.Name & "_Rename.txt" & Chr(34)),1,True
End If
'-> No Files Where Renamed
If A1 = 0 Then MsgBox "No Files Where Renamed", 4128, "No Files Renamed"
End If

Link to comment
Share on other sites

Whilst this is a relatively easy task to perform in [batch file | command line] I think you should change tack with your task. Once you change the auto-naming sequence and in time remove files etc. even the numbering serves little purpose.

I'd suggest thinking about, simply taking every .jpg in foldername and renaming them all sequentially, (say 001.jpg - 999.jpg). This would also rule out any possibility of overwriting files ending with the same three digits too.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...