Jump to content

File Searching


someone
 Share

Recommended Posts

Hey everyone,

Before I get into my problem I wanted to thank for community for all the support and of course thank everyone involved for creating autoit.

My problem is related to file searching. Let me start off with an overall idea of what I am doing with my program. I am writing an installer that will allow the user to install novell zenworks apps unattended. Since the apps themselves are changed fairly often and since there are a lot of buildings with slightly different ways of distributing apps the best way for me to do this is have a GUI and a prompt for the user to copy paste the path of the application and then the installer will use an .ini file to make the install go unattended. I need to use ini files since no one has any idea of coding (even less then me which is scary) but they can use my ini files as a template to make any needed changes to an application or whatever. Eventually I'll make a recorder to write the ini's but this way is fine for right now.

OK my problem is the path the user will copy paste looks something like this:

abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz

Sorry for not using an actual example but I can't since it shows network information.... Anyway so the path is huge; most of the x y z stuff is related to specific location, like NYC.MH.US. Because I don't want to make an ini file for each building and each application plus each place might have different versions of the apps themselves.

So ultimately while the path might be abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz I need the ini file to be abcdefg123-6-12-R1.ini If it were the other way around doing FileFindFirstFile would be easy, but I don't know how to search for a file with less characters in it. I originally tried using this;

$app2 = ("abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz")
$var = StringLeft (""& $app2, 5)
$search = FileFindFirstFile (& $var"*.*")

I know the code isn't comlete and the syntax is prob off on this I just whipped this up to show you what I was doing so you could get the idea.

Doing this does find the file, but its going to happen where 2 different ini files are very closely named, like:

abcdefg123-6-12-R1.ini

abcdefg123Client-6-12-R1.ini Or

abcdefg123-6-12-R2.ini

The point is I can't use a set number of characters in the original app path to search for the ini, in my example if I used 5 all 3 of those ini files would have been found. It seems sloppy to me to have it find all those and then say have a GUI box prompt the user to choose the correct one, but maybe thats all I can do?

Is there anyway to have autoit search by closest to the original string? So if the string is abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz it will only find abcdefg123-6-12-R1.ini not abcdefg123-6-12-R2.ini or anything else??

I couldnt' find anything searching for this but maybe I'm just not approaching the question right. If my post is waaaay to confusing let me know I'll clean it up, I hate when posters give you no idea of what they are trying to accomplish.

Thank you to everyone in advance for the help. If you need more info let me know.

Thanks :whistle:

Andrew

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

It sounds like you are getting the answer you want with FileFindFirstFile. Are you then calling FileFindNextFile in a loop? Don't do that.

FileFindFirstFile won't match a filename with less characters then the search string(or am I missing something?).

I was using the FileFindNextFile in a loop... I got the example out of the help file;

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

But even without the loop the code doesn't work...

This is my code; so you can recreate exaclty what I am doing I have this script and an ini file abcdefg123-6-12-R1.ini in the same folder.

$app2 = ("abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz")
$search = FileFindFirstFile (& $app2"*.*")

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

It doesn't find anything since the ini file is abcdefg123-6-12-R1.ini ; its missing all the characters after the R1. Is that what you were asking?

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Your syntax is false: try $app2&"*.*" to search for anything that begins with $app2. Didn't you get errors on this one?

$app2 = ("abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz")

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile($app2&"*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)
Edited by dabus
Link to comment
Share on other sites

Your syntax is false: try $app2&"*.*" to search for anything that begins with $app2. Didn't you get errors on this one?

$app2 = ("abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz")

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile($app2&"*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

Turns out I was one Beta ver behind; which is why I wasn't getting a syntax error before, however using your code I still get no results. If you create a fake ini file abcdefg123-6-12-R1.ini and place in the folder with the script do you get a positive search result?

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Turns out I was one Beta ver behind; which is why I wasn't getting a syntax error before, however using your code I still get no results. If you create a fake ini file abcdefg123-6-12-R1.ini and place in the folder with the script do you get a positive search result?

Unless I am missing something you will never find abcdefg123-6-12-R1.ini with the search that you are doing. You are in effect searching for abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz*.*


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Try this:

#Include <File.au3>
#Include <Array.au3>

$app2 = "abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz"

$ini = getIni($app2,@scriptdir)
msgbox(0,"INI file found:", $ini)

func getIni($appid,$dir)
    $FileList=_FileListToArray($dir,"*.ini")
    If (Not IsArray($FileList)) and (@Error=1) Then
        MsgBox (0,"","No Files\Folders Found.")
        Exit
    EndIf
    for $i = 1 to $FileList[0]
        $inifile = $FileList[$i]
        $inifileShort = StringTrimRight ($inifile,4) ; trim off the ".ini"
        if stringinstr($appid,$inifileShort) Then return $inifile
    Next
EndFunc

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

Here is another to try based on your last one.

$app2 = StringLeft("abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz", 15)

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile($app2 & "*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Here is another to try based on your last one.

$app2 = StringLeft("abcdefg123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz", 15)

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile($app2 & "*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

The reason I can't use StringLeft I think I said in my original post... but the $app2 var will never equal a specific length of text... for instance onetime it is abcdefghijkl123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz another time is it just abc123-6-12-R1-ttt.uuu.vvv.ww.xx.yy.zzz so cutting off letters will either make the search find nothing or be about as accurate as only using say the first 2 characters. The problem also is the app names are similiarly named so I want to avoid multiple hits in searching.

To lod3n;

What you did seems to work although looking at it briefly I can't quite figure out why. I'm actually attempting to sneak out of work early as we speak so I'll spend more time on it hopefully that is what I need.

SmOke_N;

I'll take a look at what you gave me too, might be a bit overkill but I'm interested in read the code more and see if I can make heads or tails of it.

Thanks a lot everyone I'll let you know how it works out

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

In plain English, my script is doing this:

Retreive the filename from each ini file in that directory, and then remove the .ini at the end of the filename for comparison purposes.

If that shortened filename string can be found within the target string, we have a match.

Edited by lod3n

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

Hey lod3n,

It sounds perfect, thanks for explaining it to me... I'm gonna do some trial and error to see how acurate it is, but in theory its %100 accurate right? Since its matching the ini to the app var it uses all the characters in the ini file minus the extension so it *should* only match to an app string with ALL the characters in it right?

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Under the parameters you have specified, yes. The only thing I can think of that might be a problem is if _FileListToArray didn't return the list of files in alphabetical order.

I don't know if that can happen or not. If so, you could get an alphabetical file list this way:

#include <Constants.au3>

func _FileListToArrayAlpha($dirname,$filter)
    $foo = Run(@ComSpec & ' /c dir /b /on ' & $filter, $dirname, @SW_HIDE, $STDOUT_CHILD)
    $output = ""
    While 1
        $output &= StdoutRead($foo)
        If @error Then ExitLoop
    Wend
    return stringsplit(StringStripWS($output,3),@crlf,1)    
EndFunc

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

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