Jump to content

Change File Name Case


Recommended Posts


Here is a script I wrote up it does

1:\ Runs in Cscript

2:\ Can have Manual Input

3:\ Drag And Drop Folder

4:\ uses Wmi to rename files

5:\ Save Results


Option Explicit
'-> Objects
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=" & _
"impersonate}!\\.\root\cimv2")
'-> Varibles
Dim A1, Arg, C1, Col, errResult, File, L
Dim Name, Obj, Path, Rpt, Str, T, Ts, Z
A1 = Chr(160) & Chr(187) & Chr(160)
L = "----------------------------------------------------"
'-> Make Sure Correct Engine Cscript Is Active
If InStr(1,WScript.FullName,"cscript",1) Then
UserInput()
ElseIf InStr(1,WScript.FullName,"wscript",1) Then
T = Space(14) & A1 & "ERROR : Wrong Scripting Engine"
T = T & vbCrLf
T = T & "This Script Was Ment To Be Run From The Cscript Engine."
T = T & vbCrLf
T = T & "Right Click The Script, Select The Cmd Prompt To Open It"
Msgbox T,4128,"Error Wrong Script Engine"
WScript.Quit
End If
'-> Get The Folder Path Either Drag Drop Or Manual Input.
Function UserInput()
Do While Str = ""
WScript.StdOut.WriteLine L
WScript.StdOut.WriteLine "This script was ment to rename all the Files in a"
WScript.StdOut.WriteLine "Folder,and all sub folder to lowercase lettering"
WScript.StdOut.WriteLine " 1:\ Type in the Full Path to the Folder"
WScript.StdOut.WriteLine " 2:\ Drag And Drop Folder On This Window"
WScript.StdOut.WriteLine " 3:\ Press Enter To Continue Script"
WScript.StdOut.WriteLine " 4:\ Type Exit Or Quit To Do Nothing"
WScript.StdOut.WriteLine L & vbCrLf
If Wscript.Arguments.Count = 1 Then
For Each Arg In WScript.Arguments
'-> Separate Files From Folders After Drag And Drop
If Right(InStr(Arg,"."),3) Or _
Right(InStr(Arg,"."),4) Or _
Right(InStr(Arg,"."),5) Then
Else
Str = Arg
End If
Next
End If
Str = Wscript.StdIn.ReadLine
If InStr(1,Str,"Exit",1) Or InStr(1,Str,"quit",1) Then WScript.Quit
If Len(Str) >= 4 And Fso.FolderExists(Str) Then
WScript.StdOut.WriteLine L
ReNameRecursive(Fso.GetFolder(Str))
ElseIf Len(Str) = 3 Or Len(Str) = 2 Or Len(Str) = 1 Then
call MyError(_
"Error Can Not Run Scan From Root Dir","Root Drive Error : " & Str)
ElseIf Not Fso.FolderExists(Str) Then
call MyError( _
"This Folder Does Not Exists Error", "Missing : " & Str )
End If
Loop
End Function
'-> Function To Output Error Messages
Function MyError(A,B)
WScript.StdOut.WriteBlankLines 3
WScript.StdOut.WriteLine A
WScript.StdOut.WriteLine B
WScript.StdOut.WriteLine "Reloading User Input"
WScript.Sleep 3000
WScript.StdOut.WriteBlankLines 3
Str = ""
UserInput()
End Function
'-> Rename All Files In Parent Folder And All Sub
Function ReNameRecursive(Folder)
Set File = Wmi.ExecQuery("ASSOCIATORS OF " & _
"{Win32_Directory.Name='" & Folder &"'} " & _
"Where ResultClass = CIM_DataFile")
C1 = C1 + File.Count
For Each Obj In File
WScript.StdOut.WriteLine _
"Processing" & A1 & Obj.FileName & vbCrLf & L
Path = Obj.Drive & Obj.Path
Name = Obj.FileName & "." & Obj.Extension
Rpt = Rpt & vbCrLf & _
"Old" & A1 & Path & "\" & Name & vbCrLf & _
"New" & A1 & Path & LCase(Name) & vbCrLf & L
errResult = Obj.Rename(Path & "\" & LCase(Name))
Next
For Each Col In Folder.SubFolders
ReNameRecursive(Col)
Next
End Function
'-> Save The Scan Results
Function SaveResults()
Dim Txt :Txt = Act.SpecialFolders("Desktop") & _
"\Rename_Results.txt"
Set Ts = Fso.CreateTextFile(Txt)
Ts.WriteLine "Scan Date Time" & A1 & Date & A1 & Time
Ts.WriteLine "Total Number Changed" & A1 & C1
Ts.WriteLine L & vbCrLf & Rpt
Ts.Close
Act.Run("Notepad " & Chr(34) & Txt & Chr(34)),1,False
WScript.Quit
End Function
'-> End Of Rename
Do While Z = ""
WScript.StdOut.WriteLine "Would you like to save the Results Of Rename To"
WScript.StdOut.WriteLine "Lowercase File Names to a text file?"
WScript.StdOut.WriteLine A1 & "1:\ Type Yes To Save Rename Results"
WScript.StdOut.WriteLine A1 & "2:\ Type No To Close Window And"
WScript.StdOut.WriteLine "To Not Save The Rename Results."
Z = Wscript.StdIn.ReadLine
If InStr(1,Z,"yes",1) Then SaveResults()
If InStr(1,Z,"no",1) Then WScript.Quit
Loop

Rename ReNameToLowerCase.vbs.txt to ReNameToLowerCase.vbs to make active

ReNameToLowerCase.vbs.txt

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...