Solved

Script to Zip Files Remotely Then Copy To Another Location

Posted on 2013-10-24
12
806 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

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

Author Comment

by:portillosjohn
Comment Utility
Works great. I need to change it to yesterday rather than today.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
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.

 

Author Comment

by:portillosjohn
Comment Utility
Hi BP the yesterday script works great but the date range scrip does not seem to work.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
Bp your awesome! Although I will be using this across several years worth of data.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Boomsauce! It worked perfectly. Thanks BP.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Welcome.

~bp
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Extracting computer names from an OU 6 52
Robocopy 5 78
Powershell won't output to file 10 91
USB backup batch file help needed. 6 65
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

10 Experts available now in Live!

Get 1:1 Help Now