?
Solved

Batch file (modification)

Posted on 2014-03-20
9
Medium Priority
?
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 56

Accepted Solution

by:
Bill Prew earned 1200 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 25

Assisted Solution

by:chaau
chaau earned 800 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 25

Assisted Solution

by:chaau
chaau earned 800 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 56

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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