user2922926
user2922926

Reputation: 53

Add formatted current date to file name

I want to zip a file using vb6.0 code. The following code isactually doing the zipping part.

How can I add the current date to the file name?

Sub ZipAFile(filename, filepath, Optional outputFileName, Optional outputfilepath)

    Dim d As String
    filename = ABC.xls
    filepath = App.Path & "\" & "New_Folder\"
    d = Date

    If Right(filepath, 1) <> "\" Then filepath = filepath & "\"
    If IsMissing(outputFileName) Then
        outputFileName = Left(filename, InStr(1, filename, ".") - 1) & ".zip"
    Else
        outputFileName = Trim(outputFileName) & ".ZIP"
    End If
    If IsMissing(outputfilepath) Then
        outputfilepath = filepath
    Else
        outputfilepath = Trim(outputfilepath)
        If Right(outputfilepath, 1) <> "\" Then outputfilepath = outputfilepath & "\"
    End If
    Shell "C:\Program Files\7-Zip\7z.exe a """ & outputFileName & """ """ & outputfilepath

End Sub

Upvotes: 1

Views: 3602

Answers (2)

tbur
tbur

Reputation: 2454

It is possible to create a zip file without third party software (in your case 7zip.) Just an FYI.

Sub zip_a_file(path_to_target As String, _
           file_with_extension_to_compress As String, _
           zip_file_path As String, _
           zip_file_name_without_extension As String)

Dim ZipFile
Dim o As Object
Dim d As String

'First, do a little bit of testing to see if the inputs are OK

If Right(path_to_target, 1) <> "\" Then
    path_to_target = path_to_target & "\"
End If

If Right(zip_file_path, 1) <> "\" Then
    zip_file_path = zip_file_path & "\"
End If

'Get the date and format it

d = Format(Now, "yyyymmdd")

'Create Empty Zip Package

ZipFile = zip_file_path & zip_file_name_without_extension & d & ".zip"
Open ZipFile For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1

'Do some zippin'

Set o = CreateObject("Shell.Application")
o.Namespace(ZipFile).CopyHere (path_to_target & file_with_extension_to_compress)

'Clean up

Set o = Nothing

End Sub

Upvotes: 1

C-Pound Guru
C-Pound Guru

Reputation: 16348

You can change the format of the date however you see fit using the Format$ function:

Sub ZipAFile(filename, filepath, Optional outputFileName, Optional outputfilepath)

    Dim d As String
    filename = ABC.xls
    filepath = App.Path & "\" & "New_Folder\"
    d = Format$(Now, "yyyymmdd") '// Change date formatting here

    If Right(filepath, 1) <> "\" Then filepath = filepath & "\"
    If IsMissing(outputFileName) Then
        outputFileName = Left(filename, InStr(1, filename, ".") - 1) & d & ".zip" '// Append your date string
    Else
        outputFileName = Trim(outputFileName) & d & ".ZIP" '// Append your date string
    End If
    If IsMissing(outputfilepath) Then
        outputfilepath = filepath
    Else
        outputfilepath = Trim(outputfilepath)
        If Right(outputfilepath, 1) <> "\" Then outputfilepath = outputfilepath & "\"
    End If
    Shell "C:\Program Files\7-Zip\7z.exe a """ & outputFileName & """ """ & outputfilepath

End Sub

Upvotes: 3

Related Questions