Sign in to follow this  
Followers 0
bdb4269

easier way to do this?

14 posts in this topic

OK -- my script works fine and all -- but it seems like there is probably a WAY easier method to do the following. (Like I wouldn't be surprised if someone replies with a way to do this in like 3 lines or something.)

Basically, before the script outputs a file, it checks if the file already exists. And if so, it renames, the existing file, with "old_1" or if that also already exists "old_2", etc. through "old_9".

As you can see, the way I do it, is quite long. It's already programmed, so I'm really only asking to try to educate myself more, so I can hopefully program more efficiently.

So -- does anyone know an easier way to do this?

If FileExists($dailySPSSName) Then
            If FileExists($dailySPSSName1) Then
                If FileExists($dailySPSSName2) Then
                    If FileExists($dailySPSSName3) Then
                        If FileExists($dailySPSSName4) Then
                            If FileExists($dailySPSSName5) Then
                                If FileExists($dailySPSSName6) Then
                                    If FileExists($dailySPSSName7) Then
                                        If FileExists($dailySPSSName8) Then
                                            If FileExists($dailySPSSName9) Then
                                                $errletter = "s" 
                                                CantRunMes($errletter)
                                                Exit
                                            Else
                                                FileMove($dailySPSSName, $dailySPSSName9)
                                            EndIf
                                        Else
                                            FileMove($dailySPSSName, $dailySPSSName8)
                                        EndIf
                                    Else
                                        FileMove($dailySPSSName, $dailySPSSName7)
                                    EndIf
                                Else
                                    FileMove($dailySPSSName, $dailySPSSName6)
                                EndIf
                            Else
                                FileMove($dailySPSSName, $dailySPSSName5)
                            EndIf
                        Else
                            FileMove($dailySPSSName, $dailySPSSName4)
                        EndIf
                    Else
                        FileMove($dailySPSSName, $dailySPSSName3)
                    EndIf
                Else
                    FileMove($dailySPSSName, $dailySPSSName2)
                EndIf
            Else
                FileMove($dailySPSSName, $dailySPSSName1)
            EndIf
        EndIf

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Or you can use either the current time or a GUID so you won't have to check (or check once, max)

Edited by SaphuA

Share this post


Link to post
Share on other sites

for $c=0 to 10
if fileexists($dailtySPSSName&$c) =  1 then filemove($dailtySPSSName&$c,$dailtySPSSName&$c&"_old1",1)
next
Thanks for the quick reply!!! --- but I think I may not have been clear about what I am trying to do.....

There is not 10 files that might need to be renamed with "_old1_"

There is just a single file, and if it exists, it should be renamed with _old1_ at the end -- BUT if a file by that name (i.e. with _old1_ at the end) then it should instead be renamed with _old2_ at the end, BUT if a file by that name (i.e. with _old2_ at the end) then it should instead be renamed with _old3_ at the end, etc. etc. etc.

The file names have the date, so the purpose is basically, so if need be, the script can be run up to 10 times in the same day, without ever deleting or overwriting the output from any of the runs.

In case this helps clarify any, here are the values of the variables. (the first variable, is the name of the file from the last/current run, if it exists, it is renamed to the first name, that is not already being used.)

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day & ".sav" 
$dailySPSSName1 = "E:\" & $projectName & "spss" & $month & $day & "_old1_.sav" 
$dailySPSSName2 = "E:\" & $projectName & "spss" & $month & $day & "_old2_.sav" 
$dailySPSSName3 = "E:\" & $projectName & "spss" & $month & $day & "_old3_.sav" 
$dailySPSSName4 = "E:\" & $projectName & "spss" & $month & $day & "_old4_.sav" 
$dailySPSSName5 = "E:\" & $projectName & "spss" & $month & $day & "_old5_.sav" 
$dailySPSSName6 = "E:\" & $projectName & "spss" & $month & $day & "_old6_.sav" 
$dailySPSSName7 = "E:\" & $projectName & "spss" & $month & $day & "_old7_.sav" 
$dailySPSSName8 = "E:\" & $projectName & "spss" & $month & $day & "_old8_.sav" 
$dailySPSSName9 = "E:\" & $projectName & "spss" & $month & $day & "_old9_.sav"

Share this post


Link to post
Share on other sites

Or you can use either the current time or a GUID so you won't have to check (or check once, max)

I appreciate the input -- and those could certainly work, but for various reasons beyond my control, the old1/old2 is the naming scheme desired by my department at work.

I more just looking for an easier way to do what I have already done --- not because I really need to change the script, it works as it is -- more just because I am curious, and I want to learn more about autoit.

It seems like there would be an easier to way to get the same exact functionality as I have programmed -- but it may just be, that there is not, --- and that instead, there are just easier ways, to get the general objective done, by a more logical means, like you suggest, by using time, or GUID.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

How about This solution?

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day 

If FileExists($dailySPSSName & '.sav') Then
 for $i = 8 to 1 Step -1
  If not FileExists($dailySPSSName & '_old' & $i & '_.sav') Then ContinueLoop
  $nextfile = $i + 1
  MsgBox(0, $dailySPSSName & '_old' & $i & '_.sav   Exists!!!', 'Moving file to:   test_old' & $nextfile & '_.sav');Not necessary, just to show whats going on
  FileMove($dailySPSSName & '_old' & $i & '_.sav', $dailySPSSName & '_old' & $nextfile & '_.sav', 1)
 Next
 MsgBox(0, $dailySPSSName & '.sav   Exists!!!', 'Moving file to:   ' & $dailySPSSName & '_old1_.sav');Not necessary, just to show whats going on
 FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old1_.sav', 1)
EndIf
FileDelete($dailySPSSName & '.sav')

;Now Write Your file!!
Edited by Varian

Share this post


Link to post
Share on other sites

Muchos Gracias. I knew there was an easier way. I was thinking it would probably have to do with a for loop with a -1 step , but the part I was missing was checking that the file IS there, and then just adding 1 to get the filename I want.

It seems so obvious, now that you showed me that. :P

I tweaked it a little, because I actually dont want each file renamed/incremented every time, I just want the main file name, changed to the next non-used number.

But the important part, was figuring out the concept I wasn't quite grasping, and you showed it to me perfectly. Thanks much. :P

Here is how I tweaked it to get exactly what I want, in case anyone wants to see it.

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day
If FileExists($dailySPSSName & '.sav') Then
    for $i = 8 to 0 Step -1
        If Not FileExists($dailySPSSName & '_old' & $i & '_.sav') Then ContinueLoop
        $nextfile = $i + 1
        FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old' & $nextfile & '_.sav')
    Next
EndIf

Share this post


Link to post
Share on other sites

If I understand you correctly, the newest file will go to the last file, up until $dailySPSSName_old9_.sav, right? And if $dailySPSSName_old9_.sav already exists, you send a message box saying so and exiting? Just for the cherry on top, I added the "old9" code you provided, changed the count from 0 to 1 (because there will never be an "old0" file, right?), and added the Exitloop so the check stops when it finds the highest file. Also, willthe month & date be the same for all the files? If not, then we'll need to exclude these strings from the check. Maybe if we only search for the strings "_old" and ".sav" Anyways, that's me trying to make it totally fullproof.

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day
If FileExists($dailySPSSName & '.sav') Then
  If FileExists($dailySPSSName & '_old9_.sav') Then
    $errletter = "s"
    CantRunMes($errletter)
    Exit
  EndIf
  for $i = 8 to 1 Step -1
    If Not FileExists($dailySPSSName & '_old' & $i & '_.sav') Then ContinueLoop
    $nextfile = $i + 1
    FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old' & $nextfile & '_.sav')
    ExitLoop
  Next
EndIf

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

lol - I had just realized I forgot the error message, and was going to come back to fix it, and I scrolled to the bottom and at first glance saw your code box, which looked a lot like what I did when I was working on in Scite, and for a split second I was like "what?!? how the hell did my changes from scite get put in my post automagically!"

Then i ofcourse realized it wasn't my post :P

But I had still missed the needed "ExitLoop" - so my version still would not have worked right. So thanks much for pointing that out. :P

As far as the month day thing, basically they always equal the current month day, (both in 2 digit format). So the renaming is only meant for if there is more than 1 in a day.

One thing though -- I believe the for loop, still needs to goto 0 -- you are correct, that old0 will never exist, but I think the 0 needs to be there so that if only the original file exists, (i.e. old1 does not exist yet) it will get all the way down to 0 before it skips the ContinueLoop, and then it will add 1 to $i making $nextfile = 1 for the FileMove.

So here (i think this time), is the code that should work.

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day
If FileExists($dailySPSSName & '.sav') Then
    If FileExists($dailySPSSName & '_old9_.sav') Then
        $errletter = "s"
        CantRunMes ($errletter)
        Exit
    EndIf
    For $i = 8 To 0 Step - 1
        If Not FileExists($dailySPSSName & '_old' & $i & '_.sav') Then ContinueLoop
        $nextfile = $i + 1
        FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old' & $nextfile & '_.sav')
        ExitLoop
    Next
EndIf
Edited by bdb4269

Share this post


Link to post
Share on other sites

I just realized, that still would not work, because old0 has to exist (not "not exist") for it to skip the continueloop

but if the for loop only goes to one, then it will never rename to old1 -- I think I can figure this out -- I'll post back when i do.

Share this post


Link to post
Share on other sites

OK -- I think this would do the trick (still may be able to be simplified)

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day
If FileExists($dailySPSSName & '.sav') Then
    If FileExists($dailySPSSName & '_old9_.sav') Then
        $errletter = "s"
        CantRunMes ($errletter)
        Exit
    EndIf
    $do_old_1 = 0
    For $i = 8 To 1 Step - 1
        If Not FileExists($dailySPSSName & '_old' & $i & '_.sav') Then ContinueLoop
        $nextfile = $i + 1
        FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old' & $nextfile & '_.sav')
        $do_old_1 = 1
        ExitLoop
    Next
    If $do_old_1 = 0 Then FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old1_.sav')
EndIf

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

This should work:

Counts down from 8 to 1 & checks for old & "count"; if file exists, renames file to the next file higher

If the counter goes down to 1, checks if "old1" exists...if not, write file to "old1", otherwise, write to "old2"

Your code had a flaw that if counter was at 1, it would write to "old2", & even if "old1" did not exist, you have

already renamed it so your final FileMove wouldn't find the current file(it's already been renamed)

