Page 1 of 2 12>
Topic Options
#161617 - 2006-05-05 02:31 PM Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
I'm trying to use this script to remove some old profiles but I kept getting this error.

Script error: Error in expression.!
Del $LPath + "\*.*" /c /f /h ;delete files in Folder root


So I changed that line to this.

shell '%ComSpec% /c rd /s /q "$LPath + "\*.*"" >nul 2>nul'

When that runs it delets everything in the current directory.
Here is the output.

NTUSER.DAT as of 5555555
Profile of User = . as of is deleted
NTUSER.DAT as of
Profile of User = . as of is deleted

I need to be able to delete SPECIFIC older profiles with a script(vbs, bat, kix, au3 are all acceptable).
Any suggestions?


Code:
;************************************************************************
;
;DelMachineProfiles
;
;************************************************************************
;Script to delete old profiles *
; *
;written by J.Maassen (www.JoeM.de) *
; *
;created on 11th october 2005 *
; *
;last time changed 12.oct.2005 18:10 utc *
;************************************************************************


$LPath="C:\Documents and Settings" ;path where the profiles are located
$Div = 31 ;number of days, the ntuser.dat which is going to be deleted should not written
$Result = ""

Del $LPath + "\*.*" /c /f /h ;delete files in Folder root

$User = DIR($LPath) ;read folder name

while $User <> "" and @ERROR = 0
;-------------------------------------------------------------------
;do not delete profile of some special users
;maybe this has to be changed on OS with an other language
;please check to adapt
;-------------------------------------------------------------------
if $User <> "Default User" AND $USER <> "All Users" AND $User <> "." AND $User <> ".."
$Result = GetFileTime($Lpath + $User + "\NTUSER.DAT") ;get date of file NTUSER.DAT
$ResultY = Substr ($Result,1,4)
$ResultM = Substr ($Result,6,2)
$ResultD = Substr ($Result,9,2)
$ResultS = VAL($ResultY) * 364 + VAL($ResultM) * 30 + VAL($ResultD) ;count a number of that date
?"NTUSER.DAT as of " + $Result
$DateY = Substr (@Date,1,4)
$DateM = Substr (@Date,6,2)
$DateD = Substr (@Date,9,2)
$DateS = VAL($DateY) * 364 + VAL($DateM) * 30 + VAL($DateD) ;count a number of the actual date
;-------------------------------------------------------------------
;I used the 364 and the 30 to have better a smaller number and maybe keep a profile one or two days longer
;better than deleting a profile too early
;-------------------------------------------------------------------
if $ResultS < $DateS - $Div ;Filedate older than actualdate minus $Div
$DPath = $Lpath + $User
DeleteFolder ($DPath) ;next step! delete folderstructure
;write log
?"Profile of User = " + $user + " as of " + $Result + " is deleted"
endif
endif
$User = DIR()
Loop

Exit

;************************************************************************

Function DeleteFolder ($path)

;************************************************************************
;Script to delete a whole Folderstructure even if it is not empty *
; *
;written by J.Maassen (www.JoeM.de) *
; *
;created on 11th october 2005 *
; *
;last time changed 12.oct.2005 18:10 utc *
;************************************************************************

