Jump to content

Removing directories with partial random chars in them


Recommended Posts

So I have a situation where I have a directory, that has several subfolders in it, like "LpcRT_XXXXXXXX" where x is a random character. One of those folders is referenced in a registry key, and must remain on the system, while I need to delete all the others.

I figure I can locate the necessary folder, move it elsewhere, delete all the other folders, and then move that needed folder back again.

I've found some example scripts that almost get close, but most are using the FilesToArray, and I feel like I need something like FoldersToArray... knowing that doesn't exist, I'm wondering if anyone might have some ideas about how to solve this problem.

- David

Link to comment
Share on other sites

So I have a situation where I have a directory, that has several subfolders in it, like "LpcRT_XXXXXXXX" where x is a random character. One of those folders is referenced in a registry key, and must remain on the system, while I need to delete all the others.

I figure I can locate the necessary folder, move it elsewhere, delete all the other folders, and then move that needed folder back again.

I've found some example scripts that almost get close, but most are using the FilesToArray, and I feel like I need something like FoldersToArray... knowing that doesn't exist, I'm wondering if anyone might have some ideas about how to solve this problem.

- David

_FileListToArray() with the $iFlag option set to "2" returns only folders.

Link to comment
Share on other sites

I can't say I've tried this before, but looking at the help file, FileFindFirstFile() will return directory names if they match the wildcards supplied. I think you could use that along with FileGetAttrib() to determine if what is being returned is a directory and if so do what you need with them.

I usually end up doing things the long way so there very well might be a simpler solution available. If so, someone will likely chime in with it.

[Edit: Or post the more simple solution before I even post mine :) ]

Edited by tehdon
Link to comment
Share on other sites

I can't say I've tried this before, but looking at the help file, FileFindFirstFile() will return directory names if they match the wildcards supplied. I think you could use that along with FileGetAttrib() to determine if what is being returned is a directory and if so do what you need with them.

I usually end up doing things the long way so there very well might be a simpler solution available. If so, someone will likely chime in with it.

[Edit: Or post the more simple solution before I even post mine :) ]

No sweat Tehdon, I appreciate the help either way. It's good to see how the same thing can be accomplished by different methods, as down the road one might be needed over the other.

Anyway thanks.

I'm sure I'll have a question or two, but I'll give it the ole college try first :(

Link to comment
Share on other sites

No sweat Tehdon, I appreciate the help either way. It's good to see how the same thing can be accomplished by different methods, as down the road one might be needed over the other.

Anyway thanks.

I'm sure I'll have a question or two, but I'll give it the ole college try first :(

Once you read the directories into the array, is it possible to search that array for a certain string, and remote only that one?

Link to comment
Share on other sites

_FileListToArray() with the $iFlag option set to "2" returns only folders.

So here is what I have so far, and there are a couple of things I need some help with.

#Include <File.au3>
#Include <Array.au3>
; Locate the root directory by which to begin reading into an array
$path1 = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Data Path")
; Read in directories only that match LpcRT_*, into the array.
$DirList=_FileListToArray($path1 [,$sFilter = "LpcRT_********" [,$iFlag = 2]])
; If no folders found, then something is very wrong, and the script will exit.
If @Error=1 Then
    Exit
EndIf
; Locate the name of the proper folder, that we do not want to delete. (I know that this will pull the entire path, and I need to trim it down to just the final folder name.
$gooddir = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
; Remove the name of the good folder from the array.
_ArrayDelete($DirList, $gooddir)
; Deletes all folders under $path1, except for the folder named $gooddir (work in progress)
DirRemove("C:\Test", 1)

How can I trim the full path that will be returned from my $goodir ?

... guess I only have one question thus far :(

- David

Link to comment
Share on other sites

If you are searching for a specific string, you can loop through the array using a for loop. Then use StringInStr() on each element in the array.

If you are hoping to use wildcards in your search, you would be much better off using StringRegExp() on each element to test for a certain pattern.

Edit: Oops. Posted at the same time. If you want to trim the path off of the directory and just leave the name, you would search for the last backslash in the string using StringInString(). Then you would use StringMid() to return the string from the point of the last backslash to the end of the path.

Edit: Actually, you could just use _PathSplit() to split the path into its elements. That would probably be more versatile for you.

Edited by dantay9
Link to comment
Share on other sites

If you are searching for a specific string, you can loop through the array using a for loop. Then use StringInStr() on each element in the array.

If you are hoping to use wildcards in your search, you would be much better off using StringRegExp() on each element to test for a certain pattern.

Edit: Oops. Posted at the same time. If you want to trim the path off of the directory and just leave the name, you would search for the last backslash in the string using StringInString(). Then you would use StringMid() to return the string from the point of the last backslash to the end of the path.

Edit: Actually, you could just use _PathSplit() to split the path into its elements. That would probably be more versatile for you.

thanks Dantay9, that will help a lot. I'll hit it again once I can get the kids in bed. Hopefully I can finish the script out.

Link to comment
Share on other sites

#Include <File.au3>
#Include <Array.au3>
Global $szDrive, $szDir, $szFName, $szExt


; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
        $DirList = _FileListToArray($DataPath, "lpc*",2)
            _ArrayDelete($DirList, $szFName)
_ArrayDisplay($DirList,"All folders")


; Locate the directory we are going to preserve
$gooddir = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
    $path2 = _PathSplit($gooddir, $szDrive, $szDir, $szFName, $szExt)
    ;_ArrayToString($gooddir)
_ArrayDisplay($path2,"Demo _PathSplit()")

Ok, I have made some modest progress, but am now stuck on how to pull out the data from an array, and set it aside. I'm not sure if I need to write it to clipboard, or assign it to a variable.

With the code above, I have two arrays that contain the data I want. I basically need to delete the directory found as szFName in array $gooddir, from array $DirList.

Anyone able to get me back on track?

Edited by meier3283
Link to comment
Share on other sites

#Include <File.au3>
#Include <Array.au3>
Global $szDrive, $szDir, $szFName, $szExt


; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
        $DirList = _FileListToArray($DataPath, "lpc*",2)
            _ArrayDelete($DirList, $szFName)
_ArrayDisplay($DirList,"All folders")


; Locate the directory we are going to preserve
$gooddir = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
    $path2 = _PathSplit($gooddir, $szDrive, $szDir, $szFName, $szExt)
    ;_ArrayToString($gooddir)
_ArrayDisplay($path2,"Demo _PathSplit()")

Ok, I have made some modest progress, but am now stuck on how to pull out the data from an array, and set it aside. I'm not sure if I need to write it to clipboard, or assign it to a variable.

With the code above, I have two arrays that contain the data I want. I basically need to delete the directory found as szFName in array $gooddir, from array $DirList.

Anyone able to get me back on track?

Typically, you can come up with faster running code by coding all the functions manually. But if you don't care about a few milliseconds of execution time, then the built-in functions with the Array.au3 UDF offer some possibilities and will keep your source concise and tidy. You could likely peg a match for the regkey you want to preserve by doing an _ArraySearch() with the $iPartial flag set. That would return the index within the array of the folder you want to keep, and you could feed that index number into an _ArrayDelete() to remove that entry from the list.

Edit: I'm not sure what the data returned from your RegReads looks like, you may not need to set the partial search flag if the return from the first RegRead exactly matches (a full path in both cases?) the entry in the array...

#Include <Array.au3>
Global $szDrive, $szDir, $szFName, $szExt

; Locate the directory we are going to preserve
$szFName = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
; Get list of directories
$DirList = _FileListToArray($DataPath, "lpc*",2)
; Determine array location of directory to preserve
$SaveDir = _ArraySearch($DirList, $szFName, 0, 0, 0, 1); partial match flag set
; Remove target directory from array
_ArrayDelete($DirList, $SaveDir)


_ArrayDisplay($DirList,"Directories flagged for deletion")
Edited by Spiff59
Link to comment
Share on other sites

#Include <Array.au3>
#Include <File.au3>
Global $szDrive, $szDir, $szFName, $szExt

; Locate the directory we are going to preserve
$szFName = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
    MsgBox(0,"FolderName", $szFName, 2)
; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
; Get list of directories
$DirList = _FileListToArray($DataPath, "lpc*",2)
_ArrayDisplay($DirList,"All folders")
; Determine array location of directory to preserve
$SaveDir = _ArraySearch($DirList, $szFName, 0, 0, 0, 1); partial match flag set
_ArrayDisplay($SaveDir,"Folder to save")
; Remove target directory from array
_ArrayDelete($DirList, $SaveDir)


_ArrayDisplay($DirList,"Directories flagged for deletion")
The code you pasted is great, and currently the regread to "$szFName = "MA_LPC_RUNTIME")", pulls the entire path. I'm not clear if I need the entire path, or don't need the path. I'm having problems sorting out the logic I need, to effectively preserve one folder, and delete all the others that begin with LPC.

The problem with the code right now, is that the array contains all of the folders, including the one I'd like to preserve. (I'm assuming i'll use the resulting array, to form up some code to delete the folders contained in the array)

Here is my current diagnostic code, and I can't get the _ArraySearch line, to display any data... could that be hanging us up?

Link to comment
Share on other sites

#Include <Array.au3>
#Include <File.au3>
Global $szDrive, $szDir, $szFName, $szExt

; Locate the directory we are going to preserve
$szFName = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
    MsgBox(0,"FolderName", $szFName, 2)
; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
; Get list of directories
$DirList = _FileListToArray($DataPath, "lpc*",2)
_ArrayDisplay($DirList,"All folders")
; Determine array location of directory to preserve
$SaveDir = _ArraySearch($DirList, $szFName, 0, 0, 0, 1); partial match flag set
_ArrayDisplay($SaveDir,"Folder to save")
; Remove target directory from array
_ArrayDelete($DirList, $SaveDir)


_ArrayDisplay($DirList,"Directories flagged for deletion")
The code you pasted is great, and currently the regread to "$szFName = "MA_LPC_RUNTIME")", pulls the entire path. I'm not clear if I need the entire path, or don't need the path. I'm having problems sorting out the logic I need, to effectively preserve one folder, and delete all the others that begin with LPC.

The problem with the code right now, is that the array contains all of the folders, including the one I'd like to preserve. (I'm assuming i'll use the resulting array, to form up some code to delete the folders contained in the array)

Here is my current diagnostic code, and I can't get the _ArraySearch line, to display any data... could that be hanging us up?

_ArraySearch doesn't return an array, it simply returns a number matching the index of the entry in the search array that matched your target string. So, for debugging purposes, try "Msgbox(1,"","Index of Folder to save: " & $SaveDir)". It will return a -1 for a "Not Found" condition, in which case you'd need to manually investigate why it doesn't think the $szFName string exists in your $DirList array. Edited by Spiff59
Link to comment
Share on other sites

#Include <Array.au3>
#Include <File.au3>
Global $szDrive, $szDir, $szFName, $szExt

; Locate the directory we are going to preserve
$szFolder = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
$path2 = _PathSplit($szFolder, $szDrive, $szDir, $szFName, $szExt)
; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
; Get list of directories
$DirList = _FileListToArray($DataPath, "lpc*")
_ArrayDisplay($DirList,"All folders")
; Determine array location of directory to preserve
$SaveDir = _ArraySearch($DirList, $szFName, 0, 0, 0, 1); partial match flag set
_ArrayDisplay($SaveDir,"Folder to save")
; Remove target directory from array
_ArrayDelete($DirList, $SaveDir)


_ArrayDisplay($DirList,"Directories flagged for deletion")

Msgbox(0,"","Index of Folder to save: " & $SaveDir)

Ok, I added my PathSplit command back in, and now your code is creating an array that shows only the folders I want to delete. So that's perfect. Also, the msgbox does show the proper index number of the "good" folder.

Now I'm on to figure out how to delete these folders :( (man, I really appreciate your help!)

Edit: Do I need to worry about the Index=0 that shows up in these arrays? I'm guessing they are "zero" data, and will be ignored. I just don't want to go deleting the root folder or anything :)

Edited by meier3283
Link to comment
Share on other sites

_ArrayListToFile (were you to check the helpfile entry on it) places the count of items returned into the 0 element. Many AutoIt functions do the same and people refer to them as 1-based arrays, as the first actual piece of data is in element 1. There is also an equal mix of 0-based array usage around here, where there is no "count" in element 0, and instead it contains the first piece opf data. The mix is necessary to keep us all confused. Anyway, your "0 element" is of great value, as you already know the extent of the loop you'll need to execute to do your deletions, ala:

#Include <Array.au3>
#Include <File.au3>
Global $szDrive, $szDir, $szFName, $szExt

; Locate the directory we are going to preserve
$szFolder = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
$path2 = _PathSplit($szFolder, $szDrive, $szDir, $szFName, $szExt)
; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")
; Get list of directories
$DirList = _FileListToArray($DataPath, "lpc*")
_ArrayDisplay($DirList,"All folders") ; temp
; Determine array location of directory to preserve
$SaveDir = _ArraySearch($DirList, $szFName, 0, 0, 0, 1); partial match flag set
Msgbox(0,"","Index of Folder to save: " & $SaveDir) ; temp
; Remove target directory from array
_ArrayDelete($DirList, $SaveDir)

_ArrayDisplay($DirList,"Directories flagged for deletion") ; temp

$DataPath &= "\" ; only needed if $datapath doesnt already end in a backslash
For $i = 1 to $DirList[0] - 1 ; reduce it by one to compensate for the entry we previously removed 
    DirRemove($DataPath & $DirList[$i], 1) ; 1 flag to also delete any subdirectiories
Next
Link to comment
Share on other sites

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Setup_McAfee.ico
#AutoIt3Wrapper_outfile=DEL_LPC_MA45.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#Include <Array.au3>
#Include <File.au3>
Global $szDrive, $szDir, $szFName, $szExt

; Validate MA version 4.5.0.1440 or lower
$SZProductVer=RegRead("HKLM\SOFTWARE\Network Associates\ePolicy Orchestrator\Application Plugins\EPOAGENT3000","Version")
If $SZProductVer>"4.5.0.1440" Then Exit

; Locate the directory we are going to preserve
$szFolder = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "MA_LPC_RUNTIME")
$path2 = _PathSplit($szFolder, $szDrive, $szDir, $szFName, $szExt)

; Locate the root directory to start enumerating from.
$DataPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\TVD\Shared Components\Framework", "Installed Path")

; Get list of directories
$DirList = _FileListToArray($DataPath, "lpc*")

;_ArrayDisplay($DirList,"All folders") ; debug
; Determine array location of directory to preserve
$SaveDir = _ArraySearch($DirList, $szFName, 0, 0, 0, 1); partial match flag set

;Msgbox(0,"","Index of Folder to save: " & $SaveDir) ; debug
; Remove target directory from array
_ArrayDelete($DirList, $SaveDir)

; _ArrayDisplay($DirList,"Directories flagged for deletion") ; debug
$DataPath &= "\" ; only needed if $datapath doesnt already end in a backslash
For $i = 1 to $DirList[0] - 1 ; reduce it by one to compensate for the entry we previously removed
    DirRemove($DataPath & $DirList[$i], 1) ; 1 flag to also delete any subdirectiories
Next

Here's my final code, it does exactly what I needed, and I couldn't have done it without your help. (no matter how many more hours I spend in the help file) I really appreciate it.

- David

Link to comment
Share on other sites

Looks good! AM glad to be of help. It does appear we lost the ",2" parameter off the end of the _FileListToArray() call. Were there a file (not folder) that began with "lpc" (there apparently are not any presently) it would make it into the $DirList array and the DirRemove() against a filename would likely error. Probably ought to stick the 2 back in there. Have a nice weekend!

Edit: I think you could also probably trim down the _ArraySearch statement, I guess your search and target strings are both just the folder name itself (rather than one being a full path), so partial matching is probably not necessary. You could try stripping the ",0,0,0,1" off the end of the statement. Might run a few milliseconds faster.

Edited by Spiff59
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...