$dailySPSSName = "E:\" & $projectName & "spss" & $month & $day
If FileExists($dailySPSSName & '.sav') Then
    If FileExists($dailySPSSName & '_old9_.sav') Then
        $errletter = "s"
        CantRunMes($errletter)
        Exit
    EndIf
    for $i = 8 to 1 Step -1
        If $i <> 1 and Not FileExists($dailySPSSName & '_old' & $i & '_.sav') Then ContinueLoop
            ;Either we've found our highest file, or we're at 1 If counter is 1 and "old1" does not exist, we'll name current file to "old1"
        If $i = 1 and Not FileExists($dailySPSSName & '_old1_.sav') Then
                FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old1_.sav')
                ExitLoop;exits loop since we just renamed it to "old1"
        Else
            ;Even if we are at 1, file will be named to 2 - 9 because if "old1" did not exist, If-Then would have renamed the current file to "old1"
                $nextfile = $i + 1
                FileMove($dailySPSSName & '.sav', $dailySPSSName & '_old' & $nextfile & '_.sav')
                ExitLoop
        EndIf
    Next
EndIf

Slight edit..I needed to add "If $i <> 1 and" in order for the loop to continue if counter is down to 1

EDIT:::Your code did work!! I was working the logic in my head but I realized my mistake when I ran it..So either way you want to go, looks good

