Solved

Batch file (modification)

Posted on 2014-03-20
9
403 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Changing Audit Policies through scripting 5 69
For loop to add numbers 3 85
Batch script to move disabled user accounts to different OU 5 98
Recursively Delete Files 5 93
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

790 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