Jump to content

[Solved] Deleting folders 1 year older from the current year


Recommended Posts

Hi Experts,

Good day, hope everyone is okay😊.

I have some issue right now concerning folders to be deleted if 1 year older already from the current year 2019. I have my script below but it only deletes months within the current year.

$source = GUICtrlRead($BrowsePath) & "\" ; browsing the path of folder to delete code is above this...
               Global $aList = _FileListToArray($source, "*", 2)
                  For $i = 1 To $aList[0]
                     $aDate = FileGetTime($source & $aList[$i], 0, 0)
                     $sDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2]
                     
                     ; Per months, and if it's true, then delete the folder and all its subfolders
                     If _DateDiff("M", $sDate, _NowCalcDate()) > 2 Then ; this is per month declare not year.
                        DirRemove($source & $aList[$i], 1)
                     EndIf
                  Next

 

I tried changing the parameter from "M" to "Y" in _DateDiff() but still no success.😟

 

Hope you can advise or suggest what should be done? Thanks in advance experts.

 

KS15

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@Subz, yes I tried that I change the part here.

If _DateDiff("M", $sDate, _NowCalcDate()) > 2 Then

To this:

If _DateDiff("M", $sDate, _NowCalcDate()) > 12 Then

But still the same, folders were not deleted.

 

@FrancescoDiMuro, I've got the below consoleWrite().
 

2018/05/22 ; Year older of a folder
15 ; how month old is the folder
R:\archiving\ARCHIVE\Receive\01032017\ABC3899> ; Path were the folder is.
Exit code: 0    Time: 0.3408

I tried declaring the months old but still no success.

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@Subz, here's the result.

2018/05/22
DirRemove("R:\archiving\ARCHIVE\Receive\01032017\ABC3899", 1) - 2018/05/22 ; this your result
R:\archiving\ARCHIVE\Receive\01032017\ABC3899 ; deleting using DirRemove($source & $aList[$i], 1) but still the folder was not deleted.
>Exit code: 0    Time: 0.3419

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

HI @KickStarter15,

I tried your code and the _DateDiff() output was the exactly the same when setting the parameter $sType of the function to "Y" and "M"..
Using the help file I saw the example of the _DateDiff function :

Local $iDateCalc = _DateDiff('s', "1970/01/01 00:00:00", _NowCalc())

So, you might need to add the fields hour:minutes:seconds to your $Date.

I tried it, adapt the following code to your need and tell me if this is what you needed:

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

$source = "C:\Test"& "\"
$Verbose=True

;$source = GUICtrlRead($BrowsePath) & "\" ; browsing the path of folder to delete code is above this...
Global $aList = _FileListToArray($source, "*", 2)
   For $i = 1 To $aList[0]

      If $Verbose Then ConsoleWrite(" VERBOSE: Folder="&$aList[$i]&@CRLF)

      $aDate = FileGetTime($source & $aList[$i], 0, 0)
      $sDate = $aDate[0] & "/" & $aDate[1] & "/" & $aDate[2] & " " &$aDate[3]& ":" &$aDate[4]& ":" &$aDate[5]

      If $Verbose Then
         ConsoleWrite(" VERBOSE: FormattedDate="&$sDate&@CRLF)
         ConsoleWrite(" VERBOSE: DateDiff-Month(s)="&_DateDiff("M", $sDate, _NowCalcDate())&@CRLF)
         ConsoleWrite(" VERBOSE: DateDiff-Year(s)="&_DateDiff("Y", $sDate, _NowCalcDate())&@CRLF)
      EndIf

      If _DateDiff("Y", $sDate, _NowCalcDate()) >= 1 Then
         ;(If dateDiff >= 1 year.)
         If $Verbose Then ConsoleWrite(" VERBOSE: DirectoryRemove "&$source&$aList[$i]&@CRLF)
         ;DirRemove($source & $aList[$i], 1)
      EndIf
   Next


Here is the ConsoleWrite output I got when running it in my test folder:

 VERBOSE: Folder=RecentFolderCreatedToday
 VERBOSE: FormattedDate=2019/09/02 10:45:25
 VERBOSE: DateDiff-Month(s)=-1
 VERBOSE: DateDiff-Year(s)=-1
 VERBOSE: Folder=AnOldFolderFrom2017
 VERBOSE: FormattedDate=2017/12/28 17:53:56
 VERBOSE: DateDiff-Month(s)=20
 VERBOSE: DateDiff-Year(s)=1
 VERBOSE: DirectoryRemove AnOldFolderFrom2017
>Exit code: 0    Time: 0.341

 

Link to comment
Share on other sites

@Taskms4, Thanks, but still it wasn't deleting the folder. I tried adding DirRemove() but it was not deleted. However, it is showing all the information but not the case.😅

If _DateDiff("Y", $sDate, _NowCalcDate()) >= 1 Then
         ;(If dateDiff >= 1 year.)
         If $Verbose Then ConsoleWrite(" VERBOSE: DirectoryRemove "&$source&$aList[$i]&@CRLF)
         DirRemove($source & $aList[$i], 1) ; Added here to delete the folder but still wont be deleted.
      EndIf

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@KickStarter15 Strange, it deleted my old folder when I ran the script...
No open files from that directory preventing the deletion?

 

EDIT: Saw this in the help file for the function DirRemove() 

 

Quote

 

Remarks

Some directory attributes can make the deletion impossible, therefore if this is the case look at FileSetAttrib() to change the attributes of a directory.

 

 

Edited by Taskms4
Link to comment
Share on other sites

@Taskms4, If I declared the IP Add of my computer and it was deleting (locally) but if I run it in a regular path without IP it won't be deleted. Now I tried declaring the IP of a server (map on my computer) it won't delete the folder.

6 minutes ago, Taskms4 said:

No open files from that directory preventing the deletion?

No there's no open files on this one.

 

@Subz, The same with your sample given above, it is deleting in my local drive but when it come to our server even using the IP Add. it won't delete the folder and it was map already in my computer with full access.

 

Any Idea why?🤔

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@KickStarter15 Tried the code on a remote server and it's working fine... 

Either you do not have the rights to access the remote folder, or your remote path is not correct.
I'm considering that you have the proper rights-level (Write AND Modify) like it was for my test, I created the same folder (C:\Test) on a remote server and I used the following path for the source:

;$source = "C:\Test"& "\"               ;(Local Path)
$source = "\\myremotesrv\c$\Test"& "\"  ;(Remote path)

 

EDIT: here is the ConsoleWrite() output, and the remote folder has effectively been deleted
 VERBOSE: Folder=OldFolderFrom2018
 VERBOSE: FormattedDate=2018/04/18 15:52:00
 VERBOSE: DateDiff-Month(s)=16
 VERBOSE: DateDiff-Year(s)=1
 VERBOSE: DirectoryRemove \\myremotesrv\c$\Test\OldFolderFrom2018

Edited by Taskms4
Adding ConsoleWrite output
Link to comment
Share on other sites

Thanks, Experts for sharing and suggesting. All you samples are working, the only thing that I missed was my access to that server. I lately noticed that I don't have the right access to delete on our archiving directory😅. I tested the code to my office mate's PC and the code is working even my first original script posted above.✌️😂

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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