PDA

View Full Version : Read Line from TXT-file


Pazan
02-16-2009, 06:50 AM
Hi,
I have some files on HDD. Each file consist of 10 lines.
I read the data of file via the function

result = Dialog.FileBrowse(true, "Open File", _SourceDrive, "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
resultthetxt = TextFile.ReadToString(result[1]);
end

Questions are:
1. how to read data from the line "9" of opened file into var raw_9?
2. select 14 symbols from right in this variable into var raw_9_result

Thanks:)

mwreyf1
02-16-2009, 08:55 AM
Can you post what you have now and maybe then we could help.

Unless someone wants to just do it for you which will not help you in learning how to do this on your own.

What your asking for is really very basic which would make it a great way for you to learn AMS.

Teqskater
02-16-2009, 09:55 AM
@Pazan
something like this i guess you want:

raw_9 = resultthetxt[9]
raw_9_result = String.Right(raw_9, 14);

@mwreyf1
which will not help you in learning how to do this on your own.
I don't agree with you anymore. one year ago i would, but i discovered that the best way to learn is have a look at examples and stuff. and just create what you want to create. And then learn step by step and not just reading the manual and what each function does. Just look at his code. He obviously tries.How you know how to copy 14 characters from the right of one string into another if you don't know wich function could achieve that. What's basic for us wasn't basic the first times we used ams. I just takes some time to get used to the functions that you need and stuff. But there also people that don't try something and ask to write a complete script for them. Those people don't deserve help in my opinion because they don't try. Don't be offended by me please. It's not my intention.

best regards,

Teqskater

mwreyf1
02-16-2009, 12:34 PM
@Teqskater

On something as SIMPLE (for even a beginner) as what he is asking, there is no reason for him to not at least attempt to get at least part of it on his own.

If you look at the code he posted that you reffered to as "him trying", none of that code is in no way an attempt at what he was asking for.

His question:
Questions are:
1. how to read data from the line "9" of opened file into var raw_9?
2. select 14 symbols from right in this variable into var raw_9_result

I don't see where he made any attempt at either of those questions.

I agree that it helps to have a helping hand, but when those times come along that are easy code by most standards, then those are the one's that a beginner should try at all cost to figure out on their own. If they can't figure out the most basic then they will never get it.

Teqskater
02-16-2009, 03:23 PM
On something as SIMPLE (for even a beginner) as what he is asking, there is no reason for him to not at least attempt to get at least part of it on his own. Your right about that.

none of that code is in no way an attempt at what he was asking for. Ok. That's right. But he was looking into the right direction. I did not say that it would work.

I don't see where he made any attempt at either of those questions.Also i did not say he attempted solving that.

But ok i get your point right now and i only can say you are right.

Well at least we need to let him figure out the last part of his puzzle. :) right?
When he solves it he's good enough to solve almost everything else.

Nice to have this discussion with you mwreyf1 :yes

Pazan
02-17-2009, 01:56 AM
Well...
Button OnClick:

