Jump to content

How do I sustain a variable outside of a "While"?


Recommended Posts

I have a folder that will contain one unique filename, always ending in the extension .4@Y. I'm using the following code to give my script the file name:

;Get the filename
$search = FileFindFirstFile("C:\*.4@Y)

While 1
    $filename = FileFindNextFile($search)
    If @error Then ExitLoop
Wend

FileClose($search)

Now if I insert my message box-- MsgBox(0,"File Name", "This is your file: " & $filename) before the "Wend" it works like a charm. If I put it at the end the variable is lost.

Is there a way to keep the $filename variable outside of the While statement? I suspect I should use a Dim, but I really don't know. Can anyone help?

Link to comment
Share on other sites

GIVE ME THE "d"

GIVE ME THE "I"

GIVE ME THE "m" ...

WHAT DOES IT SAY ?????? ....

DIM .. DIM ... DIM .... DONG !!! ... yes you are right I think there´s a DIM missing at the top of the program !!!

^_^

[s][font="Impact"]░▒▓▓►DrKovra◄▓▓▒░[/font][/s]The only thing I [sup]know [/sup]is that I don't know [sub]nothing[/sub]--------------- __________------------------------------ __________---------------

Link to comment
Share on other sites

#include <Array.au3>
Local $FoundFiles

;Get the filename
$search = FileFindFirstFile("C:\*.4@Y")

While 1
    $filename = FileFindNextFile($search)
    If @error Then ExitLoop
    $FoundFiles &= $filename & '|'
Wend
FileClose($search)
$FoundFiles = StringTrimRight($FoundFiles, 1)
$FoundFiles = StringSplit($FoundFiles, '|')
_ArrayDisplay($FoundFiles)

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

If you are attempting to run the code you gave inside a function and read the value outside of that function then it would account for this problem. That can be solved like this.

;
Global $FileName
Func _FindMyFile()
;Get the filename
Local $search = FileFindFirstFile("C:\*.4@Y)
While 1
    Local $filename = FileFindNextFile($search)
    If @error Then ExitLoop
Wend
FileClose($search)
EndFunc
MsgBox(0,"File Name", "This is your file: " & $filename)
;

OR

;
$Filename = _FindMyFile()
MsgBox(0,"File Name", "This is your file: " & $Filename)
Func _FindMyFile()
;Get the filename
Local $search = FileFindFirstFile("C:\*.4@Y)
While 1
    Local $filename = FileFindNextFile($search)
    If @error Then ExitLoop
Wend
FileClose($search)
Return $Filename
EndFunc
;

Just remember that, if there is more than 1 matching file, only the last match will be returned.

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

GIVE ME THE "d"

GIVE ME THE "I"

GIVE ME THE "m" ...

WHAT DOES IT SAY ?????? ....

DIM .. DIM ... DIM .... DONG !!! ... yes you are right I think there´s a DIM missing at the top of the program !!!

^_^

Global is preferable to DIM but it makes for a bad cheer.

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

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...