Jump to content

Output of variable to file...


Recommended Posts

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.

Link to comment
Share on other sites

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

wtfpl-badge-1.png

Link to comment
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
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...