Sign in to follow this  
Followers 0
jzachariasen

Output of variable to file...

5 posts in this topic

I have this script, and it works well...

$search = FileFindFirstFile("D:\SWImport\*.*") 
If $search = -1 Then
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    FileDelete("D:\SWImport\"&$file)
WEnd

FileClose($search)

But right before

FileDelete("D:\SWImport\"&$file)
I need to spit the $search variable out to a txt file so I have a log of what was deleted.

Does anybody have an idea about how to do this?

In short, I need $search to append log.txt with its value. Log.txt will then have a list of the files that were deleted.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like this?

CONST $c_Log = @SCRIPTDIR &"\DEL.log"
GLOBAL $h_Log, $h_Search
$h_search = FileFindFirstFile("D:\SWImport\*.*") 
If $h_Search = -1 Then Exit

$h_Log = FileOpen($c_Log,1)
If $h_Log = -1 Then Exit

While 1
    $file = FileFindNextFile($h_Search)
    If @error Then ExitLoop
    IF FileDelete("D:\SWImport\"& $file) THEN
        FileWriteLine($h_Log,$file)
    ENDIF
WEnd

FileClose($h_Search)
FileClose($h_Log)

Ed: Bit better.

Edited by Mobius

Share this post


Link to post
Share on other sites

I have this script, and it works well...

$search = FileFindFirstFile("D:\SWImport\*.*") 
If $search = -1 Then
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    FileDelete("D:\SWImport\"&$file)
WEnd

FileClose($search)

But right before

FileDelete("D:\SWImport\"&$file)
I need to spit the $search variable out to a txt file so I have a log of what was deleted.

Does anybody have an idea about how to do this?

In short, I need $search to append log.txt with its value. Log.txt will then have a list of the files that were deleted.

The _FileWriteLog() UDF automatically formats with a Date/Time stamp:
#include <File.au3>

Global $sLogFile = @ScriptDir & "\LogFile.log"
$search = FileFindFirstFile("D:\SWImport\*.*")
If $search = -1 Then
    _FileWriteLog($sLogFile, "No files found in D:\SWImport")
    Exit
Else
    _FileWriteLog($sLogFile, "File(s) found at D:\SWImport\*.*")
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then
        _FileWriteLog($sLogFile, "Finished deleting files.")
        FileClose($search) 
        ExitLoop
    Else
        _FileWriteLog($sLogFile, "Deleting file:  " & $file)
        FileDelete("D:\SWImport\" & $file)
    EndIf
WEnd

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thank you Mobius, that works great! I may have never figured that one out on my own.

Share this post


Link to post
Share on other sites

PsaltyDS, thank you for the _FileWriteLog tips. It works very well!

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