Link to home
Start Free TrialLog in
Avatar of MSC-HelpdeskIT
MSC-HelpdeskITFlag for Australia

asked on

Shared folder contents move

I have a shared folder that contains last nights backups. F:\Backup\

I would like to set up a job (a script that I can schedule) that makes a new folder, inside a different folder, with a name (I.e. Backup_Archive) and todays date.  F:\Backup_Archive\Backup_26-02-09

I then need to delete the any folders older than 2 days. F:\Backup_Archive\Backup_23-02-09 and earlier

I then move the contents of the shared folder into it. F:\Backup\...   ->  F:\Backup_Archive\Backup_26-02-09\
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

vbs for a beginning

Set objFSO = CreateObject("Scripting.FileSystemObject")

strMonth = Month(Date)

If Len(strMonth) = 1 Then
    strMonth = "0" & strMonth
End If

strDay = Day(Date)

If Len(strDay) = 1 Then
    strDay = "0" & strDay
End If

strYear = Year(Date)

strFolderName = "C:\Backup_" & strYear & "-" & strMonth & "-" & strDay

Set objFolder = objFSO.CreateFolder(strFolderName)
Set objFolder = objFSO.GetFolder(strFolderName)

For Each efile in objFolder.Files
 If DateDiff("d",eFile.DateLastModified,Now) >= 2 Then
   WScript.Echo "file found is more than 2 days old: " & efile
   objFSO.DeleteFile(eFile)
 End If
Next


if you are able to wait :) i can provide you with the full solution as I am using exactly the same thing (in a batch or in a vbs)
Thanks very much for your response.

I would really appreciate seeing your solution to the problem and am happy to wait.

Thanks.
may be a bit messy but it works like a charm

Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strMonth = Month(Date)
 
If Len(strMonth) = 1 Then
    strMonth = "0" & strMonth
End If
 
strDay = Day(Date)
 
If Len(strDay) = 1 Then
    strDay = "0" & strDay
End If
 
strYear = Year(Date)
 
strFolderName = "f:\Backup_Archive\Backup_" & strDay & "-" & strMonth & "-" & strYear
 
 
 
' create the folder here
 
Set objFolder = objFSO.CreateFolder(strFolderName)
Set objFolder = objFSO.GetFolder(strFolderName)
 
 
 
' delete the older dirs
 
Dim i, f, f1, sf, BasePath, CalcResult, fNameArray()
BasePath = "f:\Backup_Archive"
Set f = objFso.GetFolder(BasePath)
Set sf = f.SubFolders
For Each f1 in sf
     CalcResult = DateDiff("d",f1.DateCreated,Now)
          if CalcResult > 2 then
                ReDim preserve fNameArray(i)
                fNameArray(i) = f1.Name
                i = i + 1
        end if
Next
 
For Each fName in fNameArray
    objFso.DeleteFolder(BasePath & "\" & fName)
Next
 
 
msgbox objFolder
 
objFso.CopyFolder objFolder, "f:\Backup\" ,True

Open in new window

Avatar of MSC-HelpdeskIT

ASKER

That works a treat up to a point.  :-)

it creates the backup folder    (F:\backup_Archive\Backup_06-03-2009)
deletes the folders older than 2 days old from within  F:\backup_Archive

but it then creates another backup folder in the origional location (F:\backup\Backup_06-03-2009)
and then it errors out.

I tried changing the code to objfso.CopyFolder "f:\Backup\", objFolder ,True
     this errors out (maybe because the folder is shared out?)

I then tried changing the code to objfso.CopyFolder "f:\Backup\svros\", objFolder ,True
     this copies the contents of f:\Backup\svros\ to F:\backup_Archive\Backup_06-03-2009\
     so this sort of works

But what I would really like it to do is to move the folders within f:\Backup to F:\backup_Archive\Backup_06-03-2009  Is this possible?

Thanks again for all the help.

ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry to be  a real pain, but can the last step be made a move of all the sub folders rather than a copy?

A copy of all the folders will take over 4 hours, while I can move in 30 secs and just recreate all the folders with a script.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial