Solved

batch file to zip files

Posted on 2011-03-10
20
606 Views
Last Modified: 2012-05-11
Morning all,

Im looking to modify code to copy a file from 1 drive location and copy it over to another location zipped.  The files collectively exceed 600 gig and i only have half of that available.  Ultimately I want to copy to the new location and delete all the original files.  If you can think of a better option im also open to suggestion.

Thanks in advance!
echo off
setlocal EnableDelayedExpansion
set src=C:\pst_files
set dst=c:\test
for /F "tokens=*" %%F in ('dir /s/b/a:-d %src%\*.pst') do (
   set folder=%%~dpF
   set folder=!folder:~0,-1!
   for %%D in ("!folder!") do set folder=%%~nxD
   copy "%%~F" "%dst%\%%~nF.!folder!%%~xF
)

Open in new window

0
Comment
Question by:IT_newbie01
  • 7
  • 6
  • 5
  • +1
20 Comments
 
LVL 6

Expert Comment

by:t-max
ID: 35096266
I would use 7zip to compress files first, and if that and the copy were successful, delete the original uncompressed file.
The 7zip command line syntax is easy. If you need any help with any of this, post back.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 35096763
>>> "The files collectively exceed 600 gig and i only have half of that available." <<<

You mean that you only have half the storage capacity of the source files as your destination for them, hence the need to zip them up?

Risky business, I would say.  I would never risk copying files and deleting as I went without some solid verification in place.  Even then, the potential for loss if the system froze or you suffered a power outage could leave you in a real mess.

There is also the issue that you cannot reasonably anticipate what the compressed size of all the source files will be if they are in various file formats.  Some files like text-based files zip up to a fraction of the original file size, while others don't compress at all.

Whatever you do test, I would urge you to use the appropriate command line switch that SIMULATES the command and would give an advanced indication whether it would fail.

To be honest, I am hesitant to even make a suggestion here in case something does go horribly wrong and you lose 300 GB of files, especially while large capacity hard drives are fairly affordable these days.
0
 

Author Comment

by:IT_newbie01
ID: 35096766
Im researching 7zip and im liking what im seeing.  The code above is a batch file and was written in the command line.  Is it accurate to say that i can that code in the command line of 7zip.  I will need a loop and evaluation statement to get exactly what im looking for??
0
 

Author Comment

by:IT_newbie01
ID: 35096853
@BillDL: Much thanks for the concern.  I am only testing, i am not looking to deploy this solution until im solid it works and i have backed everything up.  I only state that i have half of the storage space and insight on why im persuing this project.  I will be adding memory next week but that's only a quick fix i want to zip files on a weekly basis as a long term solution.  Again thanks for you concern
0
 
LVL 6

Expert Comment

by:t-max
ID: 35097669
You already have the loop, right?
You only need to add the command to compress, and the rest is very easy: copy and delete.
Regarding the concerns, if you know your tons of GB contain text (repeated throughout the file), then the compression ratio will be good enough to turn those files into just a few hundred MB.
Here's a snippet of what I use in my servers:

SET SEVENZIP=C:\progra~1\7-zip\7z.exe
SET TPATH=c:\temp
SET DAT=C:\Bkup\sh11.dat
SET ZIP=sh11.zip

%SEVENZIP% a %TPATH%\%ZIP% %DAT%

DEL /Q %DAT%
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35099237
Do you still want each PST file to be a separate ZIP file, or do you want all files to be in the same ZIP?

~bp
0
 

Author Comment

by:IT_newbie01
ID: 35100738
id like each .pst file to be individual zipped and placed in a single folder.  But because you mentioned it could I get both?  If not that fine as well.  

Thanks for any help..!!
0
 
LVL 6

Accepted Solution

by:
t-max earned 100 total points
ID: 35101274
If necessary, modify the code below to point to your 7zip command.
The code I added is mainly in uppercase, except the ".zip" added to the %%~F variable.
Give it a try, being careful to check it with some test files.

echo off
setlocal EnableDelayedExpansion
set src=C:\pst_files
set dst=c:\test

SET SEVENZIP=C:\progra~1\7-zip\7z.exe

