Sign in to follow this  
Followers 0
MarkMarkMark

Renaming Files

8 posts in this topic

Hi guys, Yeah its me again,

I have some questions...

Here is the case:

I have a folder with 20 files in it, all named like this: aa1.*** (*** stands for something)

al files have the extension .txt

now i want autoit to grab the 1st file (not the autoitscript itself), rename it to "running.txt" ,wait 2 minutes , deletes the file, grab the next file and rename that to running.txt, wait 2 minuten, delete the file, grab the next file... and on an on and on until the folder is empty.

I asked a couple of weeks a question bout something that looks like this and i've been playing with the code a bit but i cant figure it out:

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop;done finding files
    
    MsgBox(4096, "File:", $file)
    
; Check if file opened for reading OK
    If $file = -1 Then ContinueLoop;couldn't open file; continue with next file
    
;throw away the first 10 lines
    For $i = 1 to 10
        FileReadLine($file)
    Next
    
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        FileWriteLine($file & ".new.txt", $line)
    Wend
    
  
WEnd

; Close the search handle
FileClose($search)

This code trows away the 1st 10 lines of each document.

But now i want to mod this script to my needs but i cant get it working...

any1 go some idea's?

Thanks alot.

-mark

Share this post


Link to post
Share on other sites



untested

; Shows the filenames of all files in the current directory
$search = FileFindFirstFile("*.*")

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop;done finding files
    
    MsgBox(4096, "File:", $file)
    
; Check if file opened for reading OK
    If $file = -1 Then ContinueLoop;couldn't open file; continue with next file
    
;throw away the first 10 lines
;~   For $i = 1 to 10
;~       FileReadLine($file)
;~   Next
;~
;~   While 1
;~       $line = FileReadLine($file)
;~       If @error = -1 Then ExitLoop
;~       FileWriteLine($file & ".new.txt", $line)
;~   Wend
    If $file <> "Yourscript.au3" Then; skip your script
        FileMove($file, "running.txt", 1); rename as running.txt
        Sleep(2 * 60 * 1000); wait 2 min
        FileDelete("running.txt)
    EndIf
    
WEnd

; Close the search handle
FileClose($search)


AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.

Share this post


Link to post
Share on other sites

Mmm, you missed 1 " at the end of FileDelete("running.txt)

but next to that,

the script does this now:

Give a msgbox with "."

then delete all files, including the script... and leaves only the 1st file in the directory and renamed that to running.txt..

mmm this is not good :(

Share this post


Link to post
Share on other sites

Mmm, you missed 1 " at the end of FileDelete("running.txt)

but next to that,

the script does this now:

Give a msgbox with "."

then delete all files, including the script... and leaves only the 1st file in the directory and renamed that to running.txt..

mmm this is not good :(

<{POST_SNAPBACK}>

True, some mistakes, but untested. Have a look in the Helpfile for FileFindFirstFile.

Then you can correct it yourself. Correcting someone's code is a good way to learn also.

I would assume this is missing from the loop ?

If '.' Then ContinueLoop
If '..' Then ContinueLoop
If @ScriptName Then ContinueLoop

Share this post


Link to post
Share on other sites

Ok, Im a bit on the way i think (...)

$search = FileFindFirstFile("*.*")

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
        If @error Then ExitLoop
 
    If $file = -1 Then ContinueLoop
    If '.' Then ContinueLoop
    If '..' Then ContinueLoop
    If @ScriptName Then ContinueLoop
    If '*.txt' Then go()

    MsgBox(4096, "File:", $file)
    


WEnd


FileClose($search)


func go()
        FileMove($file, "running.txt", 1)
        Sleep(2 * 60 * 1000); wait 2 min
        FileDelete("running.txt")
endfunc

Am i doing this right?

tnx in advanced...

-Mark

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Oops sorry, I intentionany left skipping . and .. out because I thought since it wasn't included already that

If $file = -1 Then ContinueLoop;couldn't open file; continue with next file
was skipping past them. As for the missing ", yes my fault. Yes looks ok now :(

Edit: Darn sticky keys was off

Edited by steveR

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.

Share this post


Link to post
Share on other sites

Try this instead:

$search = FileFindFirstFile("*.*")

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    
    If $file = '.' Then ContinueLoop
    If $file = '..' Then ContinueLoop
    If $file = @ScriptName Then ContinueLoop
    go()
WEnd

; Close the search handle
FileClose($search)


Func go()
    FileMove($file, "running.txt", 1)
    Sleep(2 * 60 * 1000); wait 2 min
    FileDelete("running.txt")
EndFunc

Share this post


Link to post
Share on other sites

tnx guys, that worked great :(

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