Solved

Batch file (modification)

Posted on 2014-03-20
9
405 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 55

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 25

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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 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 55

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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