result = Dialog.FileBrowse(true, "Open File", "AutoPlay\\Docs", "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
raw_9 = resultthetxt[9]
raw_9_result = String.Right(raw_9, 14);
Paragraph.SetText("Paragraph1", raw_9_result);
end

What's wrong here?
:huh OnClick, Line 3: attempt to index global 'resultthetxt' (a nil value)

Imagine Programming
02-17-2009, 04:28 AM
Well...
Button OnClick:
What's wrong here?
:huh OnClick, Line 3: attempt to index global 'resultthetxt' (a nil value)


result = Dialog.FileBrowse(true, "Open File", "AutoPlay\\Docs", "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
resultthetxt = TextFile.ReadToTable(result[1]); --<< You forgot this... <<
raw_9 = resultthetxt[9]
raw_9_result = String.Right(raw_9, 14);
Paragraph.SetText("Paragraph1", raw_9_result);
end

Pazan
02-17-2009, 05:41 AM
...
--<< You forgot this <<[/COLOR]
resultthetxt = TextFile.ReadToTable(result[1]);
...
[/CODE]
OnClick, Line 5: Argument 1 must be of type string
Ok, begin again...
Content of file, from which I try to load line 9:
data.txt
Date: 2009-02-11
Time: 14:48:51
OS: Windows XP
User: Admin
Screen resolution: 1024х768 pix
Total RAM: 238 MB
Host name: IOCTO
IP: 192.168.100.55
MAC:0007e91674fc
End Of file here-----
Button Code OnClick:

result = Dialog.FileBrowse(true, "Open File", "AutoPlay\\Docs", "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
resultthetxt = TextFile.ReadToTable(result[1]);
raw_9 = resultthetxt[9]
raw_9_result = String.Right(raw_9, 14);
Paragraph.SetText("Paragraph1", raw_9_result);
end
in result I want to get in Paragraph1:
0007e91674fc
and nothing anymore

mwreyf1
02-17-2009, 08:41 AM
You are getting C:0007e91674fc as a result of asking for code that would get 14 symbols from the right of line 9 of your text file.

If that is no longer what you want take a look at the String.Right in the code and change the 14 to whatever you want it to now return.

Imagine Programming
02-17-2009, 10:03 AM
Why would you want to get that information from a textfile anyway? this is all information you could retrieve yourself.

For reviewing it later, you could save it as an ini/xml file.

Pazan
02-18-2009, 12:29 AM
If that is no longer what you want take a look at the String.Right in the code and change the 14 to whatever you want it to now return.
I know how to select symbols from right in the line. I DON'T KNOW HOW TO GET VALUE FROM LINE 9 INTO VARIABLE:huh THAT'S ALL. When I try to do this I get errors. See above.

Pazan
02-18-2009, 12:32 AM
Why would you want to get that information from a textfile anyway?
Because I get information from users in TXT-file.

Bruce
02-18-2009, 12:40 AM
Why would you want to get that information from a textfile anyway? this is all information you could retrieve yourself.

For reviewing it later, you could save it as an ini/xml file.

Ahhhh because he wants to?

Imagine Programming
02-18-2009, 04:41 AM
Ahhhh because he wants to?

I have noticed, yes :p

Pazan
02-18-2009, 06:07 AM
Is this question very difficult for guru:huh

ShadowUK
02-18-2009, 07:00 AM
TextFile.ReadToTable(result[1])[9] or "Line not found!";

mwreyf1
02-18-2009, 08:16 AM
IT IS ALREADY IN A VARIABLE !!!

WHAT DO YOU THINK "raw_9_result" IS IN YOUR CODE?

IT'S A VARIABLE!!!

ShadowUK
02-18-2009, 09:25 AM
IT IS ALREADY IN A VARIABLE !!!

WHAT DO YOU THINK "raw_9_result" IS IN YOUR CODE?

IT'S A VARIABLE!!!

Cruise control much? Or what, anger management? Or maybe it's CAPITAL LETTER DAY.

reteset
02-18-2009, 09:25 AM
Button Code OnClick:

result = Dialog.FileBrowse(true, "Open File", "AutoPlay\\Docs", "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
resultthetxt = TextFile.ReadToTable(result[1]);
raw_9 = resultthetxt[9]
raw_9_result = String.Right(raw_9, 14);
Paragraph.SetText("Paragraph1", raw_9_result);
end


hey this code does exactly what you are asking for
if you change red (14) number to 12
the result has to be : 0007e91674fc

Check Your Txt file Encoding
You are probably using a Unicode encoded Txt file
or a different Encoding

Save your txt file with ANSI Encoding and try again

mwreyf1
02-18-2009, 09:38 AM
@ShadowUK

LMAO!!!

My CAPS were in response to his response...

I know how to select symbols from right in the line. I DON'T KNOW HOW TO GET VALUE FROM LINE 9 INTO VARIABLE THAT'S ALL. When I try to do this I get errors. See above.

Just showing him the error of his ways. :D

Desrat
02-19-2009, 03:17 AM
This is what I would do, just remove the bits you know never changes

result = Dialog.FileBrowse(true, "Open File", "AutoPlay\\Docs", "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
resultthetxt = TextFile.ReadToTable(result[1]);
raw_9 = resultthetxt[9]
raw_9_result = String.Replace(raw_9, "MAC:", "", false);
Paragraph.SetText("Paragraph1", raw_9_result);
end

Pazan
02-20-2009, 01:59 AM
I attach file in which I try to select line 9 into variable. Code examples posted above aren't working:
OnClick, Line 5: Argument 1 must be of type string
What an enigma is it:huh

longedge
02-20-2009, 03:08 AM
Have a look at your text file in notepad with word wrap turned off. You will see a single line so you'll never be able to read line 9 :)

Try something like -

result = TextFile.ReadToString("AutoPlay\\Docs\\ACCESS-DENIED_192-168-5-174.txt");
detail = String.Right(result, 12);

Pazan
02-20-2009, 06:34 AM
Have a look at your text file in notepad with word wrap turned off. You will see a single line so you'll never be able to read line 9 :)

Try something like -

result = TextFile.ReadToString("AutoPlay\\Docs\\ACCESS-DENIED_192-168-5-174.txt");
detail = String.Right(result, 12);
Hm...
These files created in AMS7:

analiz_data = "Дата аналізу: "..sys_data.."\nЧас проведення: "..sys_time.."\nОпераційна система: "..OS.."\nКористувач: "..lan.User.."\nРоздільна здатність монітора: "..ekran.Width.."х"..ekran.Height.." пікселів\nФізична оперативна пам’ять: "..ram.TotalRAM.." МБ\nІм’я комп’ютера: "..lan.Host.."\nАдреса в локальній мережі: "..lan.IP.."\nФізична адреса: "..lan.NIC;
result_file = analiz_data;
path_file = Shell.GetFolder(SHF_DESKTOP);
lan_IP = String.Replace(lan.IP, ".", "-", false);
name_file = lan.Host.."_"..lan_IP;
destination_file = path_file.."\\"..name_file..".txt";
TextFile.WriteFromString(destination_file, result_file, false);
save_result = "Дані збережено у файлі:\n\n"..path_file.."\\ "..name_file..".txt".."\n\nДЯКУЄМО ЗА РОЗУМІННЯ!";
Paragraph.SetVisible("Paragraph_Thanks", true);
Paragraph.SetText("Paragraph_Thanks", save_result);

What is wrong i this code? Why result data not wrapped correctly?

longedge
02-20-2009, 06:40 AM
Try -

analiz_data = "Дата аналізу: "..sys_data.."\r\nЧас проведення: "..sys_time.."\r\nОпераційна система: "..OS.."\r\nКористувач: "..lan.User.."\r\nРоздільна здатність монітора: "..ekran.Width.."х"..ekran.Height.." пікселів\r\nФізична оперативна пам’ять: "..ram.TotalRAM.." МБ\r\nІм’я комп’ютера: "..lan.Host.."\r\nАдреса в локальній мережі: "..lan.IP.."\r\nФізична адреса: "..lan.NIC;
result_file = analiz_data;
path_file = Shell.GetFolder(SHF_DESKTOP);
lan_IP = String.Replace(lan.IP, ".", "-", false);
name_file = lan.Host.."_"..lan_IP;
destination_file = path_file.."\\"..name_file..".txt";
TextFile.WriteFromString(destination_file, result_file, false);
save_result = "Дані збережено у файлі:\r\n\r\n"..path_file.."\\ "..name_file..".txt".."\r\n\r\nДЯКУЄМО ЗА РОЗУМІННЯ!";
Paragraph.SetVisible("Paragraph_Thanks", true);
Paragraph.SetText("Paragraph_Thanks", save_result);

That is adding \r to each \n to give you a line break and carriage return.

Imagine Programming
02-20-2009, 06:41 AM
Try "\r\n" instead of "\n" only...

Hm...
These files created in AMS7:

analiz_data = "Дата аналізу: "..sys_data.."\nЧас проведення: "..sys_time.."\nОпераційна система: "..OS.."\nКористувач: "..lan.User.."\nРоздільна здатність монітора: "..ekran.Width.."х"..ekran.Height.." пікселів\nФізична оперативна пам’ять: "..ram.TotalRAM.." МБ\nІм’я комп’ютера: "..lan.Host.."\nАдреса в локальній мережі: "..lan.IP.."\nФізична адреса: "..lan.NIC;
result_file = analiz_data;
path_file = Shell.GetFolder(SHF_DESKTOP);
lan_IP = String.Replace(lan.IP, ".", "-", false);
name_file = lan.Host.."_"..lan_IP;
destination_file = path_file.."\\"..name_file..".txt";
TextFile.WriteFromString(destination_file, result_file, false);
save_result = "Дані збережено у файлі:\n\n"..path_file.."\\ "..name_file..".txt".."\n\nДЯКУЄМО ЗА РОЗУМІННЯ!";
Paragraph.SetVisible("Paragraph_Thanks", true);
Paragraph.SetText("Paragraph_Thanks", save_result);

What is wrong i this code? Why result data not wrapped correctly?

Pazan
02-20-2009, 07:10 AM
Try -
That is adding \r to each \n to give you a line break and carriage return.
I already use these symbols but forgot "\r"
Well... And what code I have to use for select line_9 now?

longedge
02-20-2009, 07:30 AM
Have you re-tried the code that you formerly thought didn't work?

I have to say that the speed at which you are coming back with questions suggests that you aren't putting much effort into finding an answer for yourself ;)

Pazan
02-21-2009, 04:26 AM
Hi,
This code:

result = Dialog.FileBrowse(true, "Open File", "AutoPlay\\Docs", "Text File (*.txt)|*.txt|All Files(*.*)|*.*|", "", "", false, false);
if (result[1] ~= "CANCEL") then
resultthetxt = TextFile.ReadToTable(result[1]);
raw_9 = resultthetxt[9]
raw_9_result = String.Replace(raw_9, "MAC:", "", false);
Paragraph.SetText("Paragraph1", raw_9_result);
end

works great! That's all
Thanx for attention for my liittle trouble ;)

Careliyim
03-27-2009, 06:55 AM
My problem is this


I have a listbox, a combobox and 2 button,


in the combobox there are 2 applications. Portable firefox and portable MSN messenger

I already code this:
when i select in combobox1 tekst "firefox" then button1 start "firefox" when i select combobox text "msn"" then start msn messenger.

this are the application that i can start if I press button1

what i want to add is:
When application starts read from c:\myfile.txt and write to listbox1.

When listbox1 selected text "google earth" then button2 goto ftp://myfile/googleportable.exe download to location autoplay\doc and add txt google earth

What i realy want is that i can let add programs into my program from internet
But only the programs in the text file that are on my ftp server.

I hope you understand this it's realy hard to explain.
If you have a alternate for me i am open for it

Thanxsss