Jump to content

Help creating a script that will remove folders


Recommended Posts

I have backup up folders that contains exchange files, which are set to create a new folder when backup is successful.

My question is: can I remove folder that are older than 7 day's with a script? I have a mapped drive and the folders are in a folder called exchange.

Any help would be greatly appreciated

Edited by Jaymac
Link to comment
Share on other sites

I have backup up folders that contains exchange files, which are set to create a new folder when backup is successful.

My question is: can I remove folder that are older than 7 day's with a script? I have a mapped drive and the folders are in a folder called exchange.

Any help would be greatly appreciated

Hey Jaymac,

Take a look in the AutoIt helpfile for these two commands:

FileFindFirstFile

FileGetTime

With some coding of those two functions you should be able to easily do what you want=)

Szhlopp

Link to comment
Share on other sites

Hey Jaymac,

Take a look in the AutoIt helpfile for these two commands:

FileFindFirstFile

FileGetTime

With some coding of those two functions you should be able to easily do what you want=)

Szhlopp

Awesome, thanks a lot - I really appreciate it.

Link to comment
Share on other sites

This might help you out:

#118151

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

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

;Root folder
$sourceFolder = @ScriptDir & '\'

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders!
Dim $found[1]

;Loop through array
For $X = 1 To $fileList[0]
    ;Retrieve creation time of file
    $Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)
    ;Format date for use with Date UDF
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
    ;Calculate age, remove files older than seven days
    If _DateDiff('d', $fDate, _NowCalc()) < 7 Then  ; the time
        ;FileDelete($sourceFolder & "\" & $fileList[$X])
        _ArrayAdd($found, $sourceFolder & $fileList[$X])
        ;MsgBox(1, "Files deleted:", $fileList[$X], 1)
    EndIf
    $found[0] = UBound($found)
Next
_ArrayDisplay($found)

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

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

;Root folder
$sourceFolder = @ScriptDir & '\'

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders!
Dim $found[1]

;Loop through array
For $X = 1 To $fileList[0]
    ;Retrieve creation time of file
    $Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)
    ;Format date for use with Date UDF
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
    ;Calculate age, remove files older than seven days
    If _DateDiff('d', $fDate, _NowCalc()) < 7 Then  ; the time
        ;FileDelete($sourceFolder & "\" & $fileList[$X])
        _ArrayAdd($found, $sourceFolder & $fileList[$X])
        ;MsgBox(1, "Files deleted:", $fileList[$X], 1)
    EndIf
    $found[0] = UBound($found)
Next
_ArrayDisplay($found)

Mega

Where exactly would I put the folder path? Sorry, this is my first time using this program.

Ex: I want to remove folders that are located here: H:\Exchange\Exchange1

I have a folders within Exchange1 called IMG000623, 000624, 000625 and so on. I want to be able to remove the folders that are seven day's old.

Also, does #include <Date.au3>, #include <File.au3> mean that I have to include those into the script as well?

Link to comment
Share on other sites

1)

$sourceFolder = @ScriptDir & '\'

will read as

$sourcefolder = "H:\Exchange\Exchange1\"

2) No u dont need to include those files, Autoit will include those files into the script. let the script editor worry about it

when u mention u are new (i am not a good autoit programmer yet, btw) would suggest u use scite4autoit from home page which makes new programmer's life much much easier with syntax highlighting to calltips, etc.

Link to comment
Share on other sites

Just to clarify, $sourceFolder = @ScriptDir & '\' will be "H:\Exchange\Exchange1\"

Only if the script is run from H:\Exchange\Exchange1\

You can use a $sourceFolde = FileSelectFolder("Choose a folder.", @ScriptDir)

to make it interactive.

1)

$sourceFolder = @ScriptDir & '\'

will read as

$sourcefolder = "H:\Exchange\Exchange1\"

2) No u dont need to include those files, Autoit will include those files into the script. let the script editor worry about it

when u mention u are new (i am not a good autoit programmer yet, btw) would suggest u use scite4autoit from home page which makes new programmer's life much much easier with syntax highlighting to calltips, etc.