Dim $file
Del ($path + "\*.*") /c /f /h /s ;delete all files in folder structure
$file = Dir($path + "\*.*") ;read folder name, there are no more any files in the folders
While $file <> "" AND @ERROR = 0 ;no error = subfolders exist
If $file <> "." AND $file <> ".." ;do not use the fist two folders
SetFileAttr($path + "\" + $file, 128) ;set folder attributes to normal
DeleteFolder ($path + "\" + $file) ;therefor that it is a subfolder call function recursively
RD ($DPath + "\" + $file) ;delete the folder (it is an empty folder, at least now)
EndIf
$file = Dir() ;read next folder name
Loop
RD $path ;after all subfolders are deleted, delete the starting folder

EndFunction



Edited by matthewst (2006-05-05 03:59 PM)

Top
#161618 - 2006-05-05 09:47 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Well one would need Admin rights to delete the other profiles.

We can modify / write your script to work but think a better understanding of what it is you're really attempting to do is.

What version of KiXtart are you using?

Are you wanting to do it locally or remotely?
 

Top
#161619 - 2006-05-08 01:31 PM Re: Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
I have several remote machines that people are allowed to use for a week or two at a time, as you can imagine these computers are full of old unused profiles. From time to time I will need to remove the unused profiles to make room for new ones. I don't want a logon script because I don't want the profiles removed until I am sure it's safe to do so.

What I need is a script that sits on my desktop and waits for me to click it. When activated it will find the machines I have defined in the script, find their profile lists, delete all but the profiles but the ones I specify (Admin, All Users...).

Top
#161620 - 2006-05-08 03:20 PM Re: Removing old profiles with a script.
Schuliebug Offline
Hey THIS is FUN
*****

Registered: 2002-01-18
Posts: 379
Loc: Netherlands
Why not at logon, if you delete the profiles older than a certain amount of time? I use a cleanup script at logon, which 'cleans' profiles for a part: we keep 'Local Settings\Application Data' and the ntuser.* files.

We use roaming profiles and Outlook 2003 (ahumm) as a mail client and if we completely remove the profile, Outlook gives an error when a older user (profile dir earlier deleted) logges on. Somehow Outlook needs some files in the 'Local Settings\Application Data' directory.
_________________________
Kind regards,

Top
#161621 - 2006-05-08 03:35 PM Re: Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
I think you're getting an error because outlook profiles and xp profiles are kept in different directories.

I can't use a logon script because my users don't have admin priviliges. Even if they did I would have to add each one to the list of "do not deletes" in the script before they logged on. I think it would be eaiser to just wait intil a set of users doesn't need to logon on any more then delete them all at once.

Top
#161622 - 2006-05-08 05:25 PM Re: Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
To put it simply I need a script that will:
go to a remote machine that I specify
open a directoy
delete any files in that directory except the ones I specify (all users, admin, my.profile)

Top
#161623 - 2006-05-08 07:33 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Okay, but you did not specify what version of KiXtart you're using.

If I get time I'll try to post something later today that should work with KiXtart 4.52 beta 2 only.

If you can't use KiXtart 4.52 beta 2 then you'll need to code something on your own.
 

Top
#161624 - 2006-05-08 08:36 PM Re: Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
sorry..i'm using 4.52 beta 2
Top
#161625 - 2006-05-09 05:53 PM Re: Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
I stumbled across this posted by sealeopard.
I can't get it to work either.

Code:
$maxprofileage=20
$profilelist=$HKLM+'\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
$profiles=arrayenumkey($profilelist)

for each $usersid in $profiles
if $usersid<>'' and right($usersid,4)<>'-500'
$username=sidtoname($usersid)
$username=right($username,len($username)-instrrev($username,'\'))

;I added this*********
if $username='my.profile'
exit
endif
;*********************

if $username=''
$username='account unknown: '+$usersid
endif
$profiledir=expandenvironmentvars(readvalue($profilelist+'\'+$usersid,'ProfileImagePath'))
if $profiledir<>''
$userprofiles=dirlist($profiledir+'.*',1)
for each $userprofile in $userprofiles
$profiledate=getfiletime($userprofile+'\ntuser.dat')
$profiledate=left($profiledate,10)
if datemath($profiledate,@DATE)>$maxprofileage
$shellcommand='rd /s /q '+$userprofile
shell '%COMSPEC% /c '+$shellcommand
if $userprofile=$profiledir
$retcode=delkey($profilelist+'\'+$usersid)
endif
endif
next
endif
endif
next



Edited by matthewst (2006-05-09 06:35 PM)

Top
#161626 - 2006-05-09 08:34 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Okay, who is deciding when or how the age of the profile is decided? You want to decide on something like 90 days, 120 days? or are you manually deciding what is old?

Do you need to support NT4 style profiles that are stored typically under C:\WinNT\Profiles

 

Top
#161627 - 2006-05-09 08:42 PM Re: Removing old profiles with a script.
matthewst Offline
Getting the hang of it

Registered: 2005-01-26
Posts: 89
I was just planning to run the script myself every 30 days or so. These will all be XP profiles.
Top
#161628 - 2006-05-09 09:31 PM Re: Removing old profiles with a script.
jadewith Offline
Fresh Scripter

Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
I haven't tested but maybe this is a way to go...

using the dirplus() UDF
Code:
 $files = Dirplus('\\ [i]path[/i] \documents and settings\')
for each $file in $files
do ?'Would you like to delete this profile? y/n>' gets $deleteIt
until $deleteIt
if $deleteIt='y'
del '\\[i]path[/i]\documents and settings\$file'
endif
next




Maybe I'm an idiot but...

Top
#161629 - 2006-05-09 09:40 PM Re: Removing old profiles with a script.
jadewith Offline
Fresh Scripter

Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
sorry you'd probably want to use

RD not del

Top
#161630 - 2006-05-09 10:55 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Give that code a run and when you get back let us know how it went.

In the mean time I'll continue working on another example.

Top
#161631 - 2006-05-10 02:12 AM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Okay Matthewst,

Here is some example code WARNING! USE AT YOUR OWN RISK!!

This UDF does not check to verify if the remote machine is running or if you have Admin rights on the system which you must have.
It does not check who is logged on or if anyone is accessing any of the files within the profile.
It simply checks the last write time against the current time and determines how old the NTUSER.DAT file is.

You can set the age to any date you want for removing the profiles.

PARAMETERS
$Days = Number of days before the profile will be considered OLD
$sComputer = The name of the remote computer, if left blank the current machine is used
$Del = Flag to indicate if you want to remove the profile or simply display the profile. A 1 would mean to delete the profile.


 
Again - Test and Use at your own risk
I highly recommend using a Virtual machine or other Test machine first to verify this code operates as expected.

I have done minimal testing and it appears to work, but due to the nature of this
type of code any errors could be quite dangerous.
 
Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

RemoveOldProfiles(90,'SomeRemoteComputer',0)
;change the 0 to a 1 and uncomment code below where shown to actually remove the profiles

Function RemoveOldProfiles($Days, Optional $sComputer, $Del)
Dim $ProfilePath, $ProfileList, $PL
Dim $SystemRoot, $SystemDrive, $Age
$sComputer=IIf(Not $sComputer,'','\\'+Join(Split($sComputer,'\'),'',3)+'\')
$ProfilePath = Split(ReadValue($sComputer+'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList','ProfilesDirectory'),'\')[1]
$SystemRoot = ReadValue($sComputer+'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion','SystemRoot')
$SystemDrive = Left($SystemRoot,1)
Select
Case $sComputer = "" ; LOCAL COMPUTER
If InStr($ProfilePath,'Documents and Settings')
$ProfileList = ExpandEnvironmentVars(ReadValue('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList','ProfilesDirectory'))
$PL = Dir($ProfileList)
While $PL <>''
If $PL <>''
If Not InStr($PL,'..')
And Not InStr($PL,'All Users') And Not InStr($PL,'Default User')
And Not InStr($PL,'LocalService') And Not InStr($PL,'NetworkService')
If Exist($ProfileList+'\'+$PL+'\'+'NTUSER.DAT')
$Age = DateCalc(@Date,SubStr(GetFileTime($ProfileList+'\'+$PL+'\'+'NTUSER.DAT'),1,10))
If $Age > $Days
'' + $sComputer+ $PL + ' : ' + $Age + ' days old.' ?
;To actually remove the profile uncomment the code below
'Would remove profile: ' + $ProfileList+'\'+$PL ?
;If Len($ProfileList+'\'+$PL) >9
;If $Del = 1
;RD $ProfileList+'\'+$PL /S
;EndIf
;EndIf
EndIf
EndIf
EndIf
EndIf
$PL = Dir()
Loop
Else ; LOCAL COMPUTER
If GetFileAttr($SystemRoot+'\'+'Profiles') & 16
$ProfileList = $SystemRoot+'\'+'Profiles'
$PL = Dir($ProfileList)
While $PL <>''
If $PL <>''
If Not InStr($PL,'..')
And Not InStr($PL,'All Users') And Not InStr($PL,'Default User')
And Not InStr($PL,'LocalService') And Not InStr($PL,'NetworkService')
If Exist($ProfileList+'\'+$PL+'\'+'NTUSER.DAT')
$Age = DateCalc(@Date,SubStr(GetFileTime($ProfileList+'\'+$PL+'\'+'NTUSER.DAT'),1,10))
If $Age > $Days
'' + $sComputer+ $PL + ' : ' + $Age + ' days old.' ?
;To actually remove the profile uncomment the code below
'Would remove profile: ' + $ProfileList+'\'+$PL ?
;If Len($ProfileList+'\'+$PL) >9
;If $Del = 1
;RD $ProfileList+'\'+$PL /S
;EndIf
;EndIf
EndIf
EndIf
EndIf
EndIf
$PL = Dir()
Loop
EndIf
EndIf
Case $sComputer <> "" ; REMOTE COMPUTER
If InStr($ProfilePath,'Documents and Settings')
$ProfileList = $sComputer+$SystemDrive+'$'+'\'+'Documents and Settings'
$PL = Dir($ProfileList)
While $PL <>''
If $PL <>''
If Not InStr($PL,'..')
And Not InStr($PL,'All Users') And Not InStr($PL,'Default User')
And Not InStr($PL,'LocalService') And Not InStr($PL,'NetworkService')
If Exist($ProfileList+'\'+$PL+'\'+'NTUSER.DAT')
$Age = DateCalc(@Date,SubStr(GetFileTime($ProfileList+'\'+$PL+'\'+'NTUSER.DAT'),1,10))
If $Age > $Days
'' + $sComputer+ $PL + ' : ' + $Age + ' days old.' ?
;To actually remove the profile uncomment the code below
'Would remove REMOTE profile: ' + $ProfileList+'\'+$PL ?
;If Len($ProfileList+'\'+$PL) >9
;If $Del = 1
;SHELL '%COMSPEC% /C RD ' + $sComputer+$SystemDrive+'$'+'\'+'DOCUME~1'+'\'+$PL+'\' +' /S /Q'
;'ERROR: ' + @ERROR + ' - ' + @SERROR ?
;EndIf
;EndIf
EndIf
EndIf
EndIf
EndIf
$PL = Dir()
Loop
Else ; REMOTE COMPUTER
If GetFileAttr($sComputer+$SystemDrive+'$'+'\'+Right($SystemRoot,-3)+'\'+'Profiles') & 16
$ProfileList = $sComputer+$SystemDrive+'$'+'\'+Right($SystemRoot,-3)+'\'+'Profiles'
$PL = Dir($ProfileList)
While $PL <>''
If $PL <>''
If Not InStr($PL,'..')
And Not InStr($PL,'All Users') And Not InStr($PL,'Default User')
And Not InStr($PL,'LocalService') And Not InStr($PL,'NetworkService')
If Exist($ProfileList+'\'+$PL+'\'+'NTUSER.DAT')
$Age = DateCalc(@Date,SubStr(GetFileTime($ProfileList+'\'+$PL+'\'+'NTUSER.DAT'),1,10))
If $Age > $Days
'' + $sComputer+ $PL + ' : ' + $Age + ' days old.' ?
;To actually remove the profile uncomment the code below
'Would remove REMOTE profile: ' + $ProfileList+'\'+$PL ?
;If Len($ProfileList+'\'+$PL) >9
;If $Del = 1
;SHELL '%COMSPEC% /C RD ' + $sComputer+$SystemDrive+'$'+'\'+Right($SystemRoot,-3)+'\'+'Profiles'+'\'+$PL+'\' +' /S /Q'
;'ERROR: ' + @ERROR + ' - ' + @SERROR ?
;EndIf
;EndIf
EndIf
EndIf
EndIf
EndIf
$PL = Dir()
Loop
EndIf
EndIf
Case 1
;unable to determine profile location
Exit 3
EndSelect
EndFunction

Function DateCalc($date1, $DateOrMod, optional $SingleDigit)
Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = Split($date1,'/')
If UBound($date1) <> 2
Exit 1
EndIf
$_intYear1 = Val($date1[0])
$_intMonth1 = Val($date1[1])
$_intDay1 = Val($date1[2])
If $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
EndIf
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
Select
Case VarType($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf
If Not $SingleDigit
If Len($_intYear2 ) < 4
$_ = Execute("for $i=1 to 4-Len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
EndIf
$_intMonth2 = Right("0" + $_intMonth2,2)
$_intDay2 = Right("0" + $_intDay2,2)
EndIf
$DateCalc = '' + $_intYear2 + '/' + $_intMonth2 + '/' + $_intDay2
Case VarType($DateOrMod) = 8
$DateOrMod = Split($DateOrMod,'/')
If UBound($DateOrMod) <> 2
Exit 1
EndIf
$_intYear2 = Val($DateOrMod[0])
$_intMonth2 = Val($DateOrMod[1])
$_intDay2 = Val($DateOrMod[2])
If $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
EndIf
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line If you wish to return negative results also !!!
If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf
Case 1
Exit 1
EndSelect
EndFunction

Top
#161632 - 2006-05-10 11:38 AM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
    Example output RemoveOldProfiles(90) on local machine
     
  • Administrator : 91 days old.
  • Would remove profile: C:\Documents and Settings\Administrator
  • BillW : 104 days old.
  • Would remove profile: C:\Documents and Settings\BillW

 

Top
#161633 - 2006-05-10 05:37 PM Re: Removing old profiles with a script.
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
NTDOC, your code seems to work correctly.

I have made some change to not write four times the same block.
I also add some deletions in the registry if entries point to a non existant directory.

Code:
Break On

Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

;?"== local =="
;RemoveOldProfiles(365,'',0)
;
;?"== remote =="
;RemoveOldProfiles(365,'remote computer name',0)
;
;change the 0 to a 1 and uncomment code below where shown to actually remove the profiles

dim $x
? "== terminated =="
gets $x


Function RemoveOldProfiles($Days, Optional $sComputer, $Del)
Dim $ProfilePath, $SystemRoot, $SystemDrive

$sComputer = IIf(Not $sComputer,'','\\'+Join(Split($sComputer,"\"),'',3)+"\")
$ProfilePath = ReadValue($sComputer+'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList','ProfilesDirectory')
$SystemRoot = ReadValue($sComputer+'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion','SystemRoot')
$SystemDrive = Left($SystemRoot,2)

If $ProfilePath=""
$ProfilePath = "%%Systemroot%%\Profiles"
EndIf

Select
Case $sComputer = "" ; LOCAL COMPUTER
$ProfilePath = ExpandEnvironmentvars( $ProfilePath )

Case $sComputer <> "" ; REMOTE COMPUTER
$ProfilePath = Replace( $ProfilePath, "%%SystemRoot%%", $SystemRoot )
$ProfilePath = Replace( $ProfilePath, "%%SystemDrive%%", $SystemDrive )

$ProfilePath = $sComputer+left($ProfilePath,1)+'$'+substr($ProfilePath,3)

Case 1 ; should never happend
Exit 3
EndSelect

;-- scan profile dir --
If (GetFileAttr($ProfilePath) & 16)
;-- scan profile directory --
Dim $file, $ProfileDir, $Age

$file = Dir($ProfilePath)
While $file <>''
If ($file<>".") and ($file<>"..") and ($file<>"All Users") And ($file<>"Default User") And
($file<>"'LocalService") And ($file<>"NetworkService")
$ProfileDir = $ProfilePath+"\"+$file
If Exist($ProfileDir+"\"+'NTUSER.DAT')
$Age = DateCalc(@Date,SubStr(GetFileTime($ProfileDir+"\"+'NTUSER.DAT'),1,10))
If $Age > $Days
? "delete directory : "+$ProfileDir+" : "+$Age

if $Del
RemoveDirectory( $ProfileDir )
endif
EndIf
EndIf
EndIf
$file = Dir()
Loop
EndIf

;-- scan registry --
dim $regkey, $subkey, $index, $path

$regkey = $sComputer+"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$Index = 0
do
$subkey = ENUMKEY($regkey, $Index)
If not @ERROR
$Index = $Index + 1

$path = ReadValue($regkey+"\"+$subkey, "ProfileImagePath")
if $path
if $sComputer=""
$Path = ExpandEnvironmentvars( $Path )
else
$Path = Replace( $Path, "%%SystemRoot%%", $SystemRoot )
$Path = Replace( $Path, "%%SystemDrive%%", $SystemDrive )

$Path = $sComputer+left($Path,1)+'$'+substr($Path,3)
endif

if not exist($path)
? "delete regkey : "+$regkey+"\"+$subkey
if $del
dim $

;-- To actually remove the profile uncomment the code below --
;;$=DelTree($regkey+"\"+$subkey)
;;$index = 0 ; must restart key enumeration
endif
endif
endif
endif
until @Error
EndFunction

Function RemoveDirectory( $path )
;-- To actually remove the profile uncomment the code below --
;;SHELL %COMSPEC%+' /C RD "' + $path +'" /S /Q'
EndFunction

Function DateCalc($date1, $DateOrMod, optional $SingleDigit)
Dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
Dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = Split($date1,'/')
If UBound($date1) <> 2
Exit 1
EndIf
$_intYear1 = Val($date1[0])
$_intMonth1 = Val($date1[1])
$_intDay1 = Val($date1[2])
If $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
EndIf
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
Select
Case VarType($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
If InStr($_intDate2,'-') $_intDate2 = Val(SubStr($_intDate2,2,Len($_intDate2)-1)) EndIf
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
If $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 EndIf
If Not $SingleDigit
If Len($_intYear2 ) < 4
$_ = Execute("for $i=1 to 4-Len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
EndIf
$_intMonth2 = Right("0" + $_intMonth2,2)
$_intDay2 = Right("0" + $_intDay2,2)
EndIf
$DateCalc = '' + $_intYear2 + '/' + $_intMonth2 + '/' + $_intDay2

Case VarType($DateOrMod) = 8
$DateOrMod = Split($DateOrMod,'/')
If UBound($DateOrMod) <> 2
Exit 1
EndIf
$_intYear2 = Val($DateOrMod[0])
$_intMonth2 = Val($DateOrMod[1])
$_intDay2 = Val($DateOrMod[2])
If $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
EndIf
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line If you wish to return negative results also !!!
If InStr($DateCalc,'-') $DateCalc = Val(SubStr($DateCalc,2,Len($DateCalc)-1)) EndIf

Case 1
Exit 1
EndSelect
EndFunction

Function Replace($SourceString, $SearchString, $ReplaceString, Optional $First, Optional $CaseSensitive)
Dim $SearchStringLen, $String1, $String2, $Finished, $Counter, $Location

$SearchStringLen = len($SearchString)
$Finished = 0
$Counter = 0
$String1 = $SourceString
if $CaseSensitive
$PreviousState = SetOption("CaseSensitivity", "On")
endif
While Not $Finished
$String2 = $String1
$Location = InStr($String1, $SearchString)
if $Location > 0
$String1 = Substr($String1, 1, $Location - 1) + $ReplaceString + Substr($String1, $Location + $SearchStringLen)
else
$Finished = 1
endif
if $First
$Finished = 1
endif
if $String1 = $String2
$Finished = 1
endif
Loop
$Replace = $String1
if $CaseSensitive
$ = SetOption("CaseSensitivity", $PreviousState)
endif
EndFunction



I wrote something very similar about 6 months ago without any delete, just to log information about older profiles.

The RemoveOldProfiles function can be completed with other scans.
For example, with SMS 2, some logs files should be deleted for the user associated to the profile dir :
with the profile dir, i find the SID which the registry key.
then, i convert the SID to the account name
finally, i look for the SMS complete log files concerning this user.
_________________________
Christophe

Top
#161634 - 2006-05-10 08:03 PM Re: Removing old profiles with a script.
jadewith Offline
Fresh Scripter

Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
OK Doc Sorry, I was just giving a direction he could go without having to base the deletion on a time frame

This will display each existing profile in documents and settings and offer you the choice to delete it or not. you would have to change the parameters of the UDF to the url for a remote system and you would have to have admin permissions on that system (maybe you would want to map a drive to the c:\docs and settings' directory of that system).

Here's the Working code. I tested it locally with a test profile.

Code:
 break on

Dim $SO
dim $files
dim $file
dim $deleteIt

$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')


Function DirPlus($path,optional $Options, optional $f, optional $sfflag)
If not vartype($f) DIM $f EndIf
If not vartype($sfflag) DIM $sfflag EndIf

DIM $file, $i, $temp, $item, $ex1, $mask,$mask1,$maskArray,$maskarray1,
$ex2, $code, $CodeWeight, $targetWeight, $weight, $masktrue
DIM $tarray[0]

$ex1 = SetOption(Explicit,on)
$ex2 = SetOption(NoVarsInStrings,on)
$codeWeight = 0

If not Exist($path)
$temp = SetOption(Explicit,$ex1)
$temp = SetOption(NoVarsInStrings,$ex2)
Exit @ERROR
EndIf

If not vartype($f)
$f = CreateObject("Scripting.FileSystemObject").getfolder($path)
EndIf
If @ERROR
$temp = SetOption(Explicit,$ex1)
$temp = SetOption(NoVarsInStrings,$ex2)
Exit @ERROR
EndIf

For Each $temp In Split($options,"/")
$temp=Trim($temp)
Select
Case left($temp,1) = "s"
If not vartype($sfflag)
If Val(right($temp,-1)) = 0
$sfflag = -1
Else
$sfflag = Val(right($temp,-1))
EndIf
EndIf
Case Left($temp,1) = "a"
Select
Case Right($temp,-1)="d"
$codeWeight = $codeWeight + 1
$temp = "if $file.attributes & 16 " ;"if $file.type = 'File Folder' "
Case Right($temp,-1)="-d"
$codeWeight = $codeWeight + 1
$temp = "if ($file.attributes & 16)=0 " ;"if $file.type <> 'File Folder' "
Case Right($temp,-1)="s"
$codeWeight = $codeWeight + 1
$temp = "if $file.attributes & 4 "
Case Right($temp,-1)="-s"
$codeWeight = $codeWeight + 1
$temp = "if ($file.attributes & 4)=0 "
Case Right($temp,-1)="h"
$codeWeight = $codeWeight + 1
$temp = "if $file.attributes & 2 "
Case Right($temp,-1)="-h"
$codeWeight = $codeWeight + 1
$temp = "if ($file.attributes & 2)=0 "
Case Right($temp,-1)="r"
$codeWeight = $codeWeight + 1
$temp = "if $file.attributes & 1 "
Case Right($temp,-1)="-r"
$codeWeight = $codeWeight + 1
$temp = "if ($file.attributes & 1)=0 "
Case Right($temp,-1)="a"
$codeWeight = $codeWeight + 1
$temp = "if $file.attributes & 32 "
Case Right($temp,-1)="-a"
$codeWeight = $codeWeight + 1
$temp = "if ($file.attributes & 32)=0 "
EndSelect
$code = $temp + "$weight=$weight+1 endif" +@CRLF + $code

Case Left($temp,1) = "m"
$maskarray = Split(Right($temp,-2),"|")
$codeweight = $codeweight + 1
$code = "$masktrue=0 for Each $mask in $maskarray if instr($file.name,$mask) $masktrue=1 " +
"EndIf Next If $masktrue $weight=$weight+1 endif" + @CRLF +$code
Case Left($temp,1) = "f"
$maskarray1 = Split(Right($temp,-2)," ")
$codeweight = $codeweight + 1
$code = "$masktrue=0 for Each $mask1 in $maskarray1 if substr($file.name,Instrrev($file.name,'.')+1)" +
"=$mask1 $masktrue=1 EndIf Next If $masktrue $weight=$weight+1 endif" + @CRLF +$code

EndSelect
Next
$code = "$weight = 0 $targetWeight = " + $codeweight + @CRLF + $code
$code = $code + "if $weight = $targetweight Exit 1 endif"

For Each $file In $f.subfolders
If Execute($code)
$tarray[$i] = $file
$i = $i + 1
ReDIM preserve $tarray[$i]
EndIf
If $sfflag
$temp = dirplus($file,$options,$file,$sfflag-1)
For Each $item In $temp
$tarray[$i] = $item
$i = $i + 1
ReDIM preserve $tarray[$i]
Next
EndIf
Next
For Each $file In $f.files
If Execute($code)
$tarray[$i] = $file
$i = $i + 1

ReDIM preserve $tarray[$i]
EndIf
Next

If $i
ReDIM preserve $tarray[$i-1]
$i=0
Else
$tarray = 0
EndIf

$dirplus = $tarray
$temp = SetOption(Explicit,$ex1)
$temp = SetOption(NoVarsInStrings,$ex2)
Exit @ERROR
EndFunction

$files = Dirplus('c:\documents and settings')
for each $file in $files


do ? $file
?'Would you like to delete the profile '$file' ? y/n>' gets $deleteIt
until $deleteIt
if $deleteIt='y'
rd $file

endif
next


Top
#161635 - 2006-05-11 12:10 AM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
I found that I had to remove this code when tesing here as there are profiles with a "." in the name.

If ($file<>".")

I'll try to take a further look at your example Christopher at work and see how it works out.

Does it also support the NT4 profiles?

Wasn't really trying to Golf it down so much as get it to actually work correctly.
As for SMS, I'm sure that not the only application out there that doesn't appreciate a profile just being rudely removed.

Also, I've found instances of profiles that were not in the Registry so I've stopped relying on the registry as the source of determining all profiles.

Top
#161636 - 2006-05-11 12:12 AM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
No problem jadewith, just saying that you should have a lot of checks in place to make sure you don't delete the wrong thing. I'll have to look at your solution further to see if it supports the different folder locations.
Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.082 seconds in which 0.031 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org