Systems By Posted June 23, 2006 Share Posted June 23, 2006 Hi everyone. I am trying to use the files names retrieved using the _FileReadTo Array function as my basis of deleting a certain set of files in a folder. Here is my code: $root = ("C:\Temp\") $filename = ("tempdir3.txt") $home = ("C:\Homebackups\") Global $aRecords Run("cmd.exe") WinWaitActive("C:\WINNT\system32\cmd.exe") Send("cd c:\homebackups") Send("{enter}") Send("dir /a /o-d /b > c:\temp\tempdir3.txt {enter}") sleep(1000) Send("exit {enter}") If Not _FileReadToArray($root & $filename,$aRecords) Then MsgBox(4096,"Error", "Error reading Office PC backup log" & @error) Sleep(1000) EndIf Now, what I am trying to do is delete the files that are read by the function above by using the following code: For $x = $aRecords[0] -1 to 6 Step -1 FileDelete($home & $aRecords[$x]) SplashTextOn("Autobackup", "deleting "& $home & $aRecords[$x], 200, 150) sleep(200) SplashOff() Next I have replaced the FileDelete($home & $aRecords[$x]) line with MsgBox(4096, "Test", "Displaying from the bottom up " & $aRecords[$x]) and although I am able to display the file name, I can't get the FileDelete function to delete the file by combining the path variable and the file name variable. Am I just using this function the wrong way? The whole point of this program is to take a directory ordered by date and delete excess files so the number of files in the backup file directory never add up to more than 5. So, by reading the text file that the command window creates, I thought about reading the file backwards if the file has more than 5 lines, and using the combination of the path variable and file name variable as my method of telling FileDelete which files to delete. Hope I'm making sense. Any help would be greatly appreciated. Link to comment Share on other sites More sharing options...
dirtymafia Posted June 23, 2006 Share Posted June 23, 2006 (edited) I am not an expert, but if I were doing this I would write the following: for $x = $aRecords[0] to 6 Step -1 FileDelete($home & aRecords[$x]) SplashTextOn("Autobackup","Deleting " & $home & aRecords[$x],200,150) sleep(200) SplashOff() Next This would start at the last file represented by the count - aRecords[0] and end at $aRecords[6] leaving aRecords[1] through aRecords[5]. Hopefully I'm understanding you correclty Edited June 23, 2006 by dirtymafia Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 I am not an expert, but if I were doing this I would write the following: for $x = $aRecords[0] to 6 Step -1 FileDelete($home & aRecords[$x]) SplashTextOn("Autobackup","Deleting " & $home & aRecords[$x],200,150) sleep(200) SplashOff() Next This would start at the last file represented by the count - aRecords[0] and end at $aRecords[6] leaving aRecords[1] through aRecords[5]. Hopefully I'm understanding you correcltyYou are understanding me correctly and although the code does work, for some reason, all of the files in the directory are deleted instead of just the files that are read by the code above. The aRecords[0] file is returning the path with a blank aRecords file and I think this may be the reason that all of the files are being deleted. Does anyone else have any ideas why my entire directory is being deleted? Link to comment Share on other sites More sharing options...
Skruge Posted June 23, 2006 Share Posted June 23, 2006 Welcome to the forums. $aRecords[0] points to the last line in the file, which is blank. If you look at the file, it doesn't have the path... you're adding it in with $home. You're actually calling FileDelete("C:\Homebackups\") which will erase all files in that directory. You were on the right track before, when you were skipping the blank line by starting from the second-to-last record. I prefer to add a quick check for blank lines instead, in case you're working with a human-generated list, or the command interpreter reacts differently. btw- no need to start a command prompt and interact with it when you can do what you need in one line: $root = ("C:\Temp\") $filename = ("tempdir3.txt") $home = ("C:\Homebackups\") Global $aRecords RunWait("cmd.exe /c dir /a /o-d /b c:\homebackups > " & $root & $filename, "", @SW_HIDE) If Not _FileReadToArray($root & $filename, $aRecords) Then MsgBox(4096, "Error", "Error reading Office PC backup log" & @error) Sleep(1000) EndIf For $x = $aRecords[0] To 6 Step - 1 If Not $aRecords[$x] = "" Then FileDelete($home & $aRecords[$x]) SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) SplashOff() EndIf Next FileDelete($root & $filename) p.s. - Look into the use Standard IO redirection to eliminate the use of a temporary text file. Also, FileFindFirstFile/FileFindNextFile to avoid this problem altogether. [font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font] Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 Welcome to the forums. $aRecords[0] points to the last line in the file, which is blank. If you look at the file, it doesn't have the path... you're adding it in with $home. You're actually calling FileDelete("C:\Homebackups\") which will erase all files in that directory. You were on the right track before, when you were skipping the blank line by starting from the second-to-last record. I prefer to add a quick check for blank lines instead, in case you're working with a human-generated list, or the command interpreter reacts differently. btw- no need to start a command prompt and interact with it when you can do what you need in one line: $root = ("C:\Temp\") $filename = ("tempdir3.txt") $home = ("C:\Homebackups\") Global $aRecords RunWait("cmd.exe /c dir /a /o-d /b c:\homebackups > " & $root & $filename, "", @SW_HIDE) If Not _FileReadToArray($root & $filename, $aRecords) Then MsgBox(4096, "Error", "Error reading Office PC backup log" & @error) Sleep(1000) EndIf For $x = $aRecords[0] To 6 Step - 1 If Not $aRecords[$x] = "" Then FileDelete($home & $aRecords[$x]) SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) SplashOff() EndIf Next FileDelete($root & $filename) p.s. - Look into the use Standard IO redirection to eliminate the use of a temporary text file. Also, FileFindFirstFile/FileFindNextFile to avoid this problem altogether. I tried to run the code above, but it seems to stop and not do anything after running the command window. No files are deleted, but the text file is created. Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 Update: Ok, I got the program to delete a file (just one file per execution of the code). Can anyone tell me why this code: For $x = $aRecords[0] To 6 Step - 1 If Not $aRecords[$x] = "" Then FileDelete($home & $aRecords[$x]) SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) SplashOff() EndIf Next is only running the FileDelete line once and being run for each instance of the loop? The program will only delete one file at a time, but will run the splash screen for every file that should be deleted. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted June 23, 2006 Moderators Share Posted June 23, 2006 Are you trying to delete directories as well? Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 Are you trying to delete directories as well?No, just individual files. I can delete one at a time, but not one after the other. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted June 23, 2006 Moderators Share Posted June 23, 2006 Try debugging it:For $x = $aRecords[0] To 6 Step - 1 If $aRecords[$x] <> "" Then Do FileDelete($home & $aRecords[$x]) ToolTip('On this file:' & @CR & $home & $aRecords[$x], 0, 0) Until Not FileExists($home & $aRecords[$x]) ;SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) ;SplashOff() EndIf Next Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 Try debugging it:For $x = $aRecords[0] To 6 Step - 1 If $aRecords[$x] <> "" Then Do FileDelete($home & $aRecords[$x]) ToolTip('On this file:' & @CR & $home & $aRecords[$x], 0, 0) Until Not FileExists($home & $aRecords[$x]) ;SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) ;SplashOff() EndIf NextThe code above works, but still only deletes one file per run of the script. The tooltip window does appear with the files that are supposed to be deleted, but it is only the first file that gets deleted. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted June 23, 2006 Moderators Share Posted June 23, 2006 (edited) The code above works, but still only deletes one file per run of the script. The tooltip window does appear with the files that are supposed to be deleted, but it is only the first file that gets deleted.If you ran the script like I have it, it wouldn't even go to the next loop if the file still existed, did you re-fresh the folder they are in when you were checking them? Edit: Could try this too if so:For $x = $aRecords[0] To 6 Step - 1 If Not FileExists($home & $aRecords[$x]) Then MsgBox(64, 'Info:', 'File: ' & $home & $aRecords[$x] & ' Does not exist') If $aRecords[$x] <> "" Then Do FileDelete($home & $aRecords[$x]) ToolTip('On this file:' & @CR & $home & $aRecords[$x], 0, 0) Until Not FileExists($home & $aRecords[$x]) ;SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) ;SplashOff() EndIf Next Edited June 23, 2006 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 (edited) If you ran the script like I have it, it wouldn't even go to the next loop if the file still existed, did you re-fresh the folder they are in when you were checking them?Yes I did. When I go check the folder where the files reside, the first file to be deleted is always gone, but the rest remain. The loop does go through all of the files that are to be deleted, just deletes the first one. Anything else that I am not seeing???Edit:Let me try that other code you just posted. Edited June 23, 2006 by Systems By Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 Just tried the code above. Still the same results. The first file read is deleted, but it seems like the loop just does the ToolTip screen for the remaining files without deleting them. This is starting to get very annoying. Thanks for all you help. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted June 23, 2006 Moderators Share Posted June 23, 2006 I tested this with beta .126 (I don't have the release) and it worked well:Local $FileLocation = @DesktopDir & '\DeleteFilesTestFolder\' Local $FindFiles = FileFindFirstFile($FileLocation & '*.*') Local $CreateArray = '' While 1 $NextFile = FileFindNextFile($FindFiles) If @error = 1 Then ExitLoop If Not StringInStr(FileGetAttrib($FileLocation & $NextFile), 'D') Then $CreateArray &= $FileLocation & $NextFile & Chr(01) EndIf WEnd $CreateArray = StringSplit(StringTrimRight($CreateArray, 1), Chr(01)) For $iCount = UBound($CreateArray) - 1 To 6 Step - 1 Do FileDelete($CreateArray[$iCount]) Sleep(10) Until Not FileExists($CreateArray[$iCount]) Sleep(100) NextI made sure it didn't list the folders inside of it. I had 2 folders inside the test folder / 8 .exe's / 8 .txt / 8 .ini / 8 .jpgs ... ended up with 2 folders and 5 others as I should have. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
PsaltyDS Posted June 23, 2006 Share Posted June 23, 2006 (edited) If you ran the script like I have it, it wouldn't even go to the next loop if the file still existed, did you re-fresh the folder they are in when you were checking them? Edit: Could try this too if so:For $x = $aRecords[0] To 6 Step - 1 If Not FileExists($home & $aRecords[$x]) Then MsgBox(64, 'Info:', 'File: ' & $home & $aRecords[$x] & ' Does not exist') If $aRecords[$x] <> "" Then Do FileDelete($home & $aRecords[$x]) ToolTip('On this file:' & @CR & $home & $aRecords[$x], 0, 0) Until Not FileExists($home & $aRecords[$x]) ;SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) ;SplashOff() EndIf Next That is testing for file existence before testing for a blank line (which would result in just $home). How about the following (includes exhaustive logging so it can be checked for results): #Include<file.au3> $LogFile = @ScriptDir & "\DebugLog.log" _FileWriteLog($LogFile, "Entering for/next loop, $aRecords[0] = " & $aRecords[0]) For $x = $aRecords[0] To 6 Step - 1 If $aRecords[$x] <> "" Then If FileExists($home & $aRecords[$x]) Then If FileDelete($home & $aRecords[$x]) Then _FileWriteLog($LogFile, "$x = " & $x & ", Successfully deleted file: " & $home & $aRecords[$x]) Else _FileWriteLog($LogFile, "$x = " & $x & ", Error deleting file: " & $home & $aRecords[$x]) EndIf Else _FileWriteLog($LogFile, "$x = " & $x & ", File $home & $aRecords[$x] does not exitst!") EndIf Else _FileWriteLog($LogFile, "$x = " & $x & ", File line was blank.") EndIf Next Edit: Forgot a closing paren... Edited June 23, 2006 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Systems By Posted June 23, 2006 Author Share Posted June 23, 2006 Got the code to work. Apparently, something about the white spaces was causing the program to not delete any files after the first file. I just added: $aRecords[$x] = StringStripWS($aRecords[$x],3) after the FOR statement and everything works now. Thanks for everyone's help. Link to comment Share on other sites More sharing options...
Systems By Posted June 26, 2006 Author Share Posted June 26, 2006 By using the following code: For $x = $aRecords[0] To 6 Step - 1 $aRecords[$x] = StringStripWS($aRecords[$x],3) If Not FileExists($officepcbudir &$aRecords) Then MsgBox(64, 'Info', 'File ' & $officepcbudir & $aRecords[$x] & ' Does not exist') If $aRecords[$x] <> "" Then Do FileDelete($officepcbudir & $aRecords[$x]) ToolTip('On this file:' & @CR & $officepcbudir & $aRecords[$x], 0, 0) Sleep(500) Until Not FileExists($officepcbudir & $aRecords[$x]) ;SplashTextOn("Autobackup", "Deleting " & $home & $aRecords[$x], 200, 150) Sleep(200) ;SplashOff() EndIf Next is there any way I can modify this code so as it reads the file backwards, it only counts the files that end in *.zip? Link to comment Share on other sites More sharing options...
MHz Posted June 26, 2006 Share Posted June 26, 2006 To my knowledge, a variable and a array of the same name cannot co-exist without one destroying the other. ( $aRecords ) Link to comment Share on other sites More sharing options...
Valuater Posted June 26, 2006 Share Posted June 26, 2006 In my *XPClean One-Click* located herehttp://www.autoitscript.com/forum/index.php?showtopic=27603#i use this function to cleanexpandcollapse popup; Cleaning Functions Func XPClean() $Temp_Dir = StringSplit('@HomeDrive & "\Temp\",@WindowsDir & "\Temp\",@UserProfileDir & "\Local Settings\Temp\",@UserProfileDir & "\Recent\",@UserProfileDir & "\Local Settings\Temporary Internet Files\Content.IE5\"', ",") For $t = 1 To $Temp_Dir[0] $fHandle = FileFindFirstFile($Temp_Dir[$t] & "*.*") If $fHandle >= 0 Then While 1 $result = FileFindNextFile($fHandle) If @error Then ExitLoop If $result <> "." And $result <> ".." Then If StringInStr(FileGetAttrib($Temp_Dir[$t] & $result), "D") Then DirRemove($Temp_Dir[$t] & $result, 1) Else FileDelete($Temp_Dir[$t] & $result) EndIf EndIf WEnd FileClose($fHandle) EndIf Next $Del_File = StringSplit("\*.bak,\*.old,\*.tmp,\*.dmp,\*.gid", ",") For $t = 1 To $Del_File[0] RunWait(@ComSpec & ' /c ' & 'dir "' & @HomeDrive & $Del_File[$t] & '" /a :h /b /s' & ' > "' & @TempDir & '\dir.txt"', '', @SW_HIDE) Sleep(2000) $hFile = FileOpen(@TempDir & "\dir.txt", 0) If $hFile = -1 Then MsgBox(0, "Error", "Unable to open file.") Return EndIf While 1 $sLine = FileReadLine($hFile) If @error = -1 Then ExitLoop If $sLine <> "" Then FileDelete($sLine) EndIf WEnd FileClose($hFile) Next EndFunc ;==>XPClean8) Link to comment Share on other sites More sharing options...
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