Link to comment
Share on other sites

What exactly are these lines telling me?

$fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])

;Calculate age, remove files older than seven days

If _DateDiff('d', $fDate, _NowCalc()) < 7 Then ; the time

I got it to remove files and not folders as well

$fileList = _FileListToArray($sourceFolder, "*", 1) I changed the 1 to a 2

Please help. thanks. apperciate all the help

Link to comment
Share on other sites

from the helpfile

format control = The format and flags to use

Escape characters can be contain in the "format control" such as \n (@LF), \r (@CR), \t (@TAB]. So if you want to have a "\" you need to use \\, samething for "%" %%.

you'd end up with

$Date[0]/$Date[1]/$Date[2] $Date[3]:$Date[4]:$Date[5]

or

19/03/2009 16:47:33

Hope that explains it...

if not look in the help file. search stringformat and its the second one down (pretty self explanitary)

MDiesel

edit: in answer, don't change anything

Edited by mdiesel
Link to comment
Share on other sites

Where ist your problem? The script is pretty much self-explanatory, isn't it?

It is documented. (Okay older should be younger)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Where ist your problem? The script is pretty much self-explanatory, isn't it?

It is documented. (Okay older should be younger)

$fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders! (2 doesn't actually remove the folders for me)

Also, If _DateDiff('d', $fDate, _NowCalc()) < 7 Then ; the time (is still just removes files (not folders) and it removes the files modified from today.

Sorry, I do networking and servers - i'm a newb to coding

Link to comment
Share on other sites

$fileList = _FileListToArray($sourceFolder, "*", 1) ; 2 = folders! (2 doesn't actually remove the folders for me)

Also, If _DateDiff('d', $fDate, _NowCalc()) < 7 Then ; the time (is still just removes files (not folders) and it removes the files modified from today.

Sorry, I do networking and servers - i'm a newb to coding

Hi,

so you want to remove all files and folders in a folder which are older than 7 days, right?

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Try this:

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

;Root folder
$sourceFolder = @ScriptDir & '\Test\'

;Gather files into an array
$fileList = _FileListToArray($sourceFolder, "*", 0) ; 0 = files and folders
Dim $found[1]

;Loop through array
For $i = 1 To $fileList[0]
    ;Retrieve creation time of file
    $Date = FileGetTime($sourceFolder & "\" & $fileList[$i], 0, 0) ; 0 modified, 1 = created
    ;Format date for use with Date UDF
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])
    ;Calculate age, remove files older than seven days
    If _DateDiff('d', $fDate, _NowCalc()) > 7 Then ; the time
        If StringInStr(FileGetAttrib($sourceFolder & $fileList[$i]), 'D') Then
            DirRemove($sourceFolder, 1)
        Else
            FileDelete($sourceFolder & "\" & $fileList[$i])
        EndIf
        _ArrayAdd($found, $sourceFolder & $fileList[$i])
        ConsoleWrite($sourceFolder & $fileList[$i] & ' deleted' & @CRLF)
    Else
        ConsoleWrite('!' & $sourceFolder & $fileList[$i] & @CRLF)
    EndIf
Next
$found[0] = UBound($found)
_ArrayDisplay($found, 'Deleted items')

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Hi,

so you want to remove all files and folders in a folder which are older than 7 days, right?

Mega

You should take care about that. The modification times of a folder reflects the time a file was added or removed within it.

So a folder can be older than one of the file which it contains.

A other point : a file can be recently created, and modified since a longer time ( copy an old file in a new folder, you will have a new creation date, but the same old modification date ).

You didn't said if you want recursion or not, but with recursion, the problem becomes:

- 1/ remove all files older than 7 days ( creation and/or modification date ? better is to take the 2 dates in account)

- 2/ remove all folders which became empty in 1.

@xenobiologist : you should take care about return code of FileGetTime : sometimes, file haven't time at all, and function returns an error. ( some files in temporary internet files for example )

Edited by sksbir
Link to comment
Share on other sites

HI,

it was just an example oh how it could be done.

Error checking and check whether it is exactly what the thread owner wanted is another task. (by him)

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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...