Solved

Batch file

Posted on 2013-11-14
7
400 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 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
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.

 

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

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

Suggested Solutions

Title # Comments Views Activity
CMD BAT file script SET / Delayed Expansion issue in FOR/DO construct 4 49
batch script 3 49
Bat File Script - HELP 2 34
Autorun Remote Desktop - USB Stick 6 63
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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