Solved

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

Posted on 2015-01-12
38
53 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 23

Expert Comment

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

Expert Comment

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

Author Comment

by:brasiman
Comment Utility
Thanks NewVillageIT. Oh, no! I don't want to delete it, just copy it. :)
0
 
LVL 23

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Author Comment

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

Author Comment

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

Expert Comment

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

Expert Comment

by:NVIT
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Scratch that last post.
0
 
LVL 23

Expert Comment

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

Open in new window

What is returned?
0
 

Author Comment

by:brasiman
Comment Utility
Its in C:\Program Files (x86)\7-Zip
0
 
LVL 23

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

by:NVIT
Comment Utility
Seems like you're missing the bottom parens of the last FOR
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
FYI, at this point you get rid of PUSHD and POPD lines.
0
 
LVL 23

Accepted Solution

by:
NVIT earned 500 total points
Comment Utility
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
Comment Utility
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 23

Expert Comment

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

Expert Comment

by:NVIT
Comment Utility
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
Comment Utility
Now its not copying the subfolders/files or zipping anything up again.
0
 
LVL 23

Assisted Solution

by:NVIT
NVIT earned 500 total points
Comment Utility
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 23

Expert Comment

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

17 Experts available now in Live!

Get 1:1 Help Now