Solved

Script to Zip Files Remotely Then Copy To Another Location

Posted on 2013-10-24
12
860 Views
Last Modified: 2013-10-31
Hello,
I have a folder structure of several years worth of folders with names like 20131010, 20131011, 20131012...ect representing dates. I would like to create a script that remotely zips a folder based on today's date on one server then copies the zip file to another server. I would also like a second script to allow for 2 dates to be entered and all the folders get zipped and copied between those two dates.
Thanks in advance.
0
Comment
Question by:portillosjohn
  • 7
  • 5
12 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39597391
So do you just want to access the files on each remote server by a sharename, but do the actual ZIP processing from the third "controlling" computer?  Or do you want to remotely run the ZIP operation on the remote server?

What ZIP tool do you have installed that you will use, or do you need a recommendation as part of a solution?  For scripting the freeware 7-ZIP is often a reasonable choice, but you would have to install that.

Are there any subfolders in each of the main folders, and if so are they to be included?

~bp
0
 

Author Comment

by:portillosjohn
ID: 39597421
I would like to copy from server a to server b. I would like to zip the files on server a before copy. I would like to control the process from server b.  I need a recommandation on zip software as part of the solution. No sub folders.
-j
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39597520
Okay, here's a small BAT script that can be saved and run on the server that you want the zip files saved to.  You will need to edit the from and to folder paths, and the location of the 7-ZIP program near the top of the script.  This script just does your first request, the TODAY folder zipping.  Once that works we can use all the same pieces and just prompt for the range of dates for the second script, so lets get this one working first.

The from computer can be accessed with either a mapped drive, or a sharename, as in:

set FromDir=Z:\BaseDir
  or
set FromDir=\\computerA\c$\BaseDir

You will need to download and install the 7-Zip utility from:

http://www.7-zip.org/

@echo off
setlocal

REM Define source and destination locations, as well as zip program location
set FromDir=Z:\BaseDir
set ToDir=C:\DestDir
set Zip=c:\program files\7-zip\7za.exe

REM Format Todays date in YYYYMMDD format
set Today=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%

REM Zip up todays folder
call :ZipFolder %Today%

REM Exit the main script
exit /b

REM Subroutine to zip a single folder
:ZipFolder [folder-name]
  "%Zip%" a -tzip "%ToDir%\%~1.zip" "%FromDir%\%~1\*.*"
  exit /b

Open in new window

~bp
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:portillosjohn
ID: 39597737
Works great. I need to change it to yesterday rather than today.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39597920
Okay, yesterday can be done, but take a little bit more work.  I'll work that up in just a bit, and also the range version.

~bp
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39598869
Okay, here's the script for the folder from yesterday:

@echo off
setlocal

REM Define source and destination locations, as well as zip program location
set FromDir=Z:\BaseDir
set ToDir=C:\DestDir
set Zip=c:\program files\7-zip\7za.exe

REM Format Todays date in YYYYMMDD format
set Today=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%

REM Get yesterday's date (using temporary VBS helper script)
echo Wscript.Echo Year(Date-1)^&Right(100+Month(Date-1),2)^&Right(100+Day(Date-1),2)>"%temp%\dateparts.vbs"
for /F "tokens=*" %%D in ('cscript //nologo "%temp%\dateparts.vbs"') do set Yesterday=%%D

REM Zip up todays folder
call :ZipFolder %Yesterday%

REM Exit the main script
exit /b

REM Subroutine to zip a single folder
:ZipFolder [folder-name]
  "%Zip%" a -tzip "%ToDir%\%~1.zip" "%FromDir%\%~1\*.*"
  exit /b

Open in new window

And the one that prompts for a range:

@echo off
setlocal

REM Define source and destination locations, as well as zip program location
set FromDir=Z:\BaseDir
set ToDir=C:\DestDir
set Zip=c:\program files\7-zip\7za.exe

:Prompts
echo.
set FromDate=
set ToDate=

REM Prompt for FROM and TO dates
set /p "FromDate=Please enter from date (YYYMMDD):"
set /p "ToDate=Please enter to date (YYYMMDD):"

