Solved

Batch file (modification)

Posted on 2014-03-20
9
401 Views
Last Modified: 2014-03-20
Hello experts:

I recently posted a question on creating a batch file that would a) zip a folder structure & associated files and then b) move the .zip file into a specified directory.

Expert "chaau" provided an excellent solution.  See http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28294665.html#a39649505
for details.   Also, code (developed by expert 'chaau' is provided below).

At this time, I need to slightly modify the script so that it would append a date-time-stamp (DTS) to the .zip file name.  

For example, if the batch is executed on, e.g., March 20 @ 2:50 pm, I would like the filename to be as follows:  "Test_2014_03_15_1450.zip".     That said, the DTS "extension" must be dynamic (pulled from computer system time).

Note: If the date format will be different, that's fine as well.  Primary goal is to be able to add the DTS after the "MPA" filename.

Is that doable?   If yes, how should the code be modified to accommodate this task?

EEH


Const FOF_CREATEPROGRESSDLG = &H0&

Const MyZip = "c:\MyFolder\Test.zip"

Const File1 = "c:\Mainfolder_XYZ\"


'-------------- create empty zip file ---------

'Create the basis of a zip file.
CreateObject("Scripting.FileSystemObject") _
.CreateTextFile(MyZip, True) _
.Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)


'-------------- zip ---------------------------

'get ready to add files to zip
'
Dim oShell
Set oShell = CreateObject("Shell.Application")

'add files
oShell.NameSpace(MyZip).CopyHere oShell.NameSpace(File1).Items, FOF_CREATEPROGRESSDLG

wScript.Sleep 1000

' Delete files from sub-folders
Dim fso, vfolder, subFlds, fld, file, vfolderContents
Dim 
Set fso         = CreateObject("Scripting.FileSystemObject")
Set vfolder  = fso.GetFolder(File1)
Set subFlds = vfolder.SubFolders
For Each fld In subFlds
  Set vfolderContents = fld.Files
  For Each file In vfolderContents
    fso.DeleteFile(file)
  Next
  Set vfolderContents = Nothing
Next

wscript.echo "Done!"
                                            

Open in new window

0
Comment
Question by:ExpExchHelp
  • 5
  • 2
  • 2
9 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 300 total points
ID: 39943573
Here is a relatively simple approach, just using a function that formats the date and time however you want it, and adding it to the file name.

Const FOF_CREATEPROGRESSDLG = &H0&

Const MyZip = "c:\MyFolder\Test_" & TimeStamp() & ".zip"

Const File1 = "c:\Mainfolder_XYZ\"


'-------------- create empty zip file ---------

'Create the basis of a zip file.
CreateObject("Scripting.FileSystemObject") _
.CreateTextFile(MyZip, True) _
.Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)


'-------------- zip ---------------------------

'get ready to add files to zip
'
Dim oShell
Set oShell = CreateObject("Shell.Application")

'add files
oShell.NameSpace(MyZip).CopyHere oShell.NameSpace(File1).Items, FOF_CREATEPROGRESSDLG

wScript.Sleep 1000

' Delete files from sub-folders
Dim fso, vfolder, subFlds, fld, file, vfolderContents
Dim 
Set fso         = CreateObject("Scripting.FileSystemObject")
Set vfolder  = fso.GetFolder(File1)
Set subFlds = vfolder.SubFolders
For Each fld In subFlds
  Set vfolderContents = fld.Files
  For Each file In vfolderContents
    fso.DeleteFile(file)
  Next
  Set vfolderContents = Nothing
Next

wscript.echo "Done!"
                                            
Function TimeStamp()
   TimeStamp = Year(Now) & "_" & Right("0" & Month(Now), 2) & "_" &  Right("0" & Day(Now), 2) & "_" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2)
End Function

Open in new window

~bp
0
 

Author Comment

by:ExpExchHelp
ID: 39943891
bp:

Thank you for the response... I appreciate it.    I've changed/added the following lines of code.

At this time, when executing the batch file, I get an error (see attached JPG).  Did I miss some lines?    Or is the actual file patch too long/incorrect?

Again, thank you for your help.
EEH

*********************

Changed line from:
Const MyZip = "C:\00_BatchProcessing\C_Testing_OutputFile\Test.zip"

... to:
Const MyZip = "C:\00_BatchProcessing\C_Testing_OutputFile\Test_" & TimeStamp() & ".zip"


Added following lines (bottom of code):
'----- Obtains date-time-stamp from system clock
Function TimeStamp()
   TimeStamp = Year(Now) & "_" & Right("0" & Month(Now), 2) & "_" &  Right("0" & Day(Now), 2) & "_" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2)
End Function
Error.jpg
0
 
LVL 24

Assisted Solution

by:chaau
chaau earned 200 total points
ID: 39943946
You need to remove 'Const', like this:
MyZip = "C:\00_BatchProcessing\C_Testing_OutputFile\Test_" & TimeStamp() & ".zip"

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ExpExchHelp
ID: 39943967
chaau:

Thanks for chiming in... oh my, I totally overlooked that.   Thanks for the suggestion.   Works great.

Final question (to both you and bp).   Given the added date-time-stamp, is there an >> easy way (i.e., just a slight code modification) so that the dialog box message also includes the correct file name?

As shown below, right now it outputs "Test.zip" in the message.   Would be great to also show "Test_2014_03_20_1820.zip".   Doable?

EEH


'----- Display Confirmation Message -----
wscript.echo "Test.zip has been created and moved to the specified network directory!"
0
 
LVL 24

Assisted Solution

by:chaau
chaau earned 200 total points
ID: 39943982
yes:
wscript.echo MyZip & " has been created and moved to the specified network directory!"

Open in new window

0
 

Author Comment

by:ExpExchHelp
ID: 39943988
PERFECT!!!!
0
 

Author Closing Comment

by:ExpExchHelp
ID: 39943996
bp -- thank you for providing an excellent solution!!!!   I truly appreciate it.

chaau -- thank you for chiming in and helping me fix the error (as well as the modification to the output message).

Again, thank you for the great help!!!

EEH
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39944102
Sorry for the delay, was away on real work, but glad you got by those bumps.

~bp
0
 

Author Comment

by:ExpExchHelp
ID: 39944172
~bp -- thank you... 'appreciate your assistance  & understanding (about splitting points).  ;)

EEH
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question