Solved

Batch file

Posted on 2013-11-14
7
402 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
[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
  • 4
  • 3
7 Comments
 
LVL 25

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 25

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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 25

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

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