REM A few basic edits
if "%FromDate%" EQU "" if "%ToDate%" EQU "" exit /b
if "%FromDate%" LSS "000000" echo *ERROR* Invalid from date & goto :Prompts
if "%FromDate%" GTR "999999" echo *ERROR* Invalid from date & goto :Prompts
if "%ToDate%" LSS "000000" echo *ERROR* Invalid to date & goto :Prompts
if "%ToDate%" GTR "999999" echo *ERROR* Invalid to date & goto :Prompts
if "%FromDate%" GTR "%ToDate%" echo *ERROR* From date must be smaller than to date & goto :Prompts

REM Loop through all dates, and ZIP files
for /l %%A in (%FromDate%,1,%ToDate%) do (
  call :ZipFolder %Yesterday%
)

REM Exit the main script
exit /b

REM Subroutine to zip a single folder
:ZipFolder [folder-name]
  "%Zip%" a -tzip "%ToDir%\%~1.zip" "%FromDir%\%~1\*.*"
  exit /b

Open in new window

~bp
0
 

Author Comment

by:portillosjohn
ID: 39600894
Hi BP the yesterday script works great but the date range scrip does not seem to work.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39601362
Okay, I see one line that I didn't change from the single day script.  This line:

  call :ZipFolder %Yesterday%

should be:

  call :ZipFolder %%A

However, now that I think more about this, that will only work for a range within the same month, like 20131010 to 20131015.

It you tried to do something like 20131015 to 20131115 it's not going to work right.  This is due to the "simple" approach I took, which I now recognized isn't going to work for dates.

If you need to span months (and years if needed) then let me know and we can add some additional code to address that need.

~bp
0
 

Author Comment

by:portillosjohn
ID: 39601405
Bp your awesome! Although I will be using this across several years worth of data.
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39602082
Okay, using a couple of date routines I have used in other solutions, this seems to get the job done.  See how it goes for you there.

@echo off
setlocal EnableDelayedExpansion

REM Define source and destination locations, as well as zip program location
set FromDir=Z:\BaseDir
set ToDir=C:\DestDir
set Zip=c:\program files\7-zip\7za.exe

:Prompts
echo.
set FromDate=
set ToDate=

REM Prompt for FROM and TO dates
set /p "FromDate=Please enter from date (YYYMMDD):"
set /p "ToDate=Please enter to date (YYYMMDD):"

REM A few basic edits
if "%FromDate%" EQU "" if "%ToDate%" EQU "" exit /b
if "%FromDate%" LSS "000000" echo *ERROR* Invalid from date & goto :Prompts
if "%FromDate%" GTR "999999" echo *ERROR* Invalid from date & goto :Prompts
if "%ToDate%" LSS "000000" echo *ERROR* Invalid to date & goto :Prompts
if "%ToDate%" GTR "999999" echo *ERROR* Invalid to date & goto :Prompts
if "%FromDate%" GTR "%ToDate%" echo *ERROR* From date must be smaller than to date & goto :Prompts

REM Reformat date to MM/DD/YYYY and convert to Julian for loop math
call :jdate jFromDate "%FromDate:~4,2%/%FromDate:~6,2%/%FromDate:~0,4%"
call :jdate jToDate "%ToDate:~4,2%/%ToDate:~6,2%/%ToDate:~0,4%"

REM Loop through all dates, and ZIP files
for /l %%A in (%jFromDate%,1,%jToDate%) do (
  REM Convert julian date to YYYY MM and DD variables
  call :jdate2date %%A yyyy mm dd
  REM Zip the file
  call :ZipFolder !yyyy!!mm!!dd!
)

REM Exit the main script
exit /b

REM Subroutine to zip a single folder
:ZipFolder [folder-name]
  "%Zip%" a -tzip "%ToDir%\%~1.zip" "%FromDir%\%~1\*.*"
  exit /b

REM Subroutines
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
::                -- JD      [out,opt] - julian days
::                -- DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20090328 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b

Open in new window

~bp
0
 

Author Closing Comment

by:portillosjohn
ID: 39614878
Boomsauce! It worked perfectly. Thanks BP.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39615171
Welcome.

~bp
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Command-line Script 15 75
push same scheduled task across multiple servers? 16 171
MS DOS script or .Net Script to rename a list of sub-folders 7 117
how to run a batch process 16 48
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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