FileExists has a wonderful alternative!
Dear user,
Here some thoughts about using the action command FileList as alternative to FileExists.
Suppose, my music folder contains several files such as
Bach-BWV545.mp3
Bach-BWV563.mp3
Bach-BWV564.mp4
Suppose further, that we make a VisualNeo Win program that checks if a specific file exists.
For this task, VisualNeo has the action command FileExists:
SetVar "[File]" "Bach-BWV545.mp3" FileExists "[PubDir][File]" "[ReturnValue]" If "[ReturnValue]" "=" "True" AlertBox "Status" "The file [File] was found in the folder [PubDir]." EndIf
We could also use the command FileList:
SetVar "[File]" "Bach-BWV545.mp3" FileList "[PubDir][File]" "Files" "[ReturnValue]" If "[ReturnValue]" "=" "[File]" AlertBox "Status" "The file [ReturnValue] was found in the folder [PubDir]." EndIf
Notice that FileList returns the variable [ReturnValue], that contains the filename if the file exists (here: Bach-BWV545.mp3).
If the file is not found, then the variable [ReturnValue] is empty.
FileList has even more to offer thanks to the wildcard characters * and ? and []
1. Wildcard character *
* matches any character zero or more times.
To check if a specific file exists and only knowing an unique part of the filename (here the number after BWV), use:
SetVar "[PartFileName]" "545" FileList "[PubDir]*[PartFileName]*" "Files" "[ReturnValue]" If "[ReturnValue]" "<>" "" AlertBox "Status" "The file [ReturnValue] was found in the folder [PubDir]." EndIf
2. Wildcard character ?
? matches a single character.
To check if a specific file exists but not knowing if the file has the extension mp3 or mp4, use:
SetVar "[PartExtensionFileName]" ".mp?" FileList "[PubDir]BWV564.[PartExtensionFileName]" "Files" "[ReturnValue]" If "[ReturnValue]" "<>" "" AlertBox "Status" "The file [ReturnValue] was found in the folder [PubDir]." EndIf
3a. Brackets []
Use brackets [ and ] to look for any of the characters between the brackets. The previous example could be improved:
SetVar "[PartExtensionFileName]" ".mp[#91]34[#93]"
Now the so called character class or character set [34] matches 3 and 4. In this case .mp3 and .mp4 are matched.
Notice the use of [#91] and [#93] in SetVar: these ASCII values for the brackets [ and ] are necessary to have the job done.
A dash between two characters suggests a range of value:
SetVar "[PartFileName]" "B[#91]a-e[#93]ch*"
matches a filename that starts with the string Bach, Bbch, Bcch, Bdch or Bech.
3b. ! in Brackets []
An exclamation mark prevents matching any of the characters between the brackets.
SetVar "[PartFileName]" "B[#91]!a[#93]ch*" FileList "[PubDir]*[PartFileName]*" "Files" "[ReturnValue]"
The variable [ReturnValue] will now be empty.
Conclusion
Till now I used FileList in two ways (what seems a common practice looking at posts on https://archive.visualneo.com)
- To generate a list of all files: FileList “[PubDir]*.*” “Files” “[ReturnValue]”
- To generate a list of files with a specific extension like .txt: FileList “[PubDir]*.txt” “Files” “[ReturnValue]”
Since last week I use FileList as an alternative to FileExists if only parts of a filename are known, using the characters *, ? and [] – the latter whether or not with the exclamation mark !
Best regards,
Reinier
Comments
agilefalcon
That’s a great example of how to use an action command in more than the obvious way. Thanks for sharing.
Gaev
Great article; learned something new … who says you can’t teach an old dog new tricks !!!
luiz
Great. Thanks.
CN_Iceman
Thanks for the new info!!
Vadim
Thanks so much for a useful lesson!