#161638 - 2006-05-11 03:37 PM
Re: Removing old profiles with a script.
|
matthewst
Getting the hang of it
Registered: 2005-01-26
Posts: 89
|
Thanks everyone for the suggestions!! I'll be testing them all today.
|
Top
|
|
|
|
#161639 - 2006-05-11 05:43 PM
Re: Removing old profiles with a script.
|
jadewith
Fresh Scripter
Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
|
Quote:
just saying that you should have a lot of checks in place to make sure you don't delete the wrong thing.
I totally agree!
Maybe something like...? Code:
$files = Dirplus('c:\documents and settings') for each $file in $files
If $file = 'Administrator' next endif ; along with added "IF" statements for profiles you are not ever going to delete
do ? $file ?'Would you like to delete the profile '$file' ? y/n>' gets $deleteIt until $deleteIt if $deleteIt='y' do ? $file ? 'Are you sure you want to delete this profile? Y/N>' gets $confirm until $confirm
endif
if $confirm = 'y' rd $file endif
next
|
Top
|
|
|
|
#161641 - 2006-05-11 06:18 PM
Re: Removing old profiles with a script.
|
matthewst
Getting the hang of it
Registered: 2005-01-26
Posts: 89
|
I must be half retarted because I can't get either one of these to work.
NTDOC: I uncommented everything but was unsure which 0 to make a 1 or which $Del needed a 1.
Jadewith: Your code asked me y/n to delete, but I couldn't get it to delete anything. I tried changing rd $file to shell '%ComSpec% /c rd /s /q "$file" >nul 2>nul' but still no luck.
|
Top
|
|
|
|
#161643 - 2006-05-11 06:32 PM
Re: Removing old profiles with a script.
|
matthewst
Getting the hang of it
Registered: 2005-01-26
Posts: 89
|
When I run that code it changes the title bar of the dosbox to 6338890/2580 for a split second but doesn't delete anything.
Thanks everyone for your patientience and help.
|
Top
|
|
|
|
#161644 - 2006-05-11 07:58 PM
Re: Removing old profiles with a script.
|
matthewst
Getting the hang of it
Registered: 2005-01-26
Posts: 89
|
Halfway there!!!!
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' SHELL %COMSPEC%+' /C RD "' + $file +'" /S /Q' ;rd $file ;shell '%ComSpec% /c rd /s /q "$file" >nul 2>nul' endif next
That code will clean eveything out of the profile but gives the ntuser.dat file is in use error and it leaves the user.profile folder in the documents and settings directory.
|
Top
|
|
|
|
#161645 - 2006-05-11 08:39 PM
Re: Removing old profiles with a script.
|
jadewith
Fresh Scripter
Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
|
Quote:
Jadewith: Your code asked me y/n to delete, but I couldn't get it to delete anything. I tried changing rd $file to shell '%ComSpec% /c rd /s /q "$file" >nul 2>nul' but still no luck.
That's because "RD" only works on empty directories. (Big DUH on my part for testing with an empty directory)
Here is adjusted code with a single conformation and a little help from Jooel Code:
break on
Dim $SO dim $files dim $file dim $deleteIt dim $confirm
$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 If not instr($file,'Administrator') $file='"'+$file+'\"' do ? $file ?'Would you like to delete this profile? y/n>' gets $deleteIt until $deleteIt if $deleteIt='y' do ? $file ? 'Are you sure you want to delete this profile? Y/N>' gets $confirm until $confirm endif if $confirm = 'y' SHELL '%COMSPEC% /C RD ' + $file+' /S /Q' ?@error @serror sleep 1 endif endif $deleteIt = '' $confirm = '' next
|
Top
|
|
|
|
#161647 - 2006-05-11 09:40 PM
Re: Removing old profiles with a script.
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
Okay, reading Jade solution I see that it is not exceptable for most Admins, but may work if all you want/need to support is that folder structure.
1. If folder is located on D: E: etc won't work 2. If folder has been renamed won't work 3. Will not support WinNT upgraded systems with profiles typically in C:\WinNT\Profiles 4. Does not address registry cleanup like Christophe's does.
Code:
$files = Dirplus('c:\documents and settings')
for each $file in $files If not instr($file,'Administrator') $file='"'+$file+'\"' do ? $file ?'Would you like to delete this profile? y/n>' gets $deleteIt until $deleteIt if $deleteIt='y' do ? $file ? 'Are you sure you want to delete this profile? Y/N>' gets $confirm until $confirm endif if $confirm = 'y' SHELL '%COMSPEC% /C RD ' + $file+' /S /Q' ?@error @serror sleep 1 endif endif $deleteIt = '' $confirm = '' next
|
Top
|
|
|
|
#161648 - 2006-05-11 10:13 PM
Re: Removing old profiles with a script.
|
jadewith
Fresh Scripter
Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
|
Last try with a better conformation of deletion Code:
call dirplus.udf Dim $SO dim $files dim $file dim $deleteIt dim $confirm dim $confirmIt dim $rc $rc=srnd(@mdayno) $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On')
$files = Dirplus('c:\documents and settings')
for each $file in $files If not instr($file,'Administrator') and not instr($file,'All Users') ;...and not instr($file,'any other profile') $file='"'+$file+'\"' $confirm=rnd($rc) ;sets a random conformation number for later do ? $file ?'Would you like to delete this profile? y/n>' gets $deleteIt until $deleteIt if $deleteIt='y' do ? $file ? 'If you sure you want to delete this profile' ? 'type the number you see here '+$confirm+' >'gets $confirmit ;has you enter a conformation number to delete until $confirmit Select case $confirmit = $confirm ;deletes the profile in question SHELL '%COMSPEC% /C RD ' + $file+' /S /Q' ?@error @serror sleep 1 case $confirmit <> $confirm ? 'Comformation incorrect! Action Cancelled!' ? $file+'Was not removed' sleep 1 endselect endif endif cls $deleteIt = '' $confirm = '' $confirmit = '' next
|
Top
|
|
|
|
#161649 - 2006-05-11 10:39 PM
Re: Removing old profiles with a script.
|
jadewith
Fresh Scripter
Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
|
Quote:
1. If folder is located on D: E: etc won't work 2. If folder has been renamed won't work 3. Will not support WinNT upgraded systems with profiles typically in C:\WinNT\Profiles 4. Does not address registry cleanup like Christophe's does.
Ok Doc, You are OBVIOUSLY a better KiXtart coder than me. I know that and I bow to it. But I would like to address your four points.
1.& 3. If you change the parameter in the line $files = dirplus() You can send the script anywhere you want. ie: c:\winnt\profiles or \\somecomputer\e$\documents and settings 2. If the Network Admin doesn't know that the directory holding the profiles has been renamed, You've got WAY bigger problems than this script not working. 4. You are absolutly correct. But then I know I'm not good enough a KiXtarter to go suggesting registry hacks to ANYONE. I leave that to you and the other moderators.
I know my script doesn't just go in, delete directories, and get out. It seemed to me though that what he was looking to do was pick and choose which profiles to delete whenever he ran the script.
|
Top
|
|
|
|
#161651 - 2006-05-11 11:09 PM
Re: Removing old profiles with a script.
|
jadewith
Fresh Scripter
Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
|
And if you change it to this you can send the script wherever you want Code:
call dirplus.udf Dim $SO dim $files dim $file dim $deleteIt dim $confirm dim $confirmIt dim $rc dim $path $rc=srnd(@mdayno) $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On')
do ?'Please enter the path';enter the path to the directory that holds the profiles ?'>' gets $path until $path
$files = Dirplus($path)
for each $file in $files If not instr($file,'Administrator') and not instr($file,'All Users') ;...and not instr($file,'any other profile') $file='"'+$file+'\"' $confirm=rnd($rc) ;sets a random conformation number for later do ? $file ?'Would you like to delete this profile? y/n>' gets $deleteIt until $deleteIt if $deleteIt='y' do ? $file ? 'If you sure you want to delete this profile' ? 'type the number you see here '+$confirm+' >'gets $confirmit ;has you enter a conformation number to delete until $confirmit Select case $confirmit = $confirm ;deletes the profile in question SHELL '%COMSPEC% /C RD ' + $file+' /S /Q' ?@error @serror sleep 1 case $confirmit <> $confirm ? 'Comformation incorrect! Action Cancelled!' ? $file+'Was not removed' sleep 1 endselect endif endif cls $deleteIt = '' $confirm = '' $confirmit = '' next
|
Top
|
|
|
|
#161652 - 2006-05-11 11:16 PM
Re: Removing old profiles with a script.
|
jadewith
Fresh Scripter
Registered: 2003-06-13
Posts: 45
Loc: Good ole U S of A
|
I never took offence Doc,
You have helped me loads in the past, and I respect your abilities. I was being sincere with what I said before. OK maybe I was a little flustered, but not offended.
|
Top
|
|
|
|
#161655 - 2006-05-12 01:16 PM
Re: Removing old profiles with a script.
|
matthewst
Getting the hang of it
Registered: 2005-01-26
Posts: 89
|
You guys kick a$$!! I'll give these versions a shot and post back ASAP.
Thanks again!
|
Top
|
|
|
|
#161656 - 2006-05-15 04:39 PM
Re: Removing old profiles with a script.
|
matthewst
Getting the hang of it
Registered: 2005-01-26
Posts: 89
|
The confirmation number script is "flippin sweet", but for simplicity in automation I decided to use the script from post #161870. Slightly modified to exclude my profile and a few others.
I couldn't have done it without you guys, Thanks!!
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 466 anonymous users online.
|
|
|