Sign in to follow this  
Followers 0
ubelong2matt

Reading a File That Changes

9 posts in this topic

I'm trying to write a script that automates a very tedious and time consuming process. The process is manual restoration of backed up information including configuration, files, and etc. The step I'm on is to add printers using the add printer tool. The printers for each customer are stored in a text document that is created during the backup process and stored on a shared drive until the PC is reimaged. Here's what this step must do:

  • Navigates to a directory that never changes computer to computer (see example below).
  • Opens a file that always has the username_computer.txt
  • Searches for and reads a certain part of the text document and stores each different count of this as a variable.
Step one and three I can do easily either by what I know now or with what I'll read when I get there. The problem I'm having is actually on step two. Because AutoIt doesn't incorporate wildcards, it is impossible for me to find the correct text file. As the username of the person will always be the same (except in extraneous circumstances), the computer name may not be due to a replacement PC. At my company we use PC names that follow the same suit but the numbers at the end may change. There is only going to be at most, one (1) text file in this folder with a bunch of other folders full of information. Having one text file in the root of the folder may assist with a resolution but I just can't find a function to perform what I could do with that.

What is the easiest solution to find this particular text file without the use of wildcards? Or, does anyone know of a way to incorporate wildcards into AutoIt and parse the file that way?

Here is an example of my script that is just going to open the file (oh, and the loc and fileexist() information is important to keep separate as they are used in different places):

Func test()
    Local $FolderLoc = @SystemDir & "\old\" & @ComputerName
    Local $yesFolder = FileExists($FolderLoc)
    Local $Folder = FileRead($FolderLoc & "\" & @UserName & "_DOG123456.txt")
    
    $file = FileOpen($Folder, 0)

    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
EndFunc

The "DOG123456" is what changes. Any help would be greatly appreciated and thank you for taking the time to read this!

Share this post


Link to post
Share on other sites



Thank you very much for your reply! I'll look into that function.

Share this post


Link to post
Share on other sites

for matching the file, consider:

If StringInStr($file, @ComputerName) Then
; insert your search function here
EndIf

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Well finding the file correctly is the problem I'm having right now. I can find what I need to in the file (I think) when it gets to that point. However, when I try to use the FileFindFirstFile() function, I get mixed results.

If I follow the example for that function in the documentation, it gives me a list of all the files in the script's directory in a while...when loop. When I try to add my code to it to see if it would do what I want it to do, it breaks. So I started trying to play with it and came up with this script thinking it would work:

test()

Func test()
    $file = FileOpen(FileFindFirstFile(@DesktopDir & "\*.txt"), 0)
    $read = FileReadLine($file, 1)
    
    MsgBox(0, "test", $read)
EndFunc

The output of that should be the word "test". Problem is, it just comes up with a blank box and nothing in it. The document I'm trying to test a find for is just some test.txt on the desktop which contains multiple words on different lines. Perhaps I'm just not understanding what FileFindNextFile() does exactly as it seems to need the while...when loop to actually output anything.

Edited by ubelong2matt

Share this post


Link to post
Share on other sites

Is the @Workdir equeal to the @Desktopdir?

When that is not the case I am sure the FileOpen() will fail....

Now you could have tried to debug this yourself by adding tests for @error and the returned information.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Using the example in the help file for FileFindFirstFile as a guide:

; Shows the filenames of all files in the current directory.
$path = "C:\test"
$search = FileFindFirstFile($path & "\*.txt")
$myfile = ""
; 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
    If StringInStr($file, @ComputerName) Then $myfile = $file
WEnd

; Close the search handle
FileClose($search)
If $myfile = "" Then
    MsgBox(0, "Error", "File not found")
Else
    MsgBox(0, "", $path & "\" & $myfile)
EndIf

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

I've tried adding the full path to the file and surpassing the macro shortcuts; the results are the same. If I leave out the line read, I get a -1 in the prompt signifying an error with the FileFindFirstFile() function. Oh, and originally my code used the FileFindNextFile() function but I changed it in that code and in mine and the results didn't change.

Share this post


Link to post
Share on other sites

Thanks for the script Spook. That did the trick. I manipulated your script to get it to pop up the message box with the correct path to my file and told it to read the 9th line from the file. Everything worked great. I really appreciate everyone's help with this!

P.S. Sorry for the double post but it won't let me edit my previous post as it did with my earlier one.

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