Jump to content

Deleting files in a specified folders for multiple users


Recommended Posts

Hello,

I am in a bit of a struggle with my code. It does not do want i want it to do.

The problem is

In a Windows Terminal Server enviroment, every user has his own profile. In this profile are folders like Cookies for an example.

The files in these folders can be enormous and this makes logging in a long time.

So i have tried to make a script wich will first Query the Active Directory for all users.

Then for every user cleaning up his profile with the folders i specify.

The only thing i am trying to do is creating this script for general use in more situations.

So first this is my code:

$file1 = FileOpen("File1.txt", 0)
; Check if file opened for reading OK
If $file1 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


$file2 = FileOpen("File2.txt", 0)
; Check if file opened for reading OK
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    $line1 = FileReadLine($file1)

    While 1
        $line2 = FileReadLine($file2)
            If @error = -1 Then ExitLoop
        MsgBox("", "Test1", "del " & $line1 & "\" & $line2 & "\*.* /Q")
    Wend

    If @error = -1 Then ExitLoop
    MsgBox("", "Test2", "del " & $line1 & " \*.* /Q")
Wend

FileClose($file1)
FileClose($file2)

File1 contains:

C:\Test\1

C:\Test\3

C:\Test\8

File2 contains:

Cookies

What i am trying is to read one line from file1 and then every line from file2.

If file2 is at his end then the script needs to start at the second line of file1.

But in some strange way or in a way i can't find the problem this doesn't work.

Problably a stupid mistake, but could someone help me a hand?

Edited by Iznogoud
Link to comment
Share on other sites

Hi.

Just one thing I saw is this:

$file1 = FileOpen("File1.txt", 0)
; Check if file opened for reading OK
If $file1 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


$file2 = FileOpen("File2.txt", 0)
; Check if file opened for reading OK
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    $line1 = FileReadLine($file1)

    While 1
        $line2 = FileReadLine($file2)
            If @error = -1 Then ExitLoop
        MsgBox("", "Test1", "del " & $line1 & "\" & $line2 & "\*.* /Q")
    Wend
    ; here you will need to close and reopen file2, otherwise you will stuck at EOF for file2 ;)
    If @error = -1 Then ExitLoop
    MsgBox("", "Test2", "del " & $line1 & " \*.* /Q")
Wend

FileClose($file1)
FileClose($file2)

File1 contains:

C:\Test\1

C:\Test\3

C:\Test\8

File2 contains:

Cookies

and maybe more lines, right? :)

What i am trying is to read one line from file1 and then every line from file2.

If file2 is at his end then the script needs to start at the second line of file1.

.... and at this point you need to start over for file 2 from it's first line, see inserted comment in your script above.

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Hi.

Just one thing I saw is this:

$file1 = FileOpen("File1.txt", 0)
; Check if file opened for reading OK
If $file1 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


$file2 = FileOpen("File2.txt", 0)
; Check if file opened for reading OK
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    $line1 = FileReadLine($file1)

    While 1
        $line2 = FileReadLine($file2)
            If @error = -1 Then ExitLoop
        MsgBox("", "Test1", "del " & $line1 & "\" & $line2 & "\*.* /Q")
    Wend
    ; here you will need to close and reopen file2, otherwise you will stuck at EOF for file2 ;)
    If @error = -1 Then ExitLoop
    MsgBox("", "Test2", "del " & $line1 & " \*.* /Q")
Wend

FileClose($file1)
FileClose($file2)

and maybe more lines, right? :)

.... and at this point you need to start over for file 2 from it's first line, see inserted comment in your script above.

Regards, Rudi.

File1 is just a Testing file and offcourse it will be alot of lines wich are not always the same. I finished a script for that file wich generate a list of users.

File2 contains more lines than one. So for every line in file1, the script needs to execute all lines in file2.

Cookies is one folder, but it could be Temp and Temporary Internet Files etc.

Thats why i created a loop inside a loop but in some way it gets stuck.

Link to comment
Share on other sites

Hi.

File1 is just a Testing file and offcourse it will be alot of lines wich are not always the same. I finished a script for that file wich generate a list of users.

File2 contains more lines than one. So for every line in file1, the script needs to execute all lines in file2.

Cookies is one folder, but it could be Temp and Temporary Internet Files etc.

Thats why i created a loop inside a loop but in some way it gets stuck.

And the file2 has to be opened each time you run the inner loop. If you don't, then you will stuck at the EOF (End Of File) for all other loop 2 processing (2-n).

So after loop2 is done, close file2, then reopen it so that you will again go through all lines of file2.

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Oke, this is into the right direction.

I adjusted the code like this:

$file1 = FileOpen("File1.txt", 0)
; Check if file opened for reading OK
If $file1 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


$file2 = FileOpen("File2.txt", 0)
; Check if file opened for reading OK
If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    $line1 = FileReadLine($file1)

    While 1
        $line2 = FileReadLine($file2)
            If @error = -1 Then ExitLoop
        MsgBox("", "Test1", "del " & $line1 & "\" & $line2 & "\*.* /Q")
    Wend
    
    FileClose($file2)
    
    $file2 = FileOpen("File2.txt", 0)
        If $file2 = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        EndIf
    
    If @error = -1 Then ExitLoop
Wend

FileClose($file1)
FileClose($file2)

The only strange thing i now got if the script is finished reading all the lines in file1.txt it's not stopping. It keeps popping up the message del \Cookies\*.* /Q

I tried to open and close files in any other way, but for some reasing i am missing the whole picture.

Link to comment
Share on other sites

I'd tackle this using _FileReadToArray()

#include <File.au3>

Dim $aProfileList, $aFolderList

If Not _FileReadToArray("File1.txt", $aProfileList) Then
    MsgBox(4096, "Error", " Error reading ProfileList to Array     error:" & @error)
    Exit
EndIf

If Not _FileReadToArray("File2.txt", $aFolderList) Then
    MsgBox(4096, "Error", " Error reading FolderList to Array     error:" & @error)
    Exit
EndIf


For $iProfile = 1 To $aProfileList[0]
    $Profile = $aProfileList[$iProfile]
    If StringStripWS($Profile, 3) = "" Then ContinueLoop ;if line is blank then skip it
    For $iFolder = 1 To $aFolderList[0]
        $Folder = $aFolderList[$iFolder]
        If StringStripWS($Folder, 3) = "" Then ContinueLoop ;if line is blank then skip it
        $delString = $Profile & "\" & $Folder & "\*.*"
        MsgBox(0, "", 'FileDelete("' & $delString & '")')
    Next
Next

MsgBox(0, "", "ALL DONE")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...