Solved

Batch file

Posted on 2013-11-12
24
615 Views
Last Modified: 2013-11-14
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
0
Comment
Question by:ExpExchHelp
  • 11
  • 6
  • 6
  • +1
24 Comments
 
LVL 5

Expert Comment

by:MacGyver80
ID: 39643306
What application are you trying to accomplish this with?
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39643374
Get yourself a copy of 7-Zip here and then this BAT should get the job done.

http://www.7-zip.org/

@echo off

REM Define file and folder locations
set BaseDir=d:\Mainfolder_XYZ
set DestZip=c:\MyFolder\Test.zip
set z=c:\program files\7-zip\7za.exe

REM Zip all files and folders
"%z%" a -tzip "%DestZip%" -r "%BaseDir%\*.*"

Open in new window

~bp
0
 
LVL 25

Expert Comment

by:chaau
ID: 39643383
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)
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 Comment

by:ExpExchHelp
ID: 39643405
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
0
 

Author Comment

by:ExpExchHelp
ID: 39643411
@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
0
 
LVL 25

Expert Comment

by:chaau
ID: 39643415
I think you need to download Command Line version from here
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39643416
Don't use 7zfm.exe, thats the graphical version.  Use 7z.exe.

~bp
0
 
LVL 25

Expert Comment

by:chaau
ID: 39643419
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
0
 

Author Comment

by:ExpExchHelp
ID: 39643423
~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.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39643427
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
0
 
LVL 25

Expert Comment

by:chaau
ID: 39643430
check "c:\Program Files (x86)" folder. There could be 7-Zip folder there as well
0
 

Author Comment

by:ExpExchHelp
ID: 39643440
~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
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 39643445
Are there any files in the subfolders?

~bp
0
 

Author Comment

by:ExpExchHelp
ID: 39643449
Ah... that did the trick!!!   I've added some test file into both of them.

It works like a charm!   Thousand thanks!!

EEH
0
 

Author Comment

by:ExpExchHelp
ID: 39643452
chaau -- thanks for your help as well.   ~bp's solution came through first though.

Again, thanks!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39643462
Welcome, glad that was useful, thanks for the feedback.

~bp
0
 

Author Comment

by:ExpExchHelp
ID: 39648301
~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
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39648371
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
0
 

Author Comment

by:ExpExchHelp
ID: 39649266
~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
0
 
LVL 25

Expert Comment

by:chaau
ID: 39649404
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

0
 

Author Comment

by:ExpExchHelp
ID: 39649435
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
0
 
LVL 25

Expert Comment

by:chaau
ID: 39649439
Please post a new question
0
 

Author Comment

by:ExpExchHelp
ID: 39649455
Will do!  Thx.
0
 

Author Comment

by:ExpExchHelp
ID: 39649475
chaau:

I just opened a new question...

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28294665.html

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

Thanks,
EEH
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 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