Jump to content

Recommended Posts

Posted

This script must be placed in the system search path to work. If you compile it copy the EXE into the Windows folder.

;MassRename

;Run from command prompt

;Sytnax - massrename "Path" or CD Extension Newname Count

; EG: massrename CD jpg "Singapore Holiday 1

; Result - Will rename all JPG file to Sinagpore Holiday000N.jpg

#include <Process.au3>

#include <Array.au3>

; Global Variables.

Dim $Para1

Dim $Para2

Dim $Para3

Dim $Para4

Dim $Para5

Dim $CurrentActiveDirectory = StringUpper(@WorkingDir)

Dim $FileList[1] ; Will hold all file names to be renamed.

Dim $Search ; Used to hold the extension ie: *.BMP

Main()

Func Main()

ShowHelp($Para1)

CheckDir()

EndFunc ;==>Main

;-----------------------------------------------------

Func ShowHelp(ByRef $Error)

Local $1, $Error

$Para1 = $CmdLine[0] ; No of Parameters

Local $Help1 = StringFormat("%s", "MassRename<Space>""Path""<Space>File Extension<Space>Name<Space>Start Number")

Local $Help2 = StringFormat("%s", "Path = ""C:\MyFolder\MyImages\Holiday\Blah"" OR cd for Current Directory")

Local $Help3 = StringFormat("%s", "File Extension = What group of files do you want to rename --> JPG or MPEG or WMF or BMP")

Local $Help4 = StringFormat("%s", "Name = What name do you want all the files to begin with --> NZ_Holiday or Photo or XmasParty")

Local $Help5 = StringFormat("%s", "Start Number = The very first number in the rename sequence --> 0001 or 01000 ( This will increase by 1 for each file)")

Local $Help6 = StringFormat("%s", "EG: MassRename<Space>""C:\My Documents\My Images""<Space>BMP<Space>DesktopImage<Space>001")

Local $Help7 = StringFormat("%s", "EG: This works in the current directory --> MassRename<Space>CD<Space>BMP<Space>DesktopImage<Space>001")

Local $i

; Test for the correct amount of parameters

Select

Case $Para1 = 4

$Para2 = $CmdLine[1] ; Path

$Para3 = $CmdLine[2] ; File Extension

$Para4 = $CmdLine[3] ; Name

$Para5 = $CmdLine[4] ; Count Var

$1 = StringIsInt($Para5)

If $1 = 1 Then

Return

Else

MsgBox(0, "ERROR", "The FOURTH parameter can only hold numbers - Please try again")

Exit

EndIf

Case $Para1 = 0

MsgBox(0, "Syntax", $Help1 & @CRLF & @LF & $Help2 & @CRLF & @LF & $Help3 & @CRLF & @LF & $Help4 & @CRLF & @LF & $Help5 & @CRLF & @LF & $Help6 & @CRLF & @LF & $Help7)

Exit

Case $Para1 = 3

MsgBox(0, "ERROR", "You must pass 4 parameters before I will work" & @CRLF & @LF & "MASSRENAME path file extension name start number" & @CRLF & @LF & "massrename C:\MyPhotos BMP Holiday 001 OR" & @CRLF & @LF & "CD = Current Directory" & @CRLF & @LF & "massrename CD JGP CarRace 0010" & @CRLF & @LF & "You typed" & @CRLF & @LF & $CmdLine[1] & @CRLF & $CmdLine[2] & @CRLF & $CmdLine[3])

Exit

Case $Para1 = 2

MsgBox(0, "ERROR", "You must pass 4 parameters before I will work" & @CRLF & @LF & "You typed" & @CRLF & @LF & $CmdLine[1] & @CRLF & $CmdLine[2])

Exit

$Para1 = 1

MsgBox(0, "ERROR", "You must pass 4 parameters before I will work" & @CRLF & @LF & "You typed" & @CRLF & @LF & $CmdLine[1])

Exit

Case $Para1 > 4

MsgBox(0, "Syntax", $Help1 & @CRLF & @LF & $Help2 & @CRLF & @LF & $Help3 & @CRLF & @LF & $Help4 & @CRLF & @LF & $Help5 & @CRLF & @LF & $Help6 & @CRLF & @LF & $Help7)

Exit

Case $Error = 1

MsgBox(0, "ERROR", "You must pass 4 parameters before I will work")

Exit

Case $Error = 2

MsgBox(0, "ERROR", "You must pass 4 parameters before I will work" & @CRLF & @LF & "You typed" & @CRLF & @LF & $CmdLine[1] & @CRLF & $CmdLine[2])

Exit

Case $Error = 3

MsgBox(0, "ERROR", "You must pass 4 parameters before I will work" & @CRLF & @LF & "MASSRENAME path file extension name start number" & @CRLF & @LF & "massrename C:\MyPhotos BMP Holiday 001 OR" & @CRLF & @LF & "CD = Current Directory" & @CRLF & @LF & "massrename CD JGP CarRace 0010" & @CRLF & @LF & "You typed" & @CRLF & @LF & $CmdLine[1] & @CRLF & $CmdLine[2] & @CRLF & $CmdLine[3])

Exit

EndSelect

EndFunc ;==>ShowHelp

;-----------------------------------------------------

Func CheckDir()

Local $Path

$Path = StringUpper($CmdLine[1])

Select

Case $Path = "CD"

GetNames($CurrentActiveDirectory)

Case $Path <> "CD"

GetNames($Path)

EndSelect

EndFunc ;==>CheckDir

;-----------------------------------------------------

Func Rename(ByRef $Path)

Local $i, $NewName, $Count, $Path, $Complete

$Count = UBound($FileList, 1) - 1

For $i = 1 To UBound($FileList, 1) - 1

$NewName = StringUpper(StringFormat("%s%s%04d%s%s", $Path, $Para4, $Para5, ".", $Para3))

$Complete = StringUpper(StringFormat("%s%s", $Path, $FileList[$i]))

FileMove($Complete, $NewName)

$Para5 = $Para5 + 1

Next

MsgBox(0, "FINISHED", "Process completed " & $Count & " files were renamed")

EndFunc ;==>Rename

;-----------------------------------------------------

Func GetNames(ByRef $CD)

Local $1, $File, $a, $Loop, $CD, $Error, $Path

Local $FileExtension

$Loop = 0

; Get all file names and place into an array

If $Para1 < 2 Then

$Error = $Para1

ShowHelp($Error)

Else

$CD = StringUpper($CmdLine[2])

$FileExtension = StringFormat("%s%s", "*.", $CmdLine[2])

$Path = StringFormat("%s%s", $CD, "\")

$1 = StringFormat("%s%s%s", $CD, "\", $FileExtension)

$Search = FileFindFirstFile($1)

EndIf

; Check if the search was successful=0

If $Search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While $Loop = 0

$File = FileFindNextFile($Search)

$a = StringUpper(StringRight($File, 3))

If $a = $CD Then ; Compares file extension before adding to array

_ArrayAdd($FileList, $File)

Else

$Loop = 1

EndIf

WEnd

; Close the search handle

FileClose($Search)

Rename($Path)

EndFunc ;==>GetNames

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