Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to delete the old folder using the attribute. DateLastAccessed

Posted on 2011-10-27
7
Medium Priority
?
235 Views
Last Modified: 2012-08-13
Hi Experts,
I need help with a difficult script that should delete the old folder pair, including all subfolders and files. Criteria for deleting components should be as follows:
 
1) Components are searched in the directory:
% SystemDrive%\Users
2) in the name of the folder must exist a point, but should not be at the beginning or end of the name.
Example folder names:
thomas.meyer -> delete folder
davidculter. -> Will not delete folder
wolfgangpeterson -> will not delete folder

3) If the component passes through the second criterion has to verify the existence of a pair.
This means that the script in addition to the local directory (see point 1) to search the network folder \\S3\HomeS$\ and find the folder that will be its name exactly match that of local.
Example pair of components:
%SystemDrive%\Users\thomas.meyer
\\S3\HomeS$\thomas.meyer

4) The last approach to the components must be older than 270 days (will be using .DateLastAccessed), it would be easy to change this number.
5) The script, along with the names of folders and lists the variable %Computername% to a text file in the \\S2\Temp\del_folder.txt including the appropriate folder to delete it writes a text file
6) Finally, the script folder on the local and server-side deleted.

This script does not mean simple, the more I'll be glad for all of your help.
Thank you in advance.
TM
0
Comment
Question by:Thomas_Meyer
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 37038968
Try the following...
'On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")

SysDrive= WshShell.ExpandEnvironmentStrings("%Systemdrive%")
strcomputer=WshShell.ExpandEnvironmentStrings("%Computername%")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(SysDrive&"\users")
Set file = fs.OpenTextFile("\\S2\Temp\del_folder.txt",8)
Set fc = f.SubFolders
delfol=""

For Each f1 In fc
    flname=f1.Name
    
    If (Right(flname,1)<>"." And Left(flname,1)<>"." And InStr(flname,".")> 0) Then 
        
        If fs.FolderExists("\\S3\HomeS$\"&flname) And DateDiff("n",f1.DateLastModified,Now)>270 Then
            file.WriteLine strcomputer&","&flname
            delfol=delfol&f1.Path&";"&"\\S3\HomeS$\"&flname&";"
        End If
        
    End If
Next

delfile=Split(delfol,";")

For i=0 To UBound(delfile)
	Err.Clear
	fs.DeleteFolder(delfile(i))
	WScript.Echo delfile(i) &" - Deleted"
Next

Open in new window

0
 
LVL 12

Expert Comment

by:prashanthd
ID: 37038994
Missed something..try this
'On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")

SysDrive= WshShell.ExpandEnvironmentStrings("%Systemdrive%")
strcomputer=WshShell.ExpandEnvironmentStrings("%Computername%")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(SysDrive&"\users")
Set file = fs.OpenTextFile("\\S2\Temp\del_folder.txt",8)
Set fc = f.SubFolders
delfol=""

For Each f1 In fc
    flname=f1.Name
    
    If (Right(flname,1)<>"." And Left(flname,1)<>"." And InStr(flname,".")> 0) Then 
        
        If fs.FolderExists("\\S3\HomeS$\"&flname) And DateDiff("n",f1.DateLastAccessed,Now)>270 Then
            file.WriteLine strcomputer&","&flname
            delfol=delfol&f1.Path&";"&"\\S3\HomeS$\"&flname&";"
        End If
        
    End If
Next

delfile=Split(delfol,";")

For i=0 To UBound(delfile)
	Err.Clear
	fs.DeleteFolder(delfile(i))
	WScript.Echo delfile(i) &" - Deleted"
Next

Open in new window

0
 

Author Comment

by:Thomas_Meyer
ID: 37053592
Sorry that I did not respond, I had problems with internet connection.
 I tried your script and report me an error: 800A0046 - Permission denied. This is the eighth row:
 Set file = fs.OpenTextFile ("\\S2\Temp\del_folder.txt", 8)
 It can not be a problem such as missing file.Close?
http://www.eggheadcafe.com/microsoft/VBScript/31740824/error-800a0046-when-trying-to-copy-file-locally.aspx
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!

 
LVL 12

Expert Comment

by:prashanthd
ID: 37055624
Do you have write permissions to that folder and is that folder shared?

You can add file.close at the end, but the script finishes execution, it closes the file..
0
 

Author Comment

by:Thomas_Meyer
ID: 37057629
permission, of course I have. It must be about access to the file, I tried this script with no problem and I wrote the value into a text file:

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("\\S2\Temp\del_folder.txt", ForAppending, True)
Set colServices =  GetObject("winmgmts:").ExecQuery _
    ("Select * from Win32_Service")
For Each objService in colServices    
    objTextFile.WriteLine(objService.DisplayName & vbTab & _
        objService.State)
Next
objTextFile.Close
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 2000 total points
ID: 37061682
Try the following, the only change is to create the file if it does not exist.
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")

SysDrive= WshShell.ExpandEnvironmentStrings("%Systemdrive%")
strcomputer=WshShell.ExpandEnvironmentStrings("%Computername%")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(SysDrive&"\users")
Set file = fs.OpenTextFile("\\S2\Temp\del_folder.txt",8,True)
Set fc = f.SubFolders
delfol=""

For Each f1 In fc
    flname=f1.Name
    
    If (Right(flname,1)<>"." And Left(flname,1)<>"." And InStr(flname,".")> 0) Then 
        
        If fs.FolderExists("\\S3\HomeS$\"&flname) And DateDiff("n",f1.DateLastAccessed,Now)>270 Then
            file.WriteLine strcomputer&","&flname
            delfol=delfol&f1.Path&";"&"\\S3\HomeS$\"&flname&";"
        End If
        
    End If
Next

file.close

delfile=Split(delfol,";")

For i=0 To UBound(delfile)
	Err.Clear
	fs.DeleteFolder(delfile(i))
	WScript.Echo delfile(i) &" - Deleted"
Next

Open in new window

0
 

Author Closing Comment

by:Thomas_Meyer
ID: 37071708
Ok, thanks for your help - it's working.
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

580 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