Solved

Windows Batch script to create directories to backup to & housekeep directories older than 10 days

Posted on 2010-08-17
17
697 Views
Last Modified: 2012-05-10

I can manually copy using an scp utility currently to another ssh server as follows :
c:\shareware\pscp -l administrator -pw mypasswd  *.pst 192.168.149.191:d:\bkarc\yyyymmdd\
  where yyyy is the year, mm is the 2 digit month & dd is the 2 digit day


I'll need a Windows batch ( .bat or .cmd ) script that will copy on daily basis my Outlook
archive files (.pst) to the ssh server.

The directory name will be different each day (yyyymmdd, eg on 3rd Feb 2010, yyyymmdd
would be 20100203)

I'll also need another script on the ssh server that runs daily to housekeep / delete away
d:\bkarc\yyyymmdd  that are older than 10 days
0
Comment
Question by:sunhux
  • 9
  • 6
  • 2
17 Comments
 
LVL 9

Accepted Solution

by:
skipper68 earned 125 total points
ID: 33455933
I combined the 2 scripts into 1.  If you need them broken into 2, I can do that for you.  Run the following as a scheduled task.

' Copy files from yyymmdd directory to ssh directory
sourceDir = "d:\bkarc\" & year(Now) & Month(Now) & Day(Now)
destinationDir = "d:\"
Set objFolder = objFS.GetFolder(sourceDir)
Go(objFolder)
 
Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders        
        Go eFolder
    Next
    For Each strFile In objDIR.Files
        strFileName = strFile.Name
        strExtension = objFS.GetExtensionName(strFile)
        If strExtension = "doc" Then
                objFS.CopyFile strFile , destinationDir & strFileName
        End If  
    Next    
  End If  
End Sub
'
'
'
'
'
' Now delete files in a particular folder that are older than the days specified.
Dim oFS
Dim iOldFileDays' How old are the files you want to delete
Dim sInputFolder' The folder you want to look in
Dim iDays
''''' Start user defined Variables ''''''''''
' delete files older than x days (watch you don't delete the minus sign)
iDays=-10
' Provide folder name here ( don't include the trailing slash )
'Assuming you want the directory specified above as the directory you want to maintain....
sInputFolder=sourceDir
''''' End user defined Variables ''''''''''
Set oFS = CreateObject("Scripting.FileSystemObject")
' Delete the folders older than days specified
iOldFileDays = DateAdd("d", iDays, Date)
' Remove the files from given folder
If (trim(sInputFolder)<>"") Then
 RemoveOld sInputFolder, iOldFileDays
End If
Set oFS=Nothing
Function RemoveOld(sFolder,dDate)
 ' remove old files
 'On Error Resume Next ' error trap
 Dim oFolder,oFile,oFiles
 Dim oFolders,oSfolder
 Set oFolder = oFS.GetFolder(sFolder)
 Set oFiles = oFolder.Files
 For Each oFile In oFiles
 If oFile.DateLastModified < dDate Then
  'msgbox oFile.Path
  oFS.DeleteFile oFile.Path,True
 End If
 Next
 Set oFolders = oFolder.SubFolders
 For Each oSfolder In oFolders
  RemoveOld oSfolder.Path, dDate
   
  ' delete the folder now
  'msgbox oSfolder.DateCreated & " - " & dDate
  If (oSfolder.DateCreated<dDate) Then
   oSfolder.Delete
  End If
 Next
 ' destroy objects
 Set oFolder = Nothing
 Set oFiles = Nothing
End Function
0
 
LVL 9

Expert Comment

by:skipper68
ID: 33455939
Sorry I didn't use the code box but I couldn't submit it that way.  the page kept timing out.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33460925
Can you tell me what the following command displays at a prompt on the system where this will run:

ECHO %DATE%

~bp
0
 

Author Comment

by:sunhux
ID: 33460984
Hi Skipper68, split into 2 scripts, pls


ECHO %DATE%    gives
Wed 08/18/2010


Thanks
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 375 total points
ID: 33460987
Okay, since you were looking for BAT scripts, here's a couple that should be pretty close to what you need.  These assume your %DATE% variable ends with MM/DD/YYYY, if that is not the case then we will need to adjust the SET TODAY lines.

~bp
@echo off
set Today=%Date:~-4,4%%Date:~-10,2%%Date:~-7,2%
c:\shareware\pscp -l administrator -pw mypasswd  *.pst 192.168.149.191:d:\bkarc\%Today%\
exit /b
 
------------------------------------------------------------
 
@echo off
setlocal EnableDelayedExpansion
 
REM Get todays date, convert to julian for age checks
set Today=%Date:~-4,4%%Date:~-10,2%%Date:~-7,2%
call :jDate jToday %Today%
 
REM Process first level folders in backup area, remove old ones
for /D %%A in ("d:\bkarc\*") do (
  call :jDate jDirDate %%~nA
  set /A DirAge = !jToday! - !jDirDate!
  if !DirAge! GTR 10 rd /q /s "%%~fA%">NUL
)
exit /b
 
REM Subroutine to calculate julian date
:jDate return-variable date-string(YYYYMMDD)
  set DateStr=%~2
  set yy=%DateStr:~0,4%
  set /A mm=1%DateStr:~4,2%-100
  set /A dd=1%DateStr:~6,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=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
  exit /b

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33460992
==> ECHO %DATE%    gives
==> Wed 08/18/2010

Okay, my BAT scripts should work with that date format.

~bp
0
 

Author Comment

by:sunhux
ID: 33471696
Hi Skipper,  is your codes a VB script or a Windows batch script (.cmd or .bat)


Thanks Bill.


What about the other script to housekeep folders older than 10 days old.

Basically I'll run it as a scheduled .bat or .cmd  containing the line
rmdir/s/y d:\bkarc\Folders_older_than_10_days
0
 

Author Comment

by:sunhux
ID: 33472208

Sorry Bill, just noticed you've actually provided 2 scripts separated by the dashed line
-------------------
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sunhux
ID: 33472589


Hi Bill,

One little thing I overlooked for the line below:
c:\shareware\pscp -l administrator -pw mypasswd  *.pst 192.168.149.191:d:\bkarc\%Today%\

The destination folder needs to be created in advance & I'm afraid it's not an option to ssh into
the destination server to create the directory.  So will need yr help to amend your 2nd script
(which is scheduled to run daily on the destination server) to create a folder for the next day.
So if today is 20100818, when the 2nd script runs at say 2am, it will create another folder for
next day ie "mkdir d:\bkarc\20100819".  Sorry for overlooking this earlier
0
 

Author Comment

by:sunhux
ID: 33472629

You've wonderfully calculated "Today" using
set Today=%Date:~-4,4%%Date:~-10,2%%Date:~-7,2%

So in my last post above, I'll need to calculate
  set Tomorrow= ... ?? ...
& then insert into the 2nd script :
  mkdir d:\bkarc\%Tomorrow%

Also, would like to seek your advice if I prefix with "cmd/c" the following :
cmd/c c:\shareware\pscp -l administrator -pw mypasswd  *.pst 192.168.149.191:d:\bkarc\%Today%\

Reason is sometimes a certain *.pst file is locked, so I'll need the pscp to be able to continue to
copy/backup the other files over.

I thought of appending more pscp ie
cmd/c c:\shareware\pscp -l ... -pw ... *.ost ...
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 375 total points
ID: 33474018
==> to create a folder for the next day

Would it be possible to place a small EXE file on the server where the BAT file will be running?  There is a freeware tool that makes it very simple to calculate tomorrows date in a BAT file that would save us a lot of complexity in the BAT file.  No install needed, just a standalone command line utility.  Let me know if this is an option.

~bp
0
 

Author Comment

by:sunhux
ID: 33476672

> Would it be possible to place a small EXE file on the server

Yes, the above is possible.  Thanks & let me know how I can obtain it & how to use it
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 375 total points
ID: 33477774
Okay, grab the DOFF utility from here:

http://www.jfitz.com/dos/index.html#DOFF

Then place the EXE either in a folder in the PATH, or in the same folder as the BAT file executes.

Then in my original second script to delete old folders, add the following right before the first exit /b

~bp

REM Create folder for tomorrow
for /F %%A in ('doff yyyymmdd +1') do set Tomorrow=%%A
md "d:\bkarc\%Tomorrow%"

Open in new window

0
 

Author Comment

by:sunhux
ID: 33482539

When running doff.exe, got a message that  "doff.exe is not a valid Win32 application" -
perhaps the copy I downloaded is for 64 bit?
0
 

Author Comment

by:sunhux
ID: 33482588

Strange, if doff.exe is run off my thumb drive, it gave that  "not a valid win32 app"
error but when it's run off the local hard disk, it's Ok
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33484184
Yes, strange.  I did try a test here, I placed a copy of doff.exe on a usb drive here, and then ran the program from there, it worked fine.  Not sure why you had an issue, almost sounds like a bad copy to the usb or something.

~bp
0
 

Author Comment

by:sunhux
ID: 33485454

Anyway, thanks very much Bill, it worked on the local Hard disk, that's all I need
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now