Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

Batch file

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
ExpExchHelp
Asked:
ExpExchHelp
  • 4
  • 3
2 Solutions
 
chaauCommented:
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
 
ExpExchHelpAuthor Commented:
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
 
chaauCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ExpExchHelpAuthor Commented:
MOST PERFECT SOLUTION!!!   Thank you, chaau.  ;)

EEH
0
 
ExpExchHelpAuthor Commented:
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
 
chaauCommented:
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
 
ExpExchHelpAuthor Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now