Jump to content

Recommended Posts

Posted (edited)

Good Day,

 

I am trying to learn how to make an array with a list of files and folders then create another array that would scan the users profile and if certain files are there delete them. I have managed to create a base array that I think I can put the files in and I managed to scan the user profile and filter out a lot of stuff to create a somewhat parred down array of over all files but I can not sort how I would delete them. 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Local $aArray_Base[2] = ["FRST.txt", "Addition.txt"] 
_ArrayDisplay($aArray_Base, "To Be Removed Array") ;This array contains the files and folders I want to see if exist in the user profile and then remove them.

Local $aArray = _FileListToArrayRec(@UserProfileDir, "*.exe||onedrive;appdata;music;pictures;videos;.vscode" , $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT)
_ArrayDisplay($aArray, "Sorted Tree") ;This array contains all of the files and folders not explicitly EXCLUDED from a scan of the Users Profile.



$sFilePath = "C:\FRST\"
If FileExists($sFilePath) Then
    DirRemove($sFilePath, 1)
EndIf ;To remove the FRST tools from the computers hard drive if they are found.

I looked at _ArrayDelete but I am confused about how I would compare the, for lack of a better term, cell value in the base array and then if it is found in the other array delete just that file or folder from the computer. Thank you in advance for your efforts to help me understand and your patience.

Edited by Nunos
Too vague...
Posted

Do a search on the forum, this has been asked numerous times.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Local $aArray_Base[3] = ["FRST.txt", "Addition.txt","Aurora.exe"]
;_ArrayDisplay($aArray_Base, "To Be Removed Array") ;This array contains the files and folders I want to see if exist in the user profile and then remove them.

Local $aArray = _FileListToArrayRec(@UserProfileDir, "*.exe||onedrive;appdata;music;pictures;videos;.vscode" , $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT)
;_ArrayDisplay($aArray, "Sorted Tree") ;This array contains all of the files and folders not explicitly EXCLUDED from a scan of the Users Profile.


;_ArrayDisplay($aArray)

For $i = 0 To UBound($aArray_Base)-1

    $found = _ArraySearch($aArray, $aArray_Base[$i])

    If $found <> -1 Then
        _ArrayDelete($aArray, $found)
    Else
        ConsoleWrite($aArray_Base[$i] & " not found" & @CRLF)
    EndIf

Next

_ArrayDisplay($aArray)

Exit


#cs
$sFilePath = "C:\FRST\"
If FileExists($sFilePath) Then
    DirRemove($sFilePath, 1)
EndIf ;To remove the FRST tools from the computers hard drive if they are found.
#CE

Doesn't find the files in $aArray_Base that do exist in my downloads folder instead I get all other files.

Posted

what is the endgame?  look at this thread that has many examples to show matches amongst arrays.  I'd start with @mikell's scripting dictionary example as it is the easiest to digest:  

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted
28 minutes ago, iamtheky said:

what is the endgame?  look at this thread that has many examples to show matches amongst arrays.  I'd start with @mikell's scripting dictionary example as it is the easiest to digest:  

 

The endgame is to make a tool for the volunteers at Bleepingcomputer.com to cleanup the left over tools on systems they help cleanup. Previously we used DelFix but it is no longer being updated so I am trying to help make a replacement. I was given a list of the tools and I have begun getting a list of all the places they install too or could be downloaded to, usually the users profile to places like the desktop or downloads. My hope was I could use If FileExists to cleanup the ones I know for sure where they will be and the others I could put into an array and then make the other array scan the Userprofile and put them into another array then compare the two and get a list of the files that do exist and then remove them. Admittedly this is way out of my ability because I am not a developer or even smart enough to do but I am hoping to make something that others can use to speed up what they do so they can help others. 

 

I will try to make sense of what you provided in a couple of days but I am off to the store for work till morning. Thank you for your help. :D

Posted (edited)

I tried to merge my first attempt with what you suggested and my code is in the box below. 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

Local $a[3] = ["FRST.txt", "Addition.txt","Aurora.exe"] 
Local $b = _FileListToArrayRec(@UserProfileDir, "*.exe||onedrive;appdata;music;pictures;videos;.vscode" , $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT)

Local $sda = ObjCreate("Scripting.Dictionary")
Local $sdb = ObjCreate("Scripting.Dictionary")
Local $sdc = ObjCreate("Scripting.Dictionary")

For $i In $a
    $sda.Item($i)
Next
For $i In $b
    $sdb.Item($i)
Next

For $i In $a
    If $sdb.Exists($i) Then $sdc.Item($i)
Next
$asd3 = $sdc.Keys()

For $i In $asd3
    If $sda.Exists($i) Then $sda.Remove($i)
    If $sdb.Exists($i) Then $sdb.Remove($i)
Next
$asd1 = $sda.Keys()
$asd2 = $sdb.Keys()

_ArrayDisplay($asd1, "$asd1")
_ArrayDisplay($asd2, "$asd2")
_ArrayDisplay($asd3, "$asd3")

I get three arrays displayed the first seems to have just the stuff in the $a[3] Variable. The Next appears to be the $b variables scan, but the final is empty so can someone advise me what I need to change to get just the variables that do exist in both $a and $b I think. Perhaps if someone could comment the code of Mikels I could figure out what I am needing to change? Thank you and sorry for not understanding it. 

Edited by Nunos
Posted (edited)

May I suggest something a bit simpler  :D

#include <Array.au3>
#include <File.au3>

Local $a[3] = ["FRST.txt", "Addition.txt","Aurora.exe"] 

Local $str
For $i = 0 To UBound($a)-1
   $str &= $a[$i] & ";"
Next
$str = StringTrimRight($str, 1)
; Msgbox(0,"", $str)

Local $b = _FileListToArrayRec(@UserProfileDir, $str, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
_ArrayDisplay($b)

; and then walk through $b with some FileDelete  :)


Edit
I also strongly suggest to NOT search for files and folders in one shot using $FLTAR_FILESFOLDERS because this can give unexpected and quite strange results. Much better to run it a 2nd time for folders only, using $FLTAR_FOLDERS
And DON'T forget a confirmation window to display concerned files/folders before running deletion !

Edited by mikell
Posted (edited)

Thank you mikell that seems to be working. Code is below. 

#include <Array.au3>
#include <File.au3>

Local $a[3] = ["FRST.txt", "Addition.txt","Aurora.exe"] ;These are the files to search for

Local $str
For $i = 0 To UBound($a)-1
   $str &= $a[$i] & ";"
Next
$str = StringTrimRight($str, 1) ;All of this somehow makes the filemask we end up searching for below
; Msgbox(0,"", $str)

Local $b = _FileListToArrayRec(@UserProfileDir, $str, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH) ;This is the array created from searching the user profile 
_ArrayDisplay($b)

$len = $b[0]

For $i = 1 to $len
    If FileDelete($b[$i]) = 1 Then
        ConsoleWrite("Ok")
    Else
        ConsoleWrite("No")
    EndIf
Next

When there are no files in existence I get in error in the console.

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
"C:\Users\User\Documents\Scirpting\AutoIT\In Progress and Researching\Tool Cleaner\Clean Tool.au3" (16) : ==> Subscript used on non-accessible variable.:
$len = $b[0]
$len = $b^ ERROR
->17:06:16 AutoIt3.exe ended.rc:1
+>17:06:16 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 3.83

No idea what this is or what I need to add or remove to fix it. I tried to do an If statement on IsArray but I couldn't get it to work because it kept saying Else couldn't be just a statement??? I was only trying to make it a MsgBox to say something like no files found.

 

Update: I just received a partial list of files and paths and it is rather large. Is there a better way to set the file and folder names for the $a variable? If I put them all in an ini file and include it in the script is there a way to assign them to the $a variable? The help file makes it seem like the data returned from an iniread is a string but I am not sure how to read them into $a.

Edited by Nunos
Updated information
Posted

@Nunos

Hi :)

About your error, AutoIt is telling you that $b is not an array, since there are no files/folders in your "search path".

If you put an @error checking just before the _ArrayDisplay($b), you will probably see that @error code returned is 9.

To check this, add these lines before the _ArrayDisplay($b):

If @error Then
    ConsoleWrite("Error while getting the list of files/folders. Error: " & @error)
Else
    _ArrayDisplay($b)
    ; Go ahead with your code...
EndIf

And, about the Ini* functions, maybe you can take a look at IniReadSection(), which, in case of success, returns a two dimensional array, containing key and values from a given section name :)

From there, you could use directly this array to do your check.

Cheers :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted
8 hours ago, Nunos said:

I just received a partial list of files and paths and it is rather large

Files and paths are different things. Can you provide a sample ini so we can see this ?

Posted (edited)

I attached one of each. The paths for some things will always be absolute for example after FRST is run it makes a folder C:\FRST\ and my thought was to just do a FileDel() on those if they Exist. Others are not absolute because guests download them to their downloads folder, or desktop so I was thinking I would scan the @UserDataDir for just the file names say FRST64.exe wherever it shows up in the Users Directory. Please advise if my thinking is flawed.

FileList.ini

files.txt

Edited by Nunos
Added Files
Posted (edited)

Why don't you simply scan "C:" recursively ? files in @UserProfileDir will automatically be included in the search

Edit
You can extract the file names from your txt file using this

#Include <Array.au3>
$res = StringRegExp(FileRead("files.txt"), '(?m)([^\\\v]+)$', 3)
_ArrayDisplay($res)

 

Edited by mikell
Posted

I guess I thought that scanning C: on some computers might be really slow?

I will make a txt file with the names and see if I can get this new part added to the script. Thanks. 

Posted
15 hours ago, FrancescoDiMuro said:

@Nunos

Hi :)

About your error, AutoIt is telling you that $b is not an array, since there are no files/folders in your "search path".

If you put an @error checking just before the _ArrayDisplay($b), you will probably see that @error code returned is 9.

To check this, add these lines before the _ArrayDisplay($b):

If @error Then
    ConsoleWrite("Error while getting the list of files/folders. Error: " & @error)
Else
    _ArrayDisplay($b)
    ; Go ahead with your code...
EndIf

And, about the Ini* functions, maybe you can take a look at IniReadSection(), which, in case of success, returns a two dimensional array, containing key and values from a given section name :)

From there, you could use directly this array to do your check.

Cheers :)

Added your Error catching statement results from the console are:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
Error while getting the list of files/folders. Error: 1"C:\Users\User\Documents\Scirpting\AutoIT\In Progress and Researching\Tool Cleaner\Clean Tool.au3" (26) : ==> Subscript used on non-accessible variable.:
$len = $b[0]
$len = $b^ ERROR
->14:14:58 AutoIt3.exe ended.rc:1
+>14:14:58 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 3.18

 

 

Posted
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <RemovalExtras.au3>


$res = StringRegExp(FileRead("files.txt"), '(?m)([^\\\v]+)$', 3)

;_ArrayDisplay($res)
Local $str
For $i = 0 To UBound($res)-1
   $str &= $res[$i] & ";"
Next
$str = StringTrimRight($str, 1) ;All of this somehow makes the filemask we end up searching for below
; Msgbox(0,"", $str)

Local $b = _FileListToArrayRec(@UserProfileDir, $str, $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH) ;This is the array created from searching the user profile
If @error Then
    ConsoleWrite("Error while getting the list of files/folders. Error: " & @error)
Else
    _ArrayDisplay($b)
    ; Go ahead with your code...
EndIf
_ArrayDisplay($b)

$len = $b[0]

For $i = 1 to $len
    If FileDelete($b[$i]) = 1 Then
        ConsoleWrite("Ok")
    Else
        ConsoleWrite("No")
    EndIf
Next

 

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
×
×
  • Create New...