• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 77
  • Last Modified:

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

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
brasiman
Asked:
brasiman
  • 25
  • 13
2 Solutions
 
NVITCommented:
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
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
brasimanAuthor Commented:
Thanks NewVillageIT. Oh, no! I don't want to delete it, just copy it. :)
0
 
NVITCommented:
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
 
NVITCommented:
Another item... Do the contents of each DATA and OLDDATA folder have sub-folders which also have contents? Or, just individual files?
0
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
 
NVITCommented:
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
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
 
NVITCommented:
My example set set SrcDir=c:\AB_folders
Have you changed c:\AB_folders to match your use?
0
 
brasimanAuthor Commented:
Yes, i have the z-zip installed. Oh, haha. By bad. Do I put C:\OLD OLDDATA? My bad.
0
 
brasimanAuthor Commented:
I put C:\OLD OLDDATA and it still does the same thing. I have 7-zip installed too.
0
 
NVITCommented:
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
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
 
NVITCommented:
Scratch that last post.
0
 
NVITCommented:
First, confirm the 7-zip folder via:
dir "%ProgramFiles%\7-Zip"

Open in new window

What is returned?
0
 
brasimanAuthor Commented:
Its in C:\Program Files (x86)\7-Zip
0
 
NVITCommented:
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
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
 
NVITCommented:
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
 
NVITCommented:
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
 
NVITCommented:
There's an error in the Oops! post, too. Use the last one instead.
0
 
brasimanAuthor Commented:
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
 
NVITCommented:
Seems like you're missing the bottom parens of the last FOR
0
 
NVITCommented:
FYI, at this point you get rid of PUSHD and POPD lines.
0
 
NVITCommented:
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
 
brasimanAuthor Commented:
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
 
NVITCommented:
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
 
NVITCommented:
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
 
brasimanAuthor Commented:
Now its not copying the subfolders/files or zipping anything up again.
0
 
NVITCommented:
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
 
NVITCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 25
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now