I am using CADCAM Software and writing macro code. Here is my basic macro code.
OPEN "C:\Area ratio\etch.txt" FOR INPUT as #1
DO WHILE NOT EOF (1) =1
LINE INPUT #1, REC$
if REC$="" then goto jump2
'PRINT REC$
y2#=Y2#-200
Addtext# x2#,y2#,0,0,REC$
jump2:
LOOP
CLOSE #1
Clearmarkers#
end#
This code will be working fine. but it will read the text and print line by line.
I need to print read whole text file at once.
Sub Test()
'Tools -> References -> Microsoft Scripting Runtime
Dim fso As New FileSystemObject
Dim txt As TextStream
Dim all_text As String
Set txt = fso.OpenTextFile("c:\Temp\textfile.txt")
all_text = txt.ReadAll
txt.Close
End Sub
Related
How to read excel file in Perl CGI?
my $book = ReadData('$lBASEPATH/macro/$gProj/TCO_Excel_Export/TCO_Data_201931316467.xlsx');
say 'A1: ' . $book->[1]{A1};
print $book;
Enclose the path to the .xlsx file using double quotes inside ReadData function.
my $book = ReadData("$lBASEPATH/macro/$gProj/TCO_Excel_Export/TCO_Data_201931316467.xlsx");
say 'A1: ' . $book->[1]{A1};
print $book;
It is not possible to do interpolation with single quotes.
I am new to GUI and i was trying to create a simple GUI in tcl. It have a push button which when pressed runs a code and generates a output '.l' file in the directory. But i want the output to be printed in the GUI itself. SO how am i supposed to change this code to do the task.
proc makeTop { } {
toplevel .top ;#Make the window
#Put things in it
label .top.lab -text "This is output Window" -font "ansi 12 bold"
text .top.txt
.top.txt insert end "XXX.l"
#An option to close the window.
button .top.but -text "Close" -command { destroy .top }
#Pack everything
pack .top.lab .top.txt .top.but
}
label .lab -text "This is perl" -font "ansi 12 bold"
button .but -text "run perl" -command { exec perl run_me }
pack .lab .but
Can anyone help me in displaying the contents of output file XXX.l in the GUI itself???
For simple programs that just prints their results to stdout, then it's simple: exec returns all standard output of the program. So you just need to read the return value of your exec call:
proc exec_and_print {args} {
.top.txt insert end [exec {*}$args]
}
But remember, exec only returns after the program have exited. For long running programs where you want the output to appear immediately in your text box you can use open. If the first character of the file name passed to open is | then open assumes that the string is a command line to be executed. With open you get an i/o channel that you can continuously read from:
proc long_running_exec {args} {
set chan [open "| $args"]
# disable blocking to prevent read from freezing our UI:
fconfigure $chan -blocking 0
# use fileevent to read $chan only when data is available:
fileevent $chan readable {
.top.text insert end [read $chan]
# remember to clean up after ourselves if the program exits:
if {[eoc $chan]} {
close $chan
}
}
}
The long_running_exec function above returns immediately and uses events to read the output. This allows your GUI to continue functioning instead of freezing while the external program runs. To use it simply do:
button .but -text "run perl" -command { long_running_exec perl run_me }
Additional answer:
If the program generates a file as output and you want to simply display the contents of the file then just read the file:
proc exec_and_print {args} {
exec {*}$args
set f [open output_file]
.top.txt insert end [read $f]
close $f
}
If you know where the file is generated but don't know the exact file name then read the manual for glob on how to get a list of directory contents.
I am trying to read and parse a file line by line, but there is some kind of delimiter at the end of the file that is causing strange behavior.
Here is what the lines of the file I am reading looks like :
20111129 AMEX BHO OTCBB BHODD
20111129 AMEX LCAPA NASDAQ LMCA
The code to read it is straightforward :
my(#line) = <INFO>;
foreach $line(#line) {
chomp( $line );
my #vals = split('\t', $line);
my $date = $vals[0];
my $old_exch = $vals[1];
my $old_symb = $vals[2];
my $new_exch = $vals[3];
my $new_symb = $vals[4];
print "0> date '$date'\n";
print "1> old Exch '$old_exch'\n";
print "2> old symb '$old_symb'\n";
print "3> new Exch '$new_exch'\n";
print "4> new symb '$new_symb'\n";
The output appears like this :
0> date '20111129'
1> old Exch 'AMEX'
2> old symb 'BHO'
3> new Exch 'OTCBB'
'> new symb 'BHODD
so there appears to be a character at the end of each line that is causing the trailing ' to print at the beginning of the line, wiping out the 4 that should print there. it is like a character that resets where printing should be occurring back to the begining of the line. Is there any way to 'chomp out' this rogue character? or perhaps there is some kind of bug in my code, but I have other scripts doing something similar...
Thanks much In Advance!
Don
The file has Windows line endings. The rogue character is "\r", you can remove it by a regular expression:
s/\r//;
Or, you can specify the :crlf layer when opening the file.
I have a excel document that I am editing with perl OLE automation and I have run into a problem. I need to enter an empty row in between two rows that already contain data, kind of like appending it to the file but not at the end of the file. I don't want to have to rewrite the entire file using perl either. How would I go about doing this?
Thanks
One thing that always helps me when I am automating Excel with Perl & Win32::OLE is to create a macro in Excel capturing exactly what I am trying to accomplish first. From there I can view the VB code and usually convert that to Perl relatively easily.
For example, in an open Excel 2010 file:
View -> Macros -> Record New Macro
Click OK
Right click somewhere on the sheet to insert a new row
View -> Macros -> Stop Recording
View -> Macros -> View Macros, click edit
From this, I could write code, for example:
#!c:/perl/bin/perl.exe
use strict;
use warnings;
use Win32::OLE;
my $excel = Win32::OLE->new( 'Excel.Application' )
or die "Could Not Start Excel.\n";
$excel->{ 'Visible' } = 1;
$excel->{ DisplayAlerts } = 0;
$excel->{ SheetsInNewWorkBook } = 1;
my $workbook = $excel->Workbooks->Add;
my $sheet = $workbook->Sheets( 1 );
$sheet->Range( 'A1' )->{ Value } = 'Data 1';
$sheet->Range( 'A2' )->{ Value } = 'Data 2';
$sheet->Range( '2:2' )->Select(); #Select Entire 2nd Row
$sheet->Range( '2:2' )->Insert( {
'Shift' => -4121, #xlDown
'CopyOrigin' => 0, #xlFormatFromLeftOrAbove
} );
__END__
Also, here is a list of a bunch of the Excel enumerations: http://www.datapigtechnologies.com/downloads/Excel_Enumerations.txt
Hope this helps!
I have a string containing lots of text with white-spaces like:
String str = "abc xyz def";
I am now passing this string as a command line argument to a perl file using C# as in:
Process p = new Process();
p.StartInfo.FileName = "c:\\perl\\bin\\perl.exe";
p.StartInfo.Arguments = "c:\\root\\run_cmd.pl " + str + " " + text_file;
In the run_cmd.pl file, I have the follwing:
open FILE, ">$ARGV[1]" or die "Failed opening file";
print FILE $ARGV[0];
close FILE;
On printing, I am able to copy only part of the string i.e. "abc" into text_file since Perl interprets it as a single argument.
My question is, is it possible for me to copy the entire string into the text file including the white spaces?
If you want a white space separated argument treated as a single argument, with most programs, you need to surround it with " "
e.g run_cmd.pl "abc xyz def" filename
Try
p.StartInfo.Arguments = "c:\\root\\run_cmd.pl \"" + str + "\" " + text_file;
Side note:
I don't know about windows, but in Linux there's a number of arguments and maximum length of one argument limit so you might want to consider passing the string some other way, reading it from a tmp file for example.
It's a little bit of a hack, but
$ARGV[$#ARGV]
would be the last item in #ARGV, and
#ARGV[0 .. ($#ARGV - 1)]
would be everything before that.
It's not perl -- it's your shell. You need to put quotes around the arguments:
p.StartInfo.Arguments = "c:\\root\\run_cmd.pl '" + str + "' " + text_file;
If text_file comes from user input, you'll likely want to quote that, too.
(You'll also need to escape any existing quotes in str or text_file; I'm not sure what the proper way to escape a quote in Windows is)
#meidwar said: "you might want to consider passing the string some other way, reading it from a tmp file for example"
I'll suggest you look into a piped-open. See http://search.cpan.org/~jhi/perl-5.8.0/pod/perlopentut.pod#Pipe_Opens and http://perldoc.perl.org/perlipc.html#Using-open()-for-IPC
These let you send as much data as your called code can handle and are not subject to limitations of the OS's command-line.