Page 2 of 2 <12
Topic Options
#161637 - 2006-05-11 02:22 PM Re: Removing old profiles with a script.
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
at the beginning, my script was written for NT 4.
with your code, i have made changes so that it also works on recent OS.

sure that SMS is not the only application that doesn't appreciate profile removal. it's just an example.
but I mention it because we have the problem. if we delete a profile without deleting the SMS log files,
publications are not posted again to the user and he has no specific application.

i also verify profiles in the registry because sometime, when a user logs on a workstation,
his profile is created with a .000 (or .001, .002, ...).
If I clear properly the profiles directory AND the registry, i have not the problem.
_________________________
Christophe

Top
#161638 - 2006-05-11 03:37 PM Re: Removing old profiles with a script.
matthewst Offline
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 Offline
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
#161640 - 2006-05-11 06:17 PM Re: Removing old profiles with a script.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
NEXT can't be used in that manner.
only one NEXT can exist for each FOR

you could however do something like:
Code:

$files = Dirplus('c:\documents and settings')
for each $file in $files
If not instr($file,'Administrator')
?'Would you like to delete the profile '$file' ? y/n>'
gets $deleteIt
if $deleteIt='y'
? 'Are you sure you want to delete this profile? Y/N>'
gets $confirm
endif
if $confirm = 'y'
rd $file
endif
endif
next

_________________________
!

download KiXnet

Top
#161641 - 2006-05-11 06:18 PM Re: Removing old profiles with a script.
matthewst Offline
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
#161642 - 2006-05-11 06:20 PM Re: Removing old profiles with a script.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
matthewst, see my cleaned version above.
_________________________
!

download KiXnet

Top
#161643 - 2006-05-11 06:32 PM Re: Removing old profiles with a script.
matthewst Offline
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 Offline
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 Offline
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
#161646 - 2006-05-11 09:31 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Jadewith,

Are you SURE you're using KiXtart 4.52 beta 2?


I think the problem is you're trying to edit the Function which you DON'T DO. (except to uncomment the DEL part)

You simply tell the script what to do, don't change anything else in the function.

The issue with the DirPlus method is that I don't think there are sufficient checks on the actual profiles which are good which are old etc...

Any of these 3 methods can be made to work I'm sure, but I know my posted version works as I've run it.
I'm reasonably sure that Christophe's works too as he has done a lot of coding.

The method posted by Jadewith can work too, but currently I just don't think it has enough sense in it to do correctly.
 

I'll review my code and Christophe's code to combine the best of both and repost an updated version, though I do know that my already posted version works so not sure how you're calling it.
Without modifying my script can you get it to show the profiles it WOULD remove?
 


Edited by NTDOC (2006-05-11 09:36 PM)

Top
#161647 - 2006-05-11 09:40 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
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 Offline
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 Offline
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
#161650 - 2006-05-11 11:06 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Jadewith,

Please don't take offense, on the contrary I think this post will help you to code better.

There are guys here that can code much better than I, but the point is that with practice we all get better at it.

For item #2 actually if you rename the folder location the registry will automatically pick that up and all your other applications will be happy. The point being is to whenever possible stay away from HARD CODING a path or other information unless there just is no other valid way around it.
 
Sorry if you felt I was picking on you - did not mean for it to sound that way. Just wanted you to search further into your coding to see WHY there could be issues, and then code for those issues if possible.

Please don't feel discouraged or that I'm picking on you and continue to post help and suggestions for others.

I still make plenty of mistakes in my coding and I've learned quite a bit over time from others pointing out my mistakes.


Edited by NTDOC (2006-05-11 11:08 PM)

Top
#161651 - 2006-05-11 11:09 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
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 Offline
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
#161653 - 2006-05-11 11:22 PM Re: Removing old profiles with a script.
NTDOC Administrator Offline
Administrator
*****

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

I'm not going to have time to modify the script to mix mine and Christophe's code. Just too much stuff going on at work.

However, all these scripts should work so I think you may need to read the FAQ on using UDFs and / or ask some more questions.

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

Registered: 2000-07-28
Posts: 11623
Loc: CA
Okay I did some testing and Christophe Melin's code seems to work as expected on XP and NT4 / Local and Remote.

A bit odd looking compared to how I code, but none the less after reviewing it - it does appear to work correctly. That REPLACE code was initially a bit misleading for me, but I see how it is working now. Good job Christophe.

So there Mathhewst, at least 2 versions that work and the 3rd one by Jade should also work overall, didn't do any testing of that one though as noted elsewhere in this posting.
 

Top
#161655 - 2006-05-12 01:16 PM Re: Removing old profiles with a script.
matthewst Offline
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 Offline
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
Page 2 of 2 <12


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

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.08 seconds in which 0.028 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