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

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

Script to Zip Files Remotely Then Copy To Another Location

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
portillosjohn
Asked:
portillosjohn
  • 7
  • 5
1 Solution
 
Bill PrewCommented:
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
 
portillosjohnAuthor Commented:
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
 
Bill PrewCommented:
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
portillosjohnAuthor Commented:
Works great. I need to change it to yesterday rather than today.
0
 
Bill PrewCommented:
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
 
Bill PrewCommented:
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
 
portillosjohnAuthor Commented:
Hi BP the yesterday script works great but the date range scrip does not seem to work.
0
 
Bill PrewCommented:
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
 
portillosjohnAuthor Commented:
Bp your awesome! Although I will be using this across several years worth of data.
0
 
Bill PrewCommented:
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
 
portillosjohnAuthor Commented:
Boomsauce! It worked perfectly. Thanks BP.
0
 
Bill PrewCommented:
Welcome.

~bp
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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