Jump to content

Rename file to yesterdays date


Recommended Posts

I'm 90% there. I need to rename the file to yesterday's date, but I'm at a lost as to how to do so.

If FileExists ("c:\temp\*.txt") Then

    $t = FileGetTime("c:\temp\*.txt", 0, 0)

    If Not @error Then
        $mdyyyy = $t[1] & "-" & $t[2] & "-" & $t[0]
    EndIf

    If Not @error Then
    FileMove("c:\temp\*.txt", "c:\temp\" & $mdyyyy & ".txt")
    EndIf

    MsgBox(0, "SUCCESS!", "File renamed to: " & $mdyyyy)

Else
    MsgBox(0, "ERROR!", "File does not exist!")

EndIf
Link to comment
Share on other sites

I notice in your script you are first getting the files creation date. Does that matter? Or do you just need yesterdays date, regardless of files created date? I'm assuming you just need yesterdays date for now. If so, try this:

#include <Date.au3>

If FileExists ("c:\temp\*.txt") Then
    $Date = _DateAdd("d",-1,_NowCalcDate())
    $DateArray = StringSplit($Date,"/")
    $FormattedDate = $DateArray[2] & "-" & $DateArray[3] & "-" & $DateArray[1]
    FileMove("c:\temp\*.txt", "c:\temp\" & $FormattedDate & ".txt") 
Else
    MsgBox(0, "ERROR!", "File does not exist!")
EndIf
Link to comment
Share on other sites

I notice in your script you are first getting the files creation date. Does that matter? Or do you just need yesterdays date, regardless of files created date? I'm assuming you just need yesterdays date for now. If so, try this:

#include <Date.au3>

If FileExists ("c:\temp\*.txt") Then
    $Date = _DateAdd("d",-1,_NowCalcDate())
    $DateArray = StringSplit($Date,"/")
    $FormattedDate = $DateArray[2] & "-" & $DateArray[3] & "-" & $DateArray[1]
    FileMove("c:\temp\*.txt", "c:\temp\" & $FormattedDate & ".txt") 
Else
    MsgBox(0, "ERROR!", "File does not exist!")
EndIf
Yes, I need to rename the file to the day before it was created basically. That is a report that is generated sunday thru friday and they are identified by the day before. This is the way it's always been and they don't want to change. You know how some people accept change. :) Thank you for your help. Is there anyway to rename it to the day before it was created? I know I used modified date - error on my part!
Link to comment
Share on other sites

Hello guys...

Why do you use the move command ? The FileSetTime command does exactly what you want to, you just have to first get the creation date (FileGetTime command)

and calculate the previous date... And apply the FileSetTime command...

Bye

FreeRiderHonour & Fidelity

Link to comment
Share on other sites

you can just use this:

$m0=@MDAY
  $m1=@MON
  $m2=@YEAR
  
  $m3=@MDAY-1 & @MON & @YEAR & ".txt"
  
  msgbox (0,"this is $m3",$m3)
Thats not what he wanted. Also, this wouldn't work anyways because what happens if its December 1? Your script will show December 0.

Yes, I need to rename the file to the day before it was created basically.

Edited by weaponx
Link to comment
Share on other sites

Here you go. Not the easiest or shortest version but it works.

#Include <Date.au3>

RenameFileToDayPriorToCreation("test.txt")

Func RenameFileToDayPriorToCreation($myFile)
    ;Retrieve file creation date (as array)
    $DTArray = FileGetTime ($myFile, 1, 0)
    
    ;Cleanup FileGetTime return value (string return type not compatible with Date UDF)
    $FormattedDate = StringFormat("%s/%s/%s %s:%s:%s",$DTArray[0],$DTArray[1],$DTArray[2],$DTArray[3],$DTArray[4],$DTArray[5])
    
    ;Return date prior to file creation date
    $NewDate = _DateAdd('D', -1, $FormattedDate)
    
    ;Get file extension
    $FNArray = StringSplit($myFile, ".")

    ;Return date in MM/DD/YYYY format, replace slashes with dashes, append original file extension, and rename file
    FileMove($myFile,StringReplace(_DateTimeFormat($NewDate, 2), "/", "-") & "." & $FNArray[$FNArray[0]])
EndFunc
Link to comment
Share on other sites

He could also look at a function that uses

_DateIsValid and _DateDaysInMonth in the event that @MDay -1 returned 0

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

He could also look at a function that uses

_DateIsValid and _DateDaysInMonth in the event that @MDay -1 returned 0

If you use this subtraction method you also have to account for leap years and all of the various month lengths. This would be ideal if we were using a metric calendar :) .

Link to comment
Share on other sites

If you use this subtraction method you also have to account for leap years and all of the various month lengths. This would be ideal if we were using a metric calendar :) .

That's why we have _DateIsValid() in there.

And leap years should be taken into account with _DateDaysInMonth(). ;)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

So if you're already including the Date.au3...maybe it would be best to just use _DateAdd which does exactly this.

Please explain how we use _DateAdd() to subtract a day.

Actually I think That the OPs problem can be solved much easier but he didn't include a sample of a file name to change.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Please explain how we use _DateAdd() to subtract a day.

Actually I think That the OPs problem can be solved much easier but he didn't include a sample of a file name to change.

It doesn't matter what the original filename was. Look in the help file for _DateAdd, negative values work like _DateSubtract...there is an example in there.

Link to comment
Share on other sites

  • Moderators

Maybe ya'll should wait until the OP actually posts what exactly they want for clarity.

I have to agree with weaponx at this point, I'd assume his example is exactly what the OP wanted.

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

It doesn't matter what the original filename was. Look in the help file for _DateAdd, negative values work like _DateSubtract...there is an example in there.

$Older <> $Wiser

Thanks for that.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

hey WeaponX, I tried your code and it deleted the file. :)

btw, what does OP mean?

OP = Original Poster

Edit:

He didn't use FileDelete there, only FileMove to rename it... shouldn't have deleted it, sure you just didn't misplace it ;) ?

Anyway... Might want to use _PathSplit() in that function.

Edited 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

OP = Original Poster

Edit:

He didn't use FileDelete there, only FileMove to rename it... shouldn't have deleted it, sure you just didn't misplace it ;) ?

Anyway... Might want to use _PathSplit() in that function.

ARGH, I didn't place the exe in the same folder. works like a champ. Thanks guys! Learning more everyday! :)

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