• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

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\
0
MSC-HelpdeskIT
Asked:
MSC-HelpdeskIT
  • 4
  • 2
2 Solutions
 
Lukasz ChmielewskiCommented:
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)
0
 
Jason_GuthrieCommented:
Thanks very much for your response.

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

Thanks.
0
 
Lukasz ChmielewskiCommented:
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MSC-HelpdeskITAuthor Commented:
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.

0
 
Lukasz ChmielewskiCommented:
so it must have been my misunderstanding, if you need to copy contents of backup TO 06.03 then just switch the last line folders like this:
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 "f:\Backup\",objFolder ,True

Open in new window

0
 
MSC-HelpdeskITAuthor Commented:
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.
0
 
Lukasz ChmielewskiCommented:
totally forgot about the question
sorry objFso.CopyFolder -> objFso.MoveFolder

but I guess you're far away from then
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now