User71 Posted November 28, 2014 Posted November 28, 2014 Hi, I'm a newbie to both forum and AutoIT, please excuse me if this is a question with an obvious answer. I use the forums a lot so thanks for all the past help you guys have given me. I'm trying to write a simple script to find hidden folders in a location I specify in a variable, and then delete those folders using DirRemove or similar. So far I'm running a cmd with dir to make a List.txt containing paths to hidden folders and trying to read the list.txt line by line and delete the folders. I've had varying success but never 100% reliable so I thought I must be doing it wrong. Here's the current code I'm messing with: (Excuse the includes, can't remember whcih ones I need for this script ) #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <file.au3> Local $Path1 = "C:\List1.txt" Local $Path2 = "C:\List2.txt" Local $FileOpen1 = FileOpen($Path1, 0) Local $FileOpen2 = FileOpen($Path2, 2) While 1 $FileReadLine1 = FileReadLine($FileOpen1) ;MsgBox(0, "", $FileReadLine) ;MsgBox(0, "", $FileOpen2) $FileWriteLine = FileWriteLine($FileOpen2, """" & $FileReadLine1 & """") If $FileReadLine1 = "" Then ExitLoop WEnd While 1 $FileReadLine2 = FileReadLine($Path2) ;MsgBox(0, "", $FileReadLine2) ;$DirRemove = DirRemove($FileReadLine2, 1) ;MsgBox(0, "", $DirRemove) ;Local $RD = Run(@ComSpec & " /c " & "RD /S /Q " & $FileReadLine2, @WindowsDir, @SW_HIDE) If $FileReadLine2 = "" Then ExitLoop WEnd FileClose($FileOpen1) FileClose($FileOpen2) ;$DirRemove = DirRemove("X:\2\1\New Folder", 1) This is my current "lets see what works and what doesn't" kinda mess script. As you can see the first bit takes the paths given by dir and puts "" around them to be sure I can't be suckered that way. That bit works, the deleting bit does not unless I use the bottom line with manually written path. Please let me know where I'm going wrong, at least just on the deleting bit if not my scripting generally , the script was shorter but grew as I experimented trying to make it work! Thanks in advance
Spider001 Posted November 29, 2014 Posted November 29, 2014 (edited) 1 solution there will be others runwait('cmd.exe /c dir *.* /ADH > hiddendirs.txt') https://www.autoitscript.com/autoit3/docs/libfunctions/_FileListToArrayRec.htm Edited November 29, 2014 by Spider001
TheSaint Posted November 29, 2014 Posted November 29, 2014 (edited) I haven't really studied what your code is doing, yet, but I did notice that you are quoting double quotes with double quotes. If you want them to work properly, quote double with singles, and when necessary, single with doubles. $FileWriteLine = FileWriteLine($FileOpen2, '"' & $FileReadLine1 & '"') EDIT Other than that, your code seems ok, though not seeing the lines in your first text file, I'm not sure if the quotes are the only issue. I'm presuming you are using something similar to what Spider001 listed to fill that first text file? Edited November 29, 2014 by TheSaint Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)
Solution User71 Posted November 29, 2014 Author Solution Posted November 29, 2014 (edited) Thank you both for your replies, I will investigate further when I'm not being forced to Xmas shop! Thanks Saint for reminding me of the quotes, I will go through and change them when I can. And thanks Spider, never seen that function it looks interesting, I guess it would mainly change the DirRemove part of my code to look at the array with a nth variable to count the array entries? So DirRemove($Array [$nthEntry], 1) I forgot to add my dir line I'm running (which does work), just because I prepopulated the text files first this time as I was troubleshooting. I run this first normally but as said, I was trying to simplify to find the problem so I just made the C:List1.txt and put in X:21New Folder which becomes "X:21New Folder" in C:List2.txt. Local $DIRCMDPath = "X:\2\1" & " > " Local $HiddenDirPath = @MyDocumentsDir & "\SBackup\Logs\HiddenDir.txt" Local $DIRCMD = "dir /S /B /A:HD " & $DIRCMDPath & $HiddenDirPath Local $DetectHidden = Run(@ComSpec & " /c " & $DIRCMD, @WindowsDir, @SW_HIDE) Thanks EDIT I have successfully used Spider001 suggestion of an array in the following script, but it does give an error as seen below: #include <MsgBoxConstants.au3> #include <file.au3> #include <Array.au3> Array() Func Array() Global $Array = _FileListToArrayRec("X:\2\1", "*", $FLTAR_FOLDERS, 0, 0, $FLTAR_FULLPATH) If @error Then Exit _ArrayDisplay($Array, "Display all first level folders with full path") EndFunc ;==>Array Local $nth = 1 While 1 $FileAttr = FileGetAttrib($Array[$nth]) If StringInStr($FileAttr, "H") Or StringInStr($FileAttr, "S") Then DirRemove($Array[$nth], 1) EndIf $nth = $nth + 1 If $FileAttr = "" Then ExitLoop WEnd "X:\Software\AutoIT\Tutorial Tests\TestSpacev2.au3" (16) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: $FileAttr = FileGetAttrib($Array[$nth]) $FileAttr = FileGetAttrib(^ ERROR The script does work, but it wants to go on to FileGetAttrib from a non existent part of the array which I think is the cause of the error, e.g. trying to access $array[4] when only $array[0] to [3] exists. Any suggestions to solve the error? If so I think it might be case closed EDIT Think I managed to sort it; feel free to suggest a neater way though: expandcollapse popupGlobal $input2_data = Somepath $DirGetSizeArray = DirGetSize($Input2_Data, 3) Local $ArrayFolders = _FileListToArrayRec($Input2_Data, "*", $FLTAR_FOLDERS, 0, 0, $FLTAR_FULLPATH) _ArrayDisplay($ArrayFolders, "Display no hidden or system folders") If IsArray($ArrayFolders) Then Global $ArraySize = UBound($ArrayFolders) - 1 EndIf Local $nth = 1 While 1 If $DirGetSizeArray[2] = 0 Then ExitLoop $FileAttr = FileGetAttrib($ArrayFolders[$nth]) If StringInStr($FileAttr, "H") Or StringInStr($FileAttr, "S") Then DirRemove($ArrayFolders[$nth], 1) EndIf If $nth = $ArraySize Then ExitLoop $nth = $nth + 1 WEnd Local $ArrayFiles = _FileListToArrayRec($Input2_Data, "*", $FLTAR_FILES, 0, 0, $FLTAR_FULLPATH) _ArrayDisplay($ArrayFiles, "Display no hidden or system files") If IsArray($ArrayFiles) Then Global $ArraySize = UBound($ArrayFiles) - 1 EndIf Local $nth = 1 While 1 If $DirGetSizeArray[1] = 0 Then ExitLoop $FileAttr = FileGetAttrib($ArrayFiles[$nth]) If StringInStr($FileAttr, "H") Or StringInStr($FileAttr, "S") Then FileSetAttrib($ArrayFiles[$nth], "-SHRA") FileDelete($ArrayFiles[$nth]) EndIf If $nth = $ArraySize Then ExitLoop $nth = $nth + 1 WEnd Edited December 3, 2014 by User71
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now