for /F "tokens=*" %%F in ('dir /s/b/a:-d %src%\*.pst') do (
   set folder=%%~dpF
   set folder=!folder:~0,-1!
   for %%D in ("!folder!") do set folder=%%~nxD

   %SEVENZIP% a "%%~F".zip "%%~F"
   IF %ERRORLEVEL% GTR 0 (
      ECHO "Failed compressing %%~F"
      ECHO "Aborting..."
      EXIT 1
    )
   copy "%%~F".zip "%dst%\%%~nF.!folder!%%~xF
   IF %ERRORLEVEL% GTR 0 (
      ECHO "Failed copying zipped %%~F"
      ECHO "Aborting..."
      EXIT 2
    )

   ECHO "File %%~F successfully compressed and  copied. Now removing it."
   DEL /Q %%~F
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35101495
To zip them all into a single ZIP it should be pretty simple, like:

@echo off
7z a -tzip c:\test\archive.zip -r c:\pst_files\*.pst

Open in new window

~bp
0
 
LVL 38

Expert Comment

by:BillDL
ID: 35104855
Looks like you have the best solutions to your problem IT_newbie01.  Sorry to be a killjoy earlier. I was very concerned about how much storage you had available.  Out of curiosity I zipped up a couple of test *.PST files using 7-Zip on "maximum" compression.  It applied compression ratios ranging between 39 and 50%, which is to say that the zip files ended up on average about 36% the file size of the original *.pst files.  If that is a standard result, your 600GB worth of PST files might only need about 235GB storage when zipped.  You might just get away with the destination storage you have at the moment.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:IT_newbie01
ID: 35109160
@t-max: Im testing now and will get back to you with results.
@billprew: thanks for the info.
@BillDL: Thanks for the concern; i honestly appreciate your concern and following up with testing compression ratios.

YOU ALL ROCK!!
0
 

Author Comment

by:IT_newbie01
ID: 35109455
t-max: The zipping is working correctly (actually really good ration), but the file is storing in the sub-directories.  Im looking to centralize all the files.  That's why im changing the name of the file so that i dont have files naming errors.  Please advise.
0
 
LVL 38

Assisted Solution

by:BillDL
BillDL earned 200 total points
ID: 35112275
Hi IT_newbie01

I've missed some of what went on because I couldn't connect to the Internet earlier.  Unfortunately t-max's batch file created Zip files in each of the same folders as the PST files, but instead of copying the zip files to the destination it copied the PST files.

I had read your last comment just before my connection went down and wrote a batch file to test if it matches what you need.

Assuming the master folder containing loose PST files and PST files in sub-folders therof, is "C:\pst_files", and assuming that the destination folders for ALL the Zip files created by 7-Zip is C:\test, then what you want this to happen for each PST file found:

1. Zipped up to the root of "C:\pst_files".
2. The Zip file to have the name of the PST file.
(Do you need it to have the name of the containing folder used in the Zip file name also?)
3. Verification that the zipping was successful.
4. If successful, copy Zip file to "C:\test".
5. On successful copy, delete the zip file from "C:\pst_files".
6. Delete the corresponding PST file???

Is that correct?

Save the attached batch file and rename as CMD, then run it on your testing folders to see if the output indicates the commands that you wish to be executed.

If it does, then it is easy to incorporate the variables I used into a batch file.

 Zip-PSTs.txt
0
 
LVL 38

Expert Comment

by:BillDL
ID: 35112489
Sorry, my mistake.  t-max's batch file DID copy the Zip files to the C:\Test folder, but applied the *.PST extension to the copies instead of the *.ZIP extension.  I hadn't noticed the file size differences.

0
 
LVL 6

Expert Comment

by:t-max
ID: 35112772
Yes it seems I forgot to add the ".zip" to the copied files. Sorry :)
About centralizing the files, If I'm not compiling the code in my head wrong, all files are stored inside %dst%, right?
We have:
"%dst%\%%~nF.!folder!%%~xF
Which should be:
c:\test\<file-name>.<folder-name>pst

If that's not what you want, tell us what file name pattern do you need.
Personally I'll do it like this: c:\test\<folder name>.<full file name>.zip

copy "%%~F.zip" "%dst%\!folder!.%%~sF.zip"

Open in new window


Max
0
 
LVL 38

Assisted Solution

by:BillDL
BillDL earned 200 total points
ID: 35112848
Hi again IT_newbie01

No sign of t-max, so I hope he doesn't mind if I post a corrected, and very slightly modified, version of his excellent batch file.

I placed the "folder name" in front of the PST file name in the name of the zip file and separated them with a dash in case the double dots cause any problems further down the line.  The extra echoed lines are cosmetic for testing purposes and can be removed later.
I hope I haven't had any little oversights.  It works OK for me.  

Bill

 
@echo off
setlocal EnableDelayedExpansion
set src=C:\pst_files
set dst=c:\test

SET SEVENZIP=C:\progra~1\7-zip\7z.exe

for /F "tokens=*" %%F in ('dir /s/b/a:-d %src%\*.pst') do (
   set folder=%%~dpF
   set folder=!folder:~0,-1!
   for %%D in ("!folder!") do set folder=%%~nD
   %SEVENZIP% a "%src%\!folder!-%%~nF.zip" "%%~F"
   ECHO.
   IF %ERRORLEVEL% GTR 0 (
      ECHO.
      ECHO Failed compressing "%%~F" !!
      ECHO.
      ECHO "Aborting..."
      EXIT 1
    )
   copy /v /b "%src%\!folder!-%%~nF.zip" "%dst%" > nul 2>&1
   IF %ERRORLEVEL% GTR 0 (
      ECHO.
      ECHO Failed copying file "%src%\!folder!-%%~nF.zip" !!
      ECHO.
      ECHO "Aborting..."
      EXIT 2
    )
   ECHO Successfully compressed and copied File:
   ECHO "%%~F"
   ECHO.
   ECHO Removing Files:
   ECHO "%src%\!folder!-%%~nF.zip"
   ECHO "%%~F"
   ECHO.
   ECHO DEL /Q "%%~F"
   ECHO DEL /Q "%src%\!folder!-%%~nF.zip"
   ECHO.
   ECHO ---------------------------------------------
   ECHO.
)
ECHO.
ECHO.
IF %ERRORLEVEL% GTR 0 (
    ECHO FINISHED WITH ERRORS! ^(Exit Code: %ERRORLEVEL%^)
    ) ELSE (
    ECHO FINISHED WITH NO ERRORS.
)
ECHO.
ECHO.
pause

Open in new window

0
 
LVL 38

Expert Comment

by:BillDL
ID: 35112859
Oh DAMN!! Sorry t-max, the page wasn't refreshed until I posted my comment.  I was watching for you returning.
0
 

Author Comment

by:IT_newbie01
ID: 35112883
Im testing all solution.  Dont want anyone to think ive not involved!  I appreciate all the HELP!!!
0
 
LVL 6

Expert Comment

by:t-max
ID: 35113105
Yeah, that happens from time to time. I learned to refresh the page before I post, just in case.
BTW Bill, thanks for your comment, but I just added the zipping command, and some error checking. All the credit goes to the poster.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 35330806
Thank you IT_newbie01.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

18 Experts available now in Live!

Get 1:1 Help Now