Hi!
Here's the updated script. Now the date and time always is placed before the first dot in the filename, like this.
MyArchive_YYYYMMDD_HHMMSS.properties.txt.tar.bz2
I have also put a check if PACOMP.exe exists in the specified path of the constant. If not, a message will tell you about it and the script will exit.
Code:
'***********************************************************************
'* PACOMP_DT.vbs
'* @author: Micke
'* @hist 2010-04-05 CREATED:Script for adding Date and Time to Filename
'* @hist 2010-04-06 BUGFIX: Assumed the file extension always contains
'* 3 character, resulting in incorrect
'* Filename when using a file extension with
'* other than 3 characters.
'* @hist 2010-04-06 UPDATE: Added support for different dateformat
'* @hist 2010-04-08 UPDATE: Added check for existing PACOMP in the
'* specified path
'* @hist 2010-04-08 UPDATE: Moved date and time to be placed before
'* the first dot in the filename
'*
'* The script requires 4 parameters
'* Usage: cscript PACOMP_DT.vbs <param1> <param2> <param3> <param4>
'*
'* param1: -d1 = Add current date (YYYYMMDD) to filename
'* -d2 = Add current date (DDMMYYYY) to filename
'* -d3 = Add current date (MMDDYYYY) to filename
'* -d = Works the same as -d1
'* -t = Add current time to filename
'* -dt = Add current date and time to filename
'*
'* param2: The commands PACOMP uses
'* param3: Filename of the Archive with extension
'* param4: Path to file(s) and/or directory
'*
'* Add date and time to Archivename MyArchive.zip
'* Example: cscript PACOMP_DT.vbs -dt -a C:\Temp\MyArchive.zip D:\*.doc
'*
'***********************************************************************
Option Explicit
'Variables
Dim FSO, WshShell, objRegEx, strDate, strDT_Parameter, strArchiveExtension
Dim strTime, strArchiveNamePath, strPACOMP_Commands, strFileName
Dim strOnlyArchiveName, strPath, strOnlyArchiveNameNoExtension
Dim strCompressionString, bCheck
'Constants
Const PACOMP = "C:\Programs\PACL\PACOMP.exe"
'Check number of arguments
If WScript.Arguments.Count <> 4 Then
WScript.Echo "Usage: cscript PACOMP_DT.vbs <param1> <param2> <param3> <param4>"
WScript.Echo "param1: -d1 = Add current date (YYYYMMDD) to filename"
WScript.Echo "param1: -d2 = Add current date (DDMMYYYY) to filename"
WScript.Echo "param1: -d3 = Add current date (MMDDYYYY) to filename"
WScript.Echo "param1: -d = Works the same as -d1"
WScript.Echo "param1: -t = Add current time to filename"
WScript.Echo "param1: -dt = Add current date and time to filename"
WScript.Echo "param2: The commands PACOMP uses"
WScript.Echo "param3: Filename of the Archive with extension"
WScript.Echo "param4: Path to file(s) and/or directory"
WScript.Echo "Example: cscript PACOMP_DT.vbs -dt -a C:\Temp\MyArchive.zip D:\*.doc"
WScript.Echo "Will add date and time to Archivename MyArchive.zip"
WScript.Quit
End If
'Create the FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
'Create the ShellObject
Set WshShell = CreateObject("WScript.Shell")
'Get data from the Arguments
strDT_Parameter = WScript.Arguments.Item(0)
strPACOMP_Commands = WScript.Arguments.Item(1)
strArchiveNamePath = WScript.Arguments.Item(2)
strFileName = WScript.Arguments.Item(3)
'Check that PACOMP exists in the specified path
If FSO.FileExists(PACOMP) Then
WScript.Echo "PACOMP.exe found, ok to continue the script"
Else
WScript.Echo "PACOMP.exe could not be found in the specified path"
WScript.Echo "The path is set to: " & PACOMP
WScript.Echo "Update the constant PACOMP to contain a valid path"
WScript.Echo "Exit the script"
WScript.Quit
End If
'Check the parameter for Date and Time
If CheckDTParameter() = True Then
UpdateArchiveName()
RunPACOMP()
Else
WScript.Echo "Parameter 1 incorrect"
WScript.Echo "Value set as: " & strDT_Parameter
WScript.Echo "Expected value would be: -t, -d, -d1, -d2,-d3, -dt, -d1t, -d2t, -d3t"
WScript.Echo "Exit the script"
WScript.Quit
End If
Sub UpdateArchiveName()
'Get the ArchiveName from the path
strOnlyArchiveName = Mid(strArchiveNamePath, InStrRev(strArchiveNamePath, "\")+1)
'Get the ArchiveExtension
strArchiveExtension = Mid(strArchiveNamePath, InStr(strArchiveNamePath, ".")+1)
'Get the ArchiveName without extension
strOnlyArchiveNameNoExtension = Left(strOnlyArchiveName, Len(strOnlyArchiveName)-Len(strArchiveExtension)-1)
'Get the Path without the ArchiveName
strPath = Left(strArchiveNamePath,Len(strArchiveNamePath)-Len(strOnlyArchiveName))
'Insert Date and Time to the ArchiveName
If strDT_Parameter = "-d" Or strDT_Parameter = "-d1" Or strDT_Parameter = "-d2" Or strDT_Parameter = "-d3" Then
strOnlyArchiveName = strOnlyArchiveNameNoExtension & "_" & strDate & "." & strArchiveExtension
ElseIf strDT_Parameter = "-t" Then
strOnlyArchiveName = strOnlyArchiveNameNoExtension & "_" & strTime & "." & strArchiveExtension
Else
strOnlyArchiveName = strOnlyArchiveNameNoExtension & "_" & strDate & "_" & strTime & "." & strArchiveExtension
End If
strArchiveNamePath = strPath & strOnlyArchiveName
End Sub
Sub RunPACOMP()
'Create the string with parameters for PACOMP
strCompressionString = PACOMP & " " & strPACOMP_Commands & " " & Chr(34) & strArchiveNamePath & Chr(34) & " " & Chr(34) & strFileName & Chr(34)
WScript.Echo strCompressionString
WshShell.Run strCompressionString, 0, True
End Sub
Function FormatNumbers(n, totalDigits)
If totalDigits > Len(n) Then
FormatNumbers = String(totalDigits-Len(n),"0") & n
Else
FormatNumbers = n
End If
End Function
Function CheckDTParameter()
Select Case strDT_Parameter
Case "-d" 'YYYYMMDD
bCheck = True
strDate = Year(Date()) & FormatNumbers(Month(Date()),2) & FormatNumbers(Day(Date()),2)
Case "-d1" 'YYYYMMDD
bCheck = True
strDate = Year(Date()) & FormatNumbers(Month(Date()),2) & FormatNumbers(Day(Date()),2)
Case "-d2" 'DDMMYYYY
bCheck = True
strDate = FormatNumbers(Day(Date()),2) & FormatNumbers(Month(Date()),2) & Year(Date())
Case "-d3" 'MMDDYYYY
bCheck = True
strDate = FormatNumbers(Month(Date()),2) & FormatNumbers(Day(Date()),2) & Year(Date())
Case "-t" 'HHMMSS
bCheck = True
GetFormattedTime()
Case "-dt" 'YYYYMMDD HHMMSS
bCheck = True
strDate = Year(Date()) & FormatNumbers(Month(Date()),2) & FormatNumbers(Day(Date()),2)
GetFormattedTime()
Case "-d1t" 'YYYYMMDD HHMMSS
bCheck = True
strDate = Year(Date()) & FormatNumbers(Month(Date()),2) & FormatNumbers(Day(Date()),2)
GetFormattedTime()
Case "-d2t" 'DDMMYYYY HHMMSS
bCheck = True
strDate = FormatNumbers(Day(Date()),2) & FormatNumbers(Month(Date()),2) & Year(Date())
GetFormattedTime()
Case "-d3t" 'MMDDYYYY HHMMSS
bCheck = True
strDate = FormatNumbers(Month(Date()),2) & FormatNumbers(Day(Date()),2) & Year(Date())
GetFormattedTime()
Case Else
bCheck = False
End Select
CheckDTParameter = bCheck
End Function
Sub GetFormattedTime()
'Clear unwanted characters from the date and time
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "[^0-9]"
'Find out current time
strTime = FormatDateTime(Time(), vbLongTime)
'Clear unwanted characters from the date and time
strTime = objRegEx.Replace(strTime, "")
End Sub
Kind Regards
Micke