Solved

Batch file (modification)

Posted on 2014-03-20
9
396 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 51

Accepted Solution

by:
Bill Prew earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
You need to remove 'Const', like this:
MyZip = "C:\00_BatchProcessing\C_Testing_OutputFile\Test_" & TimeStamp() & ".zip"

Open in new window

0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 24

Assisted Solution

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

Open in new window

0
 

Author Comment

by:ExpExchHelp
Comment Utility
PERFECT!!!!
0
 

Author Closing Comment

by:ExpExchHelp
Comment Utility
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 51

Expert Comment

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

~bp
0
 

Author Comment

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

EEH
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now