Edited by Varian

Share this post


Link to post
Share on other sites

personnaly to backup file I use this :

Local $Rootname = GetRootNameFile($myFile)
Local $Extension = GetExtension($myFile)
If $Extension<>"" Then $Extension = "." & $Extension
Local $nbBackup = ""
Local $Nb = 0
While FileExists ($Rootname & "_BAK" & $nbBackup & $Extension)
    $Nb = $Nb + 1
    $nbBackup = "(" & $Nb & ")"
WEnd
FileCopy($myFile,$Rootname & "_BAK" & $nbBackup & $Extension)

Func GetExtension($MyString)
    $MyPos=StringInStr($MyString,".",0,-1)
    If $MyPos Then
        Return StringMid($MyString,$MyPos+1)
    Else
        Return ""
    EndIf
EndFunc

Func GetRootNameFile($MyString)
    $MyPos=StringInStr($MyString,".",0,-1)
    If $MyPos Then
        Return StringLeft($MyString,$MyPos-1)
    Else
        Return $MyString
    EndIf
EndFunc

It gives me this :

original file is :

test.txt

if I use the script 4 times, backup files are :

test_BAK.txt

test_BAK(1).txt

test_BAK(2).txt

test_BAK(3).txt

I guess you just have to replace FileCopy function with FileMove and remove _BAK from the script to goet what you want (so you don't have to use date or time)...

Share this post


Link to post
Share on other sites

I like it. Thanks! :P

Share this post


Link to post
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
Sign in to follow this  
Followers 0