Solved

Batch file

Posted on 2013-11-14
7
388 Views
Last Modified: 2014-03-20
Hello Experts:

EE expert chaau posted a great VBS script which allows to zip up a folder structure and then move the newly created zip to a specified directory.

His recommended code (which works great btw) is posted below:

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

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

wscript.echo "Done!"

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


After the folder/subfolder/files have been zipped and moved, I'd like to "clean" the source directory.   That is, I'd like to delete all files from the root directory and any subfolder/sub-subfolders.    I do NOT, however, want to delete the folders/subfolders.

How can the VBS be modified to accomplish this task?

Thanks,
EEH
0
Comment
Question by:ExpExchHelp
  • 4
  • 3
7 Comments
 
LVL 24

Assisted Solution

by:chaau
chaau earned 500 total points
ID: 39649505
Using the same script, add the following lines:

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
 

Author Comment

by:ExpExchHelp
ID: 39649543
chaau:

Almost there!   Great!

I had to remove "Dim" on line 30.   I then executed and moved the zip file.

When I checked the source directory, the subfolders were empty; however, any files on the directory's root level were still there.

How can the code be tweaked to include those that that directly on the folder level (i.e., same level as the subfolders)?

EEH
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39649562
Add this chunk of code before the last line with "Done" (after the last Next):

  Set vfolderContents = vfolder.Files
  For Each file In vfolderContents
    fso.DeleteFile(file)
  Next
  Set vfolderContents = Nothing

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Closing Comment

by:ExpExchHelp
ID: 39649576
MOST PERFECT SOLUTION!!!   Thank you, chaau.  ;)

EEH
0
 

Author Comment

by:ExpExchHelp
ID: 39943293
I have a follow-on question on the posting above (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28294665.html#a39649505)

How can add a date-time-stamp to the output file "MPA.zip"?

So, if this batch is processed on, e.g., March 20 @ 3:15 pm, I would like the filename to be as follows:   "MPA_2014_03_15-1515.zip".     If the date format will be different, that's fine as well.   Goal is to be able to add date and time to the file.

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

EEH
0
 
LVL 24

Expert Comment

by:chaau
ID: 39943783
I can somebody has already provided you with a solution in another question. I think their solution is great. I hope it will help you in your project
0
 

Author Comment

by:ExpExchHelp
ID: 39943932
chaau:

Thanks... I've tried bp's solution.   At this time though, it doesn't execute.  

I'm sure the error is mine.   I'd welcome if you could chime in (since it's your original code).

Thanks,
EEH
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

17 Experts available now in Live!

Get 1:1 Help Now