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


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
sunhuxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DougApplication Development ManagerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DougApplication Development ManagerCommented:
Sorry I didn't use the code box but I couldn't submit it that way.  the page kept timing out.
0
Bill PrewCommented:
Can you tell me what the following command displays at a prompt on the system where this will run:

ECHO %DATE%

~bp
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

sunhuxAuthor Commented:
Hi Skipper68, split into 2 scripts, pls


ECHO %DATE%    gives
Wed 08/18/2010


Thanks
0
Bill PrewCommented:
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
Bill PrewCommented:
==> ECHO %DATE%    gives
==> Wed 08/18/2010

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

~bp
0
sunhuxAuthor Commented:
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
sunhuxAuthor Commented:

Sorry Bill, just noticed you've actually provided 2 scripts separated by the dashed line
-------------------
0
sunhuxAuthor Commented:


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
sunhuxAuthor Commented:

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
Bill PrewCommented:
==> 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
sunhuxAuthor Commented:

> 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
Bill PrewCommented:
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
sunhuxAuthor Commented:

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
sunhuxAuthor Commented:

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
Bill PrewCommented:
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
sunhuxAuthor Commented:

Anyway, thanks very much Bill, it worked on the local Hard disk, that's all I need
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.