Jump to content

[SOLVED] Delete files older then x


Recommended Posts

Where I have the Sleep(500) in my demo you can put the FileDelete command. The only thing that would mess this up is if the FileDelete command returns before the file is actually deleted, I haven't tested that. It would cause the loop to run through before all the files have been deleted if it doesn't wait for them to be finished, this would be a concern for large files because it does take some time for them to actually get deleted occasionally.

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

Link to comment
Share on other sites

  • Replies 48
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I have make it, but the progress bar appear and disappers in one sec, and the file will not deleted :)

#Include <File.au3>

$From_dir = "C:Test"
$filter = "*.*"

Local $count = 0, $dir = ""
$FileList = _FileListToArray($From_dir, $filter, 0)
If(Not IsArray($FileList)) And(@error = 1) Then SetError(-1)

Global $FileList
Global $ProgressArray = _ProgressGUI("Files to delete = " & $FileList, 1)
GUICtrlSetData($ProgressArray[1], 100) ;Set the progress bar to full

For $I = 1 To $FileList
FileDelete($FileList[$I])
GUICtrlSetData($ProgressArray[1], 100 - Int(($I / $FileList[0]) * 100)) ; Reset the position of the progress bar as you delete files, using a percentage of files to delete
GUICtrlSetData($ProgressArray[2], "Files to Delete = " & $FileList[0] - $I) ; updates the label of the GUI with the number of remaining files to delete
Next

GUIDelete($ProgressArray[0]) ; deletes the ProgressBar GUI
Edited by johnmcloud
Link to comment
Share on other sites

Try this script with the ProgressGUI function, you were not using the $FileList array correctly, and you weren't formatting the FileDelete command correctly. See the comments in the code below to see what's been changed.

#Include <File.au3>
$From_dir = "C:Prova"
$filter = "*.*"

Local $count = 0, $dir = ""
$FileList = _FileListToArray($From_dir, $filter, 0)
If(Not IsArray($FileList)) And(@error = 1) Then SetError(-1)
;~ Global $FileList <<<<<<<<< you redeclared the $FileList array as a variable
Global $ProgressArray = _ProgressGUI("Files to delete = " & $FileList[0], 1)
GUICtrlSetData($ProgressArray[1], 100) ;Set the progress bar to full

For $I = 1 To $FileList[0] ; <<<<<<<<< $FileList is an array, element [0] holds the number of files
 FileDelete($From_dir & "" & $FileList[$I]) ; you need to add the folder name where the files to be deleted are.
 GUICtrlSetData($ProgressArray[1], 100 - Int(($I / $FileList[0]) * 100)) ; Reset the position of the progress bar as you delete files, using a percentage of files to delete
 GUICtrlSetData($ProgressArray[2], "Files to Delete = " & $FileList[0] - $I) ; updates the label of the GUI with the number of remaining files to delete
Next

GUIDelete($ProgressArray[0]) ; deletes the ProgressBar GUI

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

Link to comment
Share on other sites

This error:

FileDelete($From_dir & "" & $FileList[$I])

Because i'm using the _RecFileListToArray with full path on main code, I forgot it :)

Is always too fast for see if work, i have test it with a folder with 7 GB of files but i can't see the progress bar, i think work, i'll integrate it in the main code using GUICtrlCreateProgress, and if i have problem i'll post here.

Thanks

Link to comment
Share on other sites

Add a sleep(500) in the loop somewhere to see if it's working. It will pause the script for 1/2 second just so that you can make sure it's working correctly. You wouldn't want to leave that in the script once it's finished though or it will cause your script to take forever to delete lots of files.

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

Link to comment
Share on other sites

  • 6 years later...
On 30.01.2012 at 4:42 PM, BrewManNH said:

Try this script with the ProgressGUI function, you were not using the $FileList array correctly, and you weren't formatting the FileDelete command correctly. See the comments in the code below to see what's been changed.

 

 

#Include <File.au3>
$From_dir = "C:Prova"
$filter = "*.*"

Local $count = 0, $dir = ""
$FileList = _FileListToArray($From_dir, $filter, 0)
If(Not IsArray($FileList)) And(@error = 1) Then SetError(-1)
;~ Global $FileList <<<<<<<<< you redeclared the $FileList array as a variable
Global $ProgressArray = _ProgressGUI("Files to delete = " & $FileList[0], 1)
GUICtrlSetData($ProgressArray[1], 100) ;Set the progress bar to full

For $I = 1 To $FileList[0] ; <<<<<<<<< $FileList is an array, element [0] holds the number of files
 FileDelete($From_dir & "" & $FileList[$I]) ; you need to add the folder name where the files to be deleted are.
 GUICtrlSetData($ProgressArray[1], 100 - Int(($I / $FileList[0]) * 100)) ; Reset the position of the progress bar as you delete files, using a percentage of files to delete
 GUICtrlSetData($ProgressArray[2], "Files to Delete = " & $FileList[0] - $I) ; updates the label of the GUI with the number of remaining files to delete
Next

GUIDelete($ProgressArray[0]) ; deletes the ProgressBar GUI

Proper dir path is very important,
when:
$From_dir = "C:Prova"
in result is error:
Global $ProgressArray = _ProgressGUI("Files to delete = " & $FileList[0], 1)
Global $ProgressArray = _ProgressGUI("Files to delete = " & $FileList^ ERROR
but when:
$From_dir = "C:\Prova"
we have Progressbar and indication of deleting files but they are not deleted
and when
$From_dir = "C:\Prova\"
all works OK

Edited by WiValdiBB
Link to comment
Share on other sites

2 hours ago, Jos said:

@WiValdiBB,

You do realize you are answering an thread which is 6 years old?

Jos

Sure, but is always someone looking for an answers as me and that thread were very usefully, just tested some scripts here and found source of problems in not full correct dir path, that why I made my point
Wald

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

×
×
  • Create New...