Solved

Batch file

Posted on 2013-11-12
24
603 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 3

Expert Comment

by:MacGyver80
Comment Utility
What application are you trying to accomplish this with?
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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 24

Expert Comment

by:chaau
Comment Utility
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
 

Author Comment

by:ExpExchHelp
Comment Utility
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
Comment Utility
@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 24

Expert Comment

by:chaau
Comment Utility
I think you need to download Command Line version from here
0
 
LVL 51

Expert Comment

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

~bp
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
~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 51

Expert Comment

by:Bill Prew
Comment Utility
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 24

Expert Comment

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

Author Comment

by:ExpExchHelp
Comment Utility
~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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

Assisted Solution

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

~bp
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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
Comment Utility
chaau -- thanks for your help as well.   ~bp's solution came through first though.

Again, thanks!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Welcome, glad that was useful, thanks for the feedback.

~bp
0
 

Author Comment

by:ExpExchHelp
Comment Utility
~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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
~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 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
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 24

Expert Comment

by:chaau
Comment Utility
Please post a new question
0
 

Author Comment

by:ExpExchHelp
Comment Utility
Will do!  Thx.
0
 

Author Comment

by:ExpExchHelp
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now