Share via

VB Script Dashes in File Name

Anonymous
2015-09-17T23:05:13+00:00

I have the below backup VB script and it works great!

I would like to modify the Date and Time Stamp so that there is a dash between the Year month date hour and remove the seconds.

How do I modify the below to do this? I am really novice at VB.

'http://www.devhut.net/2013/11/15/vbscript-backup-a-file-and-add-a-date-time-stamp/

'

' This VB file needs to be configured for the source file and the destination folder

' See sSourceFolder, sBackupFolder, sDBFile and sDBFileExt variables and modify as necessary.

'9/17/2015, Iram Alvarez

'This file with make a backup copy with the addition of a Date Time stamp

'Author: Daniel Pineault

Dim objFSO

Dim sSourceFolder

Dim sDestFolder

Dim sDBFile

Dim sDateTimeStamp

Const OVER_WRITE_FILES = True

Set objFSO = CreateObject("Scripting.FileSystemObject")

'                      sSourceFolder = "C:\Databases\Test"                         ====>This was the orginal setting

'                      sBackupFolder = "C:\Users\Daniel\Desktop\New folder (3)"    ====>This was the orginal setting

'                      sDBFile = "Test"                                            ====>This was the orginal setting

'                      sDBFileExt = "mdb"                                          ====>This was the orginal setting

sSourceFolder = "C:\Users\Iram\Desktop"

sBackupFolder = "C:\Users\Iram\Dropbox\Custom Office Data"

sDBFile = "LLDM_Asistencias_Censo_Estadisticas_Santa Maria"

sDBFileExt = "accdb"

sDateTimeStamp = cStr(Year(now())) & _

                 Pad(cStr(Month(now())),2) & _

                 Pad(cStr(Day(now())),2) & _

                 Pad(cStr(Hour(now())),2) & _

                 Pad(cStr(Minute(now())),2) & _

                 Pad(cStr(Second(now())),2)

'If the backup folder doesn't exist, create it.

If Not objFSO.FolderExists(sBackupFolder) Then

    objFSO.CreateFolder(sBackupFolder)

End If

'Copy the file as long as the file can be found

If objFSO.FileExists(sSourceFolder & "" & sDBFile & "." & sDBFileExt) Then

    objFSO.CopyFile sSourceFolder & "" & sDBFile & "." & sDBFileExt,_

                    sBackupFolder & "" & sDBFile & "_" & sDateTimeStamp & "." & sDBFileExt,_

                    OVER_WRITE_FILES

End if

Set objFSO = Nothing

Function Pad(CStr2Pad, ReqStrLen)

'Source: http://saltwetbytes.wordpress.com/2012/10/16/vbscript-adding-datetime-stamp-to-log-file-name/

    Dim Num2Pad

    Pad = CStr2Pad

    If len(CStr2Pad) < ReqStrLen Then

        Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0")

        Pad = Num2Pad & CStr2Pad

    End If

End Function

Thanks.

Iram

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-09-18T00:38:04+00:00

Untested, but if I understood what you want it would be something along these lines:

sDateTimeStamp = cStr(Year(now())) & "-" & _

                  Pad(cStr(Month(now())),2) & "-" & _

                  Pad(cStr(Day(now())),2) & "-" & _

                  Pad(cStr(Hour(now())),2) & _

                  Pad(cStr(Minute(now())),2)

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-09-17T23:42:23+00:00

    Sorry, Iram - I posted assuming (incorrectly) that you were using Microsoft Access VBA, rather than VBScript. I'm not experienced in VBScript and I can't suggest how to recast your code. 

    Anyone able to jump in and help?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-09-17T23:35:40+00:00

    I made the change just as you mentioned but now when I run the VB script I get the following error....

    Script: C:\Users\Iram\Dropbox\Custom Office Data\Access backup Used with Task Scheduler.vbs

    Line: 31

    Char: 1

    Error: type mismatch: 'Format'

    Code: 800A000D

    Source: Microsoft VBScript runtime error

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-09-17T23:20:44+00:00

    I'd just replace

    sDateTimeStamp = cStr(Year(now())) & _

                     Pad(cStr(Month(now())),2) & _

                     Pad(cStr(Day(now())),2) & _

                     Pad(cStr(Hour(now())),2) & _

                     Pad(cStr(Minute(now())),2) & _

                     Pad(cStr(Second(now())),2)

    with 

    sDateTimeStamp = Format(Now(), "yyyy-mm-dd-hh-nn")

    This will generate a string like 2015-09-17-17-20 which will then be inserted into your filename string.

    Was this answer helpful?

    0 comments No comments