Sign in to follow this  
Followers 0
tkocsir

How to read a file by lines with a command line software?

5 posts in this topic

#1 ·  Posted (edited)

Hi!

I searched a lot, but still didn't find a solution to my problem. I'm new to AutoIt, so I think it is a big problem only for me.:mellow:

I am trying to make an edit field, where user can paste list of files, then presses the Collect files button, and the list of files will be collected (processed) by a command line application.

Here is my code:

#include <Process.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>

GUICreate("something gui",400,300)
Opt("GUICoordMode", 1)
Global $Filelist, $Run_Button, $msg
$Filelist = GuiCtrlCreateEdit("here comes the list of files",10,45,300,200)
$Run_Button = GUICtrlCreateButton("Collect files", 315, 45, 80)

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Run_Button
            $EnteredData = GUICtrlRead($Filelist)
            FileWrite("entereddata.txt",$EnteredData)
            
            $file = "entereddata.txt"
            
            FileOpen($file, 0)
            For $i = 1 to _FileCountLines($file)
            $line = FileReadLine($file, $i)

            Run('"collectfiles.exe -c " $line & " C:\files\" $line')
            
            Next
            FileClose($file)
            
            FileDelete("entereddata.txt")
    EndSelect
WEnd

To the edit control I wait this kind of format data:

c:\example.com

c:\folder\smtnhg.exe

d:\directory\file.txt

...

So I write the list into a txt file, then I read it line by line.

But then there is this line:

Run('"collectfiles.exe -c " $line & " C:\files\" $line')

I also tried _RunDOS, Shellexecute, but none of them worked, nor this Run. The collectfiles -c waits for a source file and then a destination, but I cannot "insert" the filenames from the txt to the command line.

How is it possible to do this?

Thans,

Thomas

edit: removed some hungarian comments from the code :P

Edited by tkocsir

Share this post


Link to post
Share on other sites

Your error stays in this line:

Run('"collectfiles.exe -c " $line & " C:\files\" $line')

- you will need to provide full path for collectfiles.exe

- you are not using "&" where it should

Run('"collectfiles.exe -c " & $line & " C:\files\" & $line')

"&" is a concatenation operator, you need to use it anytime you need to have variables and text in a command line.

SciTE should have given you an error on that line.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Hi enaiman!

SciTE didn't give error any error message to this code.

I also tried before to put "&" operators to the appropriate places, but didn't work.

Now I tried it with fill path, but didn't work.

If I write the result of the Run command to a file like this:

$tofile = Run('"D:\collectfiles.exe -c " & $line & " C:\files\" & $line')

FileWrite("result.txt",$tofile)

Then the result.txt will contain a zero ("0"). But I also managed somehow earlier to have the result.txt this content:

'"D:\collectfiles.exe -c " & $line & " C:\files\" & $line'

So it seems it don't want to change the $line to the filename. But if it worked, I would also need a space character before and after the filename and I don't know how to do that neither.

(Maybe it seems that I want only to copy files and AutoIT would do it easy, but no, this is a special command line software that I have to use)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well - I was hopping that the script will run for you and to avoid further work on this ... well ...

Your script is not the best it can be - it works (it will work) but it can be done much easier.

Here it comes:

#include <Process.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>
#include <Array.au3>        ;for debugging only

GUICreate("something gui",400,300)
Opt("GUICoordMode", 1)
Global $Filelist, $Run_Button, $msg, $FileContent
$Filelist = GuiCtrlCreateEdit("here comes the list of files",10,45,300,200)
$Run_Button = GUICtrlCreateButton("Collect files", 315, 45, 80)

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Run_Button
            _FileReadToArray($Filelist, $FileContent)
            If @error Then
                MsgBox(16, "Error", "File reading ERROR")
                Exit
            EndIf
            _ArrayDisplay($FileContent, "The content of your FileList") ;debug line - to show the content of the file
            For $i = 1 To $FileContent[0]
                ;debug line - to show the command line syntax
                ConsoleWrite ('cmdline:   "collectfiles.exe -c "'&$FileContent[$i]&" C:\files\"&$FileContent[$i]&@CRLF)
                ;
                Run("d:\collectfiles.exe -c "&$FileContent[$i]&" C:\files\"&$FileContent[$i])
            Next
    EndSelect
WEnd

I've just realized that it was my fault in the previous post - have a look in the current script to see how "Run" should look.

Note 1: once you get rid of the debug lines you can see how simple your script looks now :P

Note 2: if your file content looks like: "c:\example.com ...." you will run into some problems - the last part of the commandline would look like "C:\Files\c:\example.com"; guess you'll want to avoid that :mellow:

Well that looks like a bit of practice to you (hint: _PathSplit )

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Thank you very much for your help enaiman, now it works! :mellow:

At first, your code gave me the file reading error, but I corrected it and it works. It seems you forgot to write the content of the edit box into a file.

Here is the code:

#include <Process.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>
#include <Array.au3>        ;for debugging only

GUICreate("something gui",400,300)
Opt("GUICoordMode", 1)
Global $Filelist, $Run_Button, $msg, $FileContent
$Filelist = GuiCtrlCreateEdit("here comes the list of files",10,45,300,200)
$Run_Button = GUICtrlCreateButton("Collect files", 315, 45, 80)

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Run_Button
            $EnteredData = GUICtrlRead($Filelist)
            FileWrite("entered.txt",$EnteredData )
            _FileReadToArray("entered.txt", $FileContent)
            If @error Then
                MsgBox(16, "Error", "File reading ERROR")
                Exit
            EndIf
            _ArrayDisplay($FileContent, "The content of your FileList") ;debug line - to show the content of the file
            For $i = 1 To $FileContent[0]
                ;debug line - to show the command line syntax
                ConsoleWrite ('cmdline:   "collectfiles.exe -c "'&$FileContent[$i]&" C:\files\file"&$i&".ext"&@CRLF)
                ;
                Run("d:\collectfiles.exe -c "&$FileContent[$i]&" C:\files\file"&$i&".ext")
            Next
    EndSelect
WEnd

When I modified this part:

Case $msg = $Run_Button

$EnteredData = GUICtrlRead($Filelist)

FileWrite("entered.txt",$EnteredData )

_FileReadToArray("entered.txt", $FileContent)

to this:

Case $msg = $Run_Button

$EnteredData = GUICtrlRead($Filelist)

_FileReadToArray($EnteredData, $FileContent)

then the script read the content of the test file that I typed into the edit box (it contained 2700+ lines and the collectfiles.exe wanted to copy them as files one by one :party: :party:)

I modified the result filename too, but I will have a look at that _PathSplit thing you advised.:P

Thank you very much again!

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