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

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

batch file to zip files

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
IT_newbie01
Asked:
IT_newbie01
  • 7
  • 6
  • 5
  • +1
3 Solutions
 
t-maxCommented:
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
 
BillDLCommented:
>>> "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
 
IT_newbie01Author Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
IT_newbie01Author Commented:
@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
 
t-maxCommented:
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
 
Bill PrewCommented:
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
 
IT_newbie01Author Commented:
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
 
t-maxCommented:
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
 
Bill PrewCommented:
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
 
BillDLCommented:
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
 
IT_newbie01Author Commented:
@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
 
IT_newbie01Author Commented:
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
 
BillDLCommented:
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
 
BillDLCommented:
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
 
t-maxCommented:
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
 
BillDLCommented:
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
 
BillDLCommented:
Oh DAMN!! Sorry t-max, the page wasn't refreshed until I posted my comment.  I was watching for you returning.
0
 
IT_newbie01Author Commented:
Im testing all solution.  Dont want anyone to think ive not involved!  I appreciate all the HELP!!!
0
 
t-maxCommented:
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
 
BillDLCommented:
Thank you IT_newbie01.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now