Solved

Copy folders from one drive to another and zip the copied folders

Posted on 2015-01-12
38
66 Views
Last Modified: 2015-03-02
I have two folders on the C drive. We can call them folder A and B. Here's what i'd like to do:

1. I would like to copy those two folders to the G drive in a folder called Roll. When the folders are copied to the Roll folder on the G drive, I would like those two folders (A and B) to be zipped so it doesn't take alot of space on my G drive.
2. If possible, I would also for the script to somehow only keep the newest 7 copies. I want to run this every day but don't want to keep deleting the zipped folders older than 7 days.
3. When the folders are copied to the Roll folder on the G drive, I would like the zipped folder to be named the date the script was run (timestamp).

Does that make sense? I hope that does. :) Thank you!!
0
Comment
Question by:brasiman
  • 25
  • 13
38 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 40545791
I think Windows doesn't have a built-in zip for the command line. You'll need to install a zip program like 7-zip.

Note: Remove the ECHO prefix to run it for real.

Make the .zip files:
This batch...

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
  Set Day=%%A
  Set Month=%%B
  Set Year=%%C
)
For /F "tokens=1-4 delims=: " %%A in ('time /t') do @(
  Set HH=%%A
  Set MM=%%B
  Set AM=%%C
)

set SrcDir=c:\AB_folders
set TgtDir=g:\roll

pushd %SrcDir%

for %%a in (A B) do (md "%TgtDir%\%%a\") 2>nul
for /d %%a in (A B) do (
  ECHO 7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*"
  ECHO rd /q "%TgtDir%\%%a\"
  ECHO move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)
popd

Open in new window


... makes something like this:

7z a "2015_12_01_03_48_PM_A.zip" "*.*"
move "2015_12_01_03_48_PM_A.zip" "g:\roll\A\"
rd /q "c:\AB_folders\A\"
7z a "2015_12_01_03_48_PM_B.zip" "*.*"
move "2015_12_01_03_48_PM_B.zip" "g:\roll\B\"
rd /q "c:\AB_folders\B\"

Open in new window


To Delete all zip files older than 7 days:

REM Delete all zip files older than 7 days

set TgtDir=g:\roll
set TgtFExt=zip

forfiles /p "%TgtDir%" /m "*.%TgtFExt%" /d -7 /c "cmd /c ECHO del @path"

Open in new window

0
 

Author Comment

by:brasiman
ID: 40549021
Hi NewVillageIT. Thanks for your help! I have a couple questions. I installed 7-Zip. When I run the first script, it created a folder called A and another called B. I wasn't very clear on my explanation of those folders, so I apologize. I was giving the folders I want to copy a different name, I shouldn't have. On my C drive, I have a folder called DATA and another called OLDDATA. Those are the two folders I want to copy and move to the G:/roll folder all zipped up. Would I use a variation of that first scrips to copy the DATA and OLDDATA folders, zip them up together, then paste them into the G:/roll folder? Thanks!
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40549411
If I understand you... yes, you can substitute the folder names A and B with DATA and OLDDATA like...
for %%a in (DATA OLDDATA) do (md "%TgtDir%\%%a\") 2>nul
for /d %%a in (DATA OLDDATA) do (
  ECHO 7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*"
  ECHO rd /q "%TgtDir%\%%a\"
  ECHO move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)

Open in new window

On another note, you do realize that the RD command will erase both the C: DATA and OLDDATA folders - content and folder itself. Is that what you want? Or, do you just want to erase the contents?
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!

 

Author Comment

by:brasiman
ID: 40549424
Thanks NewVillageIT. Oh, no! I don't want to delete it, just copy it. :)
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40549433
I don't want to delete it, just copy it
OK. But how do/will you account for the C: filling up? Do you manually erase the files?
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40549471
Another item... Do the contents of each DATA and OLDDATA folder have sub-folders which also have contents? Or, just individual files?
0
 

Author Comment

by:brasiman
ID: 40549478
Correct, we manage both folders manually. In those folders, we are just collecting some data. There isn't a ton in each. It is mostly Word, Excel and other random files.

Yes, there are sub folders in both DATA and OLDDATA folders.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40549617
Revised first part: "Make the .zip files":
- Includes sub-folders
- Removed RD line to erase source
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
  Set Day=%%A
  Set Month=%%B
  Set Year=%%C
)
For /F "tokens=1-4 delims=: " %%A in ('time /t') do @(
  Set HH=%%A
  Set MM=%%B
  Set AM=%%C
)

set SrcDir=c:\AB_folders
set TgtDir=g:\roll

pushd %SrcDir%

for %%a in (DATA OLDDATA) do (md "%TgtDir%\%%a\") 2>nul
for /d %%a in (DATA OLDDATA) do (
  ECHO 7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  ECHO move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)
popd

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40549643
Revised 2nd part - To Delete all zip files older than 7 days:
REM Delete all zip files older than 7 days

set TgtDir=g:\roll
set TgtFExt=zip

for %%a in (DATA OLDDATA) do (
  forfiles /p "%TgtDir%\%%a" /m "*.%TgtFExt%" /d -7 /c "cmd /c ECHO del @path"
)

Open in new window

0
 

Author Comment

by:brasiman
ID: 40553843
Hi NewVilliageIT. It looks like its just creating a folder called OLD and OLDDATA. When run the revised script above, a folder called DATA and OLDDATA show up in my "rolls" folder on the g drive. But there isn't anything in those folders. All my files/sub folders aren't there. Did I do something wrong?
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554164
Did you remove the ECHO prefixes as I stated earlier? As good practice, the ECHO prefixes are there mainly for testing, in this case to see that the source and target folders and filenames are as expected. Once that's done, remove the ECHO to run it for real:
  ECHO 7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  ECHO move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"

Open in new window

becomes...
  7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"

Open in new window

0
 

Author Comment

by:brasiman
ID: 40554176
Yes. I took the ECHO off and it still looks like it just created the two folders or copy's them with no data in the folder. It also doesn't timestamp or zip it.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554198
My example assumes 7-zip is installed. Is that what you have?

Would you please run the .bat from the command line? This will show errors. Then, post everything here.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554202
My example set set SrcDir=c:\AB_folders
Have you changed c:\AB_folders to match your use?
0
 

Author Comment

by:brasiman
ID: 40554213
Yes, i have the z-zip installed. Oh, haha. By bad. Do I put C:\OLD OLDDATA? My bad.
0
 

Author Comment

by:brasiman
ID: 40554220
I put C:\OLD OLDDATA and it still does the same thing. I have 7-zip installed too.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554225
Your original post...
On my C drive, I have a folder called DATA and another called OLDDATA
In my example, I assumed DATA and OLDDATA are sub-folders of c:\AB_folders

Are yours specifically: C:\DATA and C:\OLDDATA? If so:
SrcDir=c:\

Open in new window

0
 

Author Comment

by:brasiman
ID: 40554240
Sorry, yes. DATA and OLDDATA.

I have a folder called DATA and OLDDATA on the C drive. SO, C:\DATA and C:\OLDDATA. Within those folders, I have subfolders and files.

I rand the script from the cmd prompt and it says:
'7z' is not recognized as an internal or external command, operable program or batch file. The system cannot find the file specified.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554248
For now, please change:
7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r

Open in new window

to...
"C:\Program Files\7-Zip\7z.exe" a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554250
Scratch that last post.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554255
First, confirm the 7-zip folder via:
dir "%ProgramFiles%\7-Zip"

Open in new window

What is returned?
0
 

Author Comment

by:brasiman
ID: 40554263
Its in C:\Program Files (x86)\7-Zip
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554273
Next, change:
7z a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r

Open in new window

to...
"%ProgramFiles(x86)%\7-Zip\7z.exe" a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r

Open in new window

0
 

Author Comment

by:brasiman
ID: 40554289
That kind of worked. But it started to compress every folder on my C drive. How do I change the SrcDir=c:\ so it copy's then zips the c:\DATA folder and c:\OLDDATA folder...including all the subfolders\files in it?
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554308
Please move the PUSHD and POPD lines into the FOR loop and revise like so:
for /d %%a in (DATA OLDDATA) do (
  pushd %SrcDir%%a%
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
  popd
)

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554323
Oops!
for /d %%a in (DATA OLDDATA) do (
  pushd %SrcDir%%%a
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  move "%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
  popd
)

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554325
A simpler way is to get rid of PUSHD and POPD:
for /d %%a in (DATA OLDDATA) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  move "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554330
There's an error in the Oops! post, too. Use the last one instead.
0
 

Author Comment

by:brasiman
ID: 40554371
It copies the folders but nothing is in those folders and is not zipped up. Here's the code I now have:
SETLOCAL ENABLEDELAYEDEXPANSION

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
  Set Day=%%A
  Set Month=%%B
  Set Year=%%C
)
For /F "tokens=1-4 delims=: " %%A in ('time /t') do @(
  Set HH=%%A
  Set MM=%%B
  Set AM=%%C
)

set SrcDir=c:\
set TgtDir=g:\roll

pushd %SrcDir%

for %%a in (DATA OLDDATA) do (md "%TgtDir%\%%a\") 2>nul
for /d %%a in (DATA OLDDATA) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  move "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
popd
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554384
Seems like you're missing the bottom parens of the last FOR
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554394
FYI, at this point you get rid of PUSHD and POPD lines.
0
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 40554413
Tightened up a bit...
SETLOCAL ENABLEDELAYEDEXPANSION

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(Set Day=%%A&Set Month=%%B&Set Year=%%C)
For /F "tokens=1-4 delims=: " %%A in ('time /t') do @(Set HH=%%A&Set MM=%%B&Set AM=%%C)

set SrcDir=c:\
set TgtDir=g:\roll

for %%a in (DATA OLDDATA) do (md "%TgtDir%\%%a\") 2>nul

for /d %%a in (DATA OLDDATA) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "*.*" -r
  move "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)

Open in new window

0
 

Author Comment

by:brasiman
ID: 40554457
Now it will zip what it copies, but it copies all the files/folders on my desktop, not the C:\DATA and C:\OLDDATA folders. It created the two folders DATA and OLDDATA on the g:\roll. But the same desktop files are zipped up.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554491
for /d %%a in (DATA OLDDATA) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%SrcDir%%%a\*.*" -r -x!*%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip
  move "%SrcDir%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40554561
If you make the zip in a different folder, in this case the TEMP folder, then we don't need the -x (exclude) switch:
for /d %%a in (DATA OLDDATA) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%temp%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%SrcDir%%%a\*.*" -r
  move "%temp%%%a\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)

Open in new window

0
 

Author Comment

by:brasiman
ID: 40554594
Now its not copying the subfolders/files or zipping anything up again.
0
 
LVL 24

Assisted Solution

by:NVIT
NVIT earned 500 total points
ID: 40554685
Sorry. Try this:
for /d %%a in (DATA OLDDATA) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" a "%temp%\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%SrcDir%%%a\*.*" -r
  move "%temp%\%Year%_%Month%_%Day%_%HH%_%MM%_%AM%_%%a.zip" "%TgtDir%\%%a\"
)

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40612755
Hi brasiman.

It's been awhile but do you still need help with this? Either way, if you would close your question appropriately that would be great.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

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