Sign in to follow this  
Followers 0
Diana (Cda)

[Resolved] If folder contains more than AU3 files, open ...

9 posts in this topic

#1 ·  Posted (edited)

On a test, this works fine, so I'm on the right track:

If FileExists($FolderPath & $Filetype1) Then
        ShellExecute($WEReplacement, $FolderPath, "", "open", @SW_MAXIMIZE)     ; opens in WEReplacement
    Else
        Exit     ; finished
EndIf
The folder is never empty, it contains AutoIt scripts. However, it's a dumping folder so there could be any number of file types in there at any given moment. Since I I keep forgetting to empty the folder when it does contain other than the base scripts, rather than just putting a shortcut to the folder into the startup menu so it opens every time on bootup, it would be so much simpler if AutoIt opens it only if it isn't empty, meaning it contains more than the base AI scripts.

But I ran into 2 scenarios. Telling AI to open it only if there are are files OTHER THAN AutoIt scripts, that would be easiest solution, something like this:

If FileExists($FolderPath & "\ALL FILES BESIDES AU3 FILE FORMAT") Then
        ShellExecute($WEReplacement, $FolderPath, "", "open", @SW_MAXIMIZE)     ; opens in WEReplacement
    Else
        Exit     ; finished
EndIf

Barring that, I guess the only solution would be to list the top likeliest file formats, say, MP3 and AVI.

Trouble lies with the If statement. There is ability to cobble together with AND, right? But what does one do about an OR situation? Help talks about If Then Else EndIf but couldn't find anything about if this OR if that ...

How about how to make this work since AI doesn't like the OR - again, only if there's no way to get AI to open the folder using syntax along the lines of if folder contains OTHER THAN the AU3 files in folder ...:

If FileExists($FolderPath & $Filetype1) Or
    If FileExists($FolderPath & $Filetype2) Then
        ShellExecute($WEReplacement, $FolderPath, "", "open", @SW_MAXIMIZE)     ; opens in WEReplacement
    Else
        Exit     ; finished
EndIf

Thanks once again. :x

Edited by Diana (Cda)

Share this post


Link to post
Share on other sites



You were dancing all around the correct syntax. You can just comine the two FileExists() statements with the OR in a single If..Then..EndIf statement. Logically, it reads: "If either condition 1 is true or condition 2 is true, then proceed."

If FileExists($FolderPath & $Filetype1) Or FileExists($FolderPath & $Filetype2) Then
    ShellExecute($WEReplacement, $FolderPath, "", "open", @SW_MAXIMIZE)     ; opens in WEReplacement
Else
    Exit
EndIf

Share this post


Link to post
Share on other sites

Damn! I got the error just because I didn't put the OR on the first line with the rest <lol>. Too funny!

I ended up making the file formats into variables just in case I have to list a whole bunch more. Otherwise the line will get very long and very unwieldly very fast! <g>

;=================================================
$Condition1 = FileExists($FolderPath & $Filetype1)
$Condition2 = FileExists($FolderPath & $Filetype2)
;=================================================


If $Condition1 Or $Condition2 Then
        ShellExecute($WEReplacement, $FolderPath, "", "open", @SW_MAXIMIZE)     ; opens in WEReplacement
    Else
        Exit     ; finished
EndIf

So, if no coding can make this easier by just opening the folder if it isn't empty because it contains MORE THAN the AU3 files, then listing all the possible file formats that might be there is the next best possible solution.

Thanks. :x

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is a way to check with FileFindFirstFile() so no includes are necessary...not that bad unless you have 1000s of files in the folder

$File = FileFindFirstFile($FolderPath & '\*.*')
If $File = -1 Then Exit

While 1
    $Next = FileFindNextFile($File)
    If @error Then ExitLoop
    ;Next Line reads: If current file has a "." in it and extension = au3, search for next file
    If StringRegExp($Next, '\.', 0) And StringRegExpReplace($Next, '^.+\.', '') = 'au3' Then ContinueLoop
    ;The Next 2 lines only run if a file is found without an extension or if the extension <> au3
    ShellExecute($WEReplacement, $FolderPath, "", "open", @SW_MAXIMIZE)     ; opens in WEReplacement
    ExitLoop
WEnd
Exit
Edited by Varian

Share this post


Link to post
Share on other sites

Here is a way to check with FileFindFirstFile() so no includes are necessary...not that bad unless you have 1000s of files in the folder ...

Thanks, Varian!

I actually personally don't use a lot of UDFs. I basically only have one that I use all the time. I even tend to extract stuff even when building GUIs. Hate compiling and having thousands of extra confusing lines when all it takes is adding a bit of syntax so that I can leave out UDF references entirely.

However, what I am a junkie of is variables! That I definitely plead guilty. I'm not yet a very experienced AutoIt code writer even though I've been pummeling this stuff into my brains for a few years now <g>. So once I figure something out, I tend to structure it so that the next time I use the code, it's simply a question of a quick change to small bits of pertinent data within the script and having those present as easy variables at the top. So the above is 99% variables not references to UDFs <g>. Hope it's not too confusing, though I'm sure it is half the time.

But this is still good to know. Thanks for this! :)

Share this post


Link to post
Share on other sites

Thanks, Varian, for the help. I've had this script in my startup folder now for a few days and the folder only opens up on Windows reboot when it has files of a certain type. This means that the folder no longer accumulates junk which I forget to clean out. I keep it neat and tidy now very easily.

Cheeers. :)

Share this post


Link to post
Share on other sites

Hi Diana,

FYI, (may save some flamming later on)

-filefindfirst is a function not a UDF. UDF's all have an underscore prefix

-The change that Varian made was not just moving the "or" operator, but also eliminating the compound "if"

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Here is a way to check with FileFindFirstFile() so no includes are necessary

kylomas, good to get things straightened out, thanks.

I started writing out a query but then a light bulb went off. Okay, I happened to by accident label all my own personal include files with an underscore at the front way back when. I just saw some labelled that way and copied that since it kept all my personal ones all together at the top. The "includes" you must mean are all the other files in the folder without that underscore. But the other ones aren't called UDFs, if I've understood correctly, just our own. Okaaay. I understand now.

Still, the meaning is the same ... includes vs. UDFs. Despite having a handful of UDFs and a ton of includes that come with AI, I personally only usually reference one at any given moment <g>.

Goodness, hope no-one flames. Wasn't intended as anything incendiary. Just a comment to explain why I have a ton of variables in my small, simple scripts (well, simple compared to all you geniuses around here <lol>). It might look like I'm referencing a lot of includes and/or UDFs but I have noticed that I tend to use more variable than I see others using in simple scripts like mine. The big, complicated scripts, sure. Lots of variables there, but that's just logical <g>.

Thanks for teaching me re includes vs. UDFs. Didn't really grasp that before.

:)

Share this post


Link to post
Share on other sites

Hi Diana,

-The change that Varian made was not just moving the "or" operator, but also eliminating the compound "if"

kylomas

Thanks for pointing that out. I missed that. When I see code I don't yet understand, my eyes tend to cross (figuratively, not literally) <lol>. I'll definitely come back to this code again at some point to see if I've got a better handle on it. I betcha it'll come in handy for others in the forum and for me once I wrap my brain around some of the syntax. Regex and string stuff tend to throw me off still.

Cheers. :)

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