Link to home
Start Free TrialLog in
Avatar of ExpExchHelp
ExpExchHelpFlag for United States of America

asked on

Batch file

Experts:

I'm trying to create a batch file that does the following:

1. Upon execution, add a pre-defined folder structure (i.e., directory and/or subfolders + all their content) into a .zip file

2. Move the .zip file into a pre-defined directory on the C-drive

****** Step 1 ******
Let's say I have the following folder structure.  
The batch creates a .zip (e.g., "Test.zip") including all of their content.

Mainfolder_XYZ
Subfolder_ABC (includes various documents)
Subfolder_DEF (includes various documents)
Subfolder_GHI (includes various documents)


****** Step 2 ******

Move "Test.zip" into C:\MyFolder



Does anyone know how to create such batch file?

EEH
Avatar of MacGyver80
MacGyver80
Flag of United States of America image

What application are you trying to accomplish this with?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is how to do this with WinZip command line tool:
"C:\Program Files\WinZip\wzzip" -p -r "c:\MyFolder\Test.zip" "c:\Mainfolder_XYZ\*.*"

Open in new window

The trick is to use -p -r switches
(Please note: although WinZip is a commercial product, you can use Command line tool free of charge. You need to install the trial version of the main WinZip program though)
Avatar of ExpExchHelp

ASKER

bp:

Thanks for the prompt response... I appreciate it.

I've modified the code to replicate my (test) folder structure.
I've downloaded and installed 7-zip (once installed, however, the .exe is not called "7za.exe"... instead it's "7zFM.exe"

Upon executing the .bat file, the DOS window is open (blank) and 7-zip is open.

However, the .zip file was not moved into the destination directory.

What am I missing?   Please see attached Word document w/ some screenshots.
Modified-Code.doc
@chaau:

Thanks... my current default zip program came with Windows 7.  

I used to have WinZip but I don't think it's currently installed.  

How would I turn the single command line into a batch file (assuming I can use the default program)?

Also, per bp's recommendation, I've already installed 7-zip.

EEH
I think you need to download Command Line version from here
Avatar of Bill Prew
Bill Prew

Don't use 7zfm.exe, thats the graphical version.  Use 7z.exe.

~bp
To turn  a single command line (or a few lines) into a batch file do the following:

1. Open Notepad
2. Copy and paste the content from here to the Notepad
3. Save the file as test.bat (the main trick to use .bat extension)

This file will become a batch file that can be executed by double click from Explorer
~bp:

I've downloaded -- what appears to be 7z.exe.   Once installed though, it's back to the 7zfm.exe.

Where of their site can I find the "generic" version?   Btw, I have a 64-bit OS.
Look in the Program Files\7-Zip folder (or where ever you installed it), you should see several EXEs in there.  7z.exe is the command line version that you want to use.

~bp
check "c:\Program Files (x86)" folder. There could be 7-Zip folder there as well
~bp:

Ok... one step further.   I found the .exe and modified the code.

It executed and put the .zip file into the correct destination folder.

However, per screenshot (see Word document that I posted previously), the .zip file contains only the spreadsheet "Test.exe".    It did not include the two subfolders... i.e., "Subfolder 1" and "Subfolder 2".

Something's misses... but I'm confident we're close here.  ;)

EEH
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah... that did the trick!!!   I've added some test file into both of them.

It works like a charm!   Thousand thanks!!

EEH
chaau -- thanks for your help as well.   ~bp's solution came through first though.

Again, thanks!
Welcome, glad that was useful, thanks for the feedback.

~bp
~bp:

May I ask two follow-up questions?

1. After zipping the directory structure & content of the source folder(s) and then moving it, is there a chance to remove all files from the source directory?   No folders or subfolders, however, should be deleted... only files.

2. Some of our customers may not want (or are allowed to) install 7-zip on their network.   For testing purposes, I update the zip utility's path to the one that comes with Windows 7 and Windows 8 (i.e., Windows preinstalled zip utility).   Unfortunately, it didn't seem to work w/ the default zip utility program.    Is there any chance the code code be modified to allow flexibility as to which utility program is utilized?

Thanks in advance for your continued help,
EEH
Yes, we can remove the files that were placed in the ZIP.

Technically you don't need to install the 7-zip utility to use it, just having the main EXE available I believe is enough to run it.  Is that a possibility for you, to distribute the EXE with the BAT file?

What is the "preinstalled zip utility" that you are reference related to Windows?  It doesn't really come with a true command line ZIP utility, want to understand what you are referring to.

~bp
~bp:

After additional testing on a 2nd machine, it appears to me that additional files from the 7-zip directory are required.

Essentially, I temporarily renamed the original 7-Zip folder "7-Zip_x".  Then I created a new folder called "7-Zip" and move the .EXE into it.  

When executing the batch, it didn't work.   I then deleted this new folder and renamed the original folder back to "7-Zip".   Then it worked.

If we knew what the additional files may be, I believe we should be able to just drop them on the customer's network w/o actually installing the full program.  

Any ideas which files the .EXE might require?

EEH
This little vbs script will work with native XP, Vista, Win7, Win8 "Send to compressed folder" driver. Please save the content of this script to a file called test.vbs and run:
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!"

Open in new window

chaau:

Oh... very nice... that worked great as well.

Quick question... as mentioned in today's earlier up (whether via 7-zip or this VBS), how can I remove all of the files (not folders or subfolders) from the source AFTER they have been zipped and moved?

So, process should be:
- Zip source directory (folders, subfolders, files)
- Save zip to another directory
- Remove all files from source directory

Can this be accommodated in this VBS script?

EEH
Please post a new question
Will do!  Thx.
chaau:

I just opened a new question...

https://www.experts-exchange.com/questions/28294665/Batch-file.html

Any chance you know how to tweak this VBS script to remove the files?

Thanks,
EEH