Jump to content

Error in expression when using @systemdir


Recommended Posts

title says it all

hello again guys it been ages since ive been on here, i have come accross an issue that i cant find anything about in the help file

Line 37

$filedir=FileFindFirstFile(@Systemdir"\Users\"@Username"\Local\Temp")
$filedir=FileFindFirstFile(^ERROR

error:Error in expression

what this is trying to do is remove temp files on a windows 7 user account, got big plans for this to be a highly useful optimization tool

any ideas?

thanks in advance

source attached

note: error doesnt occur until you press the "sweeper" button

test.au3

Edited by snowman533

Intermediate AutoIt/Autohotkey User

Link to comment
Share on other sites

Hi snowman533,

You are not concatenating the strings together with "&". I did not look at your attachment.

; path seems incorrect. look at the next attempt for comparison.
$filedir = FileFindFirstFile(@Systemdir & "\Users\" & @Username & "\Local\Temp")

; I would try
$filedir = FileFindFirstFile(@HomeDrive & "\Users\" & @Username & "\Local\Temp")

; or perhaps better
$filedir = FileFindFirstFile(@TempDir)

Check the syntax with Au3check and see if it gives you another error to fix. :huh2:

Edit:

I normally a a glob (star) character after the find path i.e. @tempDir & '\*'. Not tested if it works without the glob.

Edited by MHz
Link to comment
Share on other sites

Use Scite. In the tools menu, look for SyntaxCheck. That entry runs Au3check on your script to check for literal syntax errors.

FileFindNextFile() is missing the error check immediately after the call. The @error macro is cleared upon the next function call so you need to check it immediately or store it in a variable for later use,

Change your loop to

While 1
    $filearray = FileFindNextFile($filedir)
    If @error Then ExitLoop; <<<<< exit out of loop upon error condition
    FileDelete($filearray)
WEnd

The variable name $filearray implies an array. $filearray is a string returned from FileFindNextFile().

Link to comment
Share on other sites

It will not loop fully when FileFindFirstFile() and FileFindNextFile() are both in the same loop.

I did some changes to your script

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("Mustdeclarevars", 1)

Main()
Func Main()
    Local $Gui1, $Gui1Button1, $msg
    $Gui1 = GUICreate("a test window", 600, 450, 150, 150, -1, -1)
    GUISetState(@SW_SHOW, $Gui1)
    $Gui1Button1 = GUICtrlCreateButton("Utils", 10, 10, 50, 20)
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $Gui1Button1
                GUISetState(@SW_HIDE, $Gui1); hide 1st GUI
                Win7Utils()
                GUISwitch($Gui1); switch GUIGetMsg() back to 1st GUI
                GUISetState(@SW_SHOW, $Gui1); show 1st GUI
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete($Gui1)
EndFunc   ;==>Main

Func Win7Utils()
    Local $Gui2, $Gui2Button1, $msg, $filearray, $filedir
    $Gui2 = GUICreate("Windows 7 Utilities", 600, 450, 150, 150, -1, -1)
    GUISetState(@SW_SHOW, $Gui2)
    $Gui2Button1 = GUICtrlCreateButton("sweeper", 10, 10, 50, 20)
    GUISwitch($Gui2); switch GUIGetMsg() to 2nd GUI
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $Gui2Button1
                $filedir = FileFindFirstFile(@TempDir & "\*.*")
                ConsoleWrite(@TempDir & @CRLF)
                If $filedir <> -1 Then
                    While 1
                        $filearray = FileFindNextFile($filedir)
                        If @error Then ExitLoop
                        FileDelete($filearray)
                    WEnd
                    MsgBox(0, "Success", "sweep success")
                EndIf
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete($Gui2)
EndFunc   ;==>Win7Utils

Perhaps works closer to what you may want. :huh2:

Link to comment
Share on other sites

still isnt deleting the Temp Files

after poiinting it again to the <Username>\AppData\Local\Temp\*.* it just sits there, no success message, no activity at all (opening the temp dir shows all the temp files are still there) any ideas?

using the code as is just simple puts a success message on screen hasnt actually done anything thats why i tried to repoint it to the username Temp folder

Edited by snowman533

Intermediate AutoIt/Autohotkey User

Link to comment
Share on other sites

Tip: Using "*.*" in Windows is totally useless because Windows will read it as "*" anyway.

I tested this and it worked fine.

$sTemp = @TempDir
MsgBox(0, "Result", $sTemp);  Just to show you the path
If FileExists($sTemp & "\*") Then ;; no sense going there if the folder is empty.
    _EmptyFolder($sTemp)
    ShellExecute(@TempDir)
Else
    MsgBox(0, "Error", "No files exist in the Temp Dir")
EndIf

Func _EmptyFolder($s_Folder)
    Local $s_Hold = @WorkingDir, $s_File
    FileChangeDir($s_Folder)
    MsgBox(0, "New working folder", $s_Hold & @CRLF & @WorkingDir) ;; Just to show that we changed the path
    Local $h_Find = FileFindFirstFile("*")
    If $h_Find <> -1 Then
        While 1
            $s_File = FileFindNextFile($h_Find)
            If @Error Then ExitLoop
            If @Extended Then
                If NOT DirRemove($s_File, 1) Then ContinueLoop ;; Happens if there is a file in the folder which can not be deleted
            Else
                If NOT FileDelete($s_File) Then ContinueLoop ;;  This can happen if the file is in use or the permissions are wrong so move on to the next one
            EndIf
        WEnd
    EndIf
    FileChangeDir($s_Hold)
    MsgBox(0, "Working Directory", $sTemp & @CRLF & @WorkingDir) ;; To show that we changed the working dir back to what we started with
EndFunc

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

Sorry snowman533, I should have seen the issue. My copying and pasting of code sometimes makes me miss the simplest of things.

FileFindNextFile() returns just the file name. Unless the script has the same working directory as the target, then you may need to provide a path to the target.

Change

FileDelete($filedir)
to

FileDelete(@TempDir & '\' & $filedir)
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...