AutoIT getting text with _Stringbeetwen and saving it to .txt file - string

so I need to get a text from this file in brackets
Lance Hill (born February 17, 1972) is a retired U.S. soccer forward. He spent one season in USISL.
And save it to the Output.txt
Local $fArray
If NOT FileExists("C:\Users\fiwi\Desktop\Input.txt") Then
MsgBox(0, "Error", "Unable to open Exclusion File" & #CRLF & "It appears that the file does not exist.")
Exit
Else
_FileReadToArray("C:\Users\fiwi\Desktop\update.txt", $fArray)
EndIf
local $check = _StringBetween( $fArray, "Comments" , "PDF" )
FileWrite("Output.txt", $check)
Mine script gives me a "0" in a output file.

_StringBetween
_StringBetween has strings as parameters and return an array.
This should work:
$fileContent = FileRead($file)
$array = _StringBetween($fileContent, "Comments" , "PDF")
For $i = 0 To Ubound($array)-1
FileWriteLine("C:\Output.txt", $array[$i])
Next
And you are checking one file, and reading another.
If NOT FileExists("C:\Users\fiwi\Desktop\Input.txt") Then
_FileReadToArray("C:\Users\fiwi\Desktop\update.txt", $fArray)

Related

Write to the next empty line in an existing csv file

I am writing a script wherein rather than writing over the existing data, I would like to write to the next empty line in the csv file.
Current Output
texta,2
textb,
filed1,field2,field3
1,abc,123
2,xyz,124
Expected Output
texta,2
textb,
filed1,field2,field3
1,abc,123
2,xyz,124
1,jkl,547
The current code which writes over the existing data in the file
#! /bin/bash
echo "Enter file name"
read name
if [ condition ]
then
#commands
else
echo "Enter alphabets"
read alpha
echo "Enter number"
read number
echo "1,$alpha,$number" >> ${name}.csv
fi
Example input here aplha = jkl and number = 547
This will write over the entire data but I would like to write only to the next empty line each time the if condition fails. Any help is appreciated.

Parsing Excel of ASCII format in Perl

We have perl script whose job is to read an excel file and convert it into a flat file. We get excel file from some other system on a shared location.
The other system is generating a flat file dumping data in the file seperated by a tab and appending with .xls. Now the problem with that is since in xls file if there is a string with leading 0 e.g 012345 it will be displayed as 12345 in excel. To preseve the leading 0 what they do is they write the data in this fashion (in java)
"=\"" + some string + "\""
Now if we open the file in excel it is proper with no = or ", but when reading via perl it read the string as it is i.e. ="some string".
How can we work around this, i have tried a solution to trim the leading =" and " but do not feel it to be clen one. Can someont suggest anything else
My suggestion (with the limited information you've given) would be to read the source file using Text::CSV, and then output via whatever means you would otherwise. For substitution, use regular expressions.
Simplified example (partially taken straight from the documentation):
#!usr/bin/perl
use Text::CSV;
# set binary attribute and use a tab as a separator:
my $csv = Text::CSV->new ( { binary => 1, sep_char => "\t"} )
or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", "test.xls" or die "test.xls: $!";
my #rows;
while ( my $row = $csv->getline( $fh ) ) {
foreach my $column (#$row) {
$column =~ s/^="|"$//g; # remove opening '="' and closing '"'
}
push #rows, $row;
}
$csv->eof or $csv->error_diag();
close $fh;
# do file writing magic or further processing here
In case you don't know, the 's' at the beginning of the regular expression indicates you want to substitue and the 'g' at the end means "repeat for all matches".
For more information, see:
https://metacpan.org/pod/Text::CSV
http://perldoc.perl.org/perlretut.html (Perl documentation on regular expressions)

BATCH - error in XLSX sheet when importing %time%

I have a little issue when my batch writes the time before 10 AM.
What it does is scan the barcode from a user card and writes date, time and that code into a .csv file, so that it can be imported into a excel file.
here is my code:
#echo off
...
:: set /p fname="Digite o nome do arquivo a guardar os logs: "
set fname=refeicao
goto REF
:REF
....
set cod=""
set /p cod="-> "
if /i %cod%=="" goto err
goto ok
:print
echo %date% %time% %cod%>>w:\REFEICAO\%fname%.csv
goto REF
:err
....
goto REF
:OK
...
goto print
:qq
exit
The problem is that when time is earlier than 10AM, it exports with a space before, let's suppose it's 4 AM. It would be like: " 4:00:00,00" instead of "04:00:00,00"
How could i get rid of that space or turn it into a "0"?
[Actual code inserted - minus irrelevant echos etc.]
echo %time: =0%
echo %time: =%
The first line changes spaces to 0, the second one removes spaces. Use the one better fits your problem

Check user input against words in text file

I am working on an option for my program and it should work like this:
user inputs title
user inputs author
system then checks user's title & author input in the text file named BookDB.txt
if there is already existing record in the text file, system will prompt an error
else it will continue user to input price, quantity available and quantity sold.
book will then be added
I tried playing around with grep but to no avail.
Below are my codes for this particular function.
function fnAddBook()
{
echo "Title: "
read inputTitle
echo "Author: "
read inputAuthor
if grep -Fq "$inputTitle" BookDB.txt; then
if grep -Fq "$inputAuthor" BookDB.txt; then
echo "Error!"
fi
else
echo "Price: "
read inputPrice
echo "$inputTitle:$inputAuthor:$inputPrice" >> BookDB.txt
echo "New Book successfully added!"
fi
}
contents of BookDB.txt
format of the contents | Title:Author:Price:QtyAvail:QtySold
Hello World:Andre:10.50:10:5
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
UPDATED PROBLEM:
In this case, even if I typed "Catch Me If You Can" as title + "Ubuntu Team" as Author, it raises the error. How can I modify the codes such that it checks line by line?
Thanks in advance to those who helped! :)
There are 3 problems with your code.
The first is that the x option to grep causes it to match only complete lines, and since you put author and title on the same line, this will not match.
With the x option "Gaiman" does not match "Gaiman:Nation:$20", if you remove the x from the grep-options, this will work.
The second problem is that the two greps are independent of eachother. Thus if you have a book titled 'Nation' and a book by 'Gaiman' it will be considered a match, even if the 'Nation' book you have is 'The wealth of Nations' and the Gaiman book you've got is 'Anansi Boys'.
The third problem is that grep will find partial matches. If you try to enter the book "It", then grep will conclude it's already in the database, because "It came from the desert" is.
You need a sentinel-value to delineate the titles to fix this. (the sentinel must be some character that cannot exist in book-titles or author-names)
function fnAddBook()
{
echo "Title: "
read inputTitle
echo "Author: "
read inputAuthor
if grep -Fq "$inputTitle:inputAuthor:" BookDB.txt
then
echo "Error!"
else
echo "Price: "
read inputPrice
echo "$inputTitle:$inputAuthor:$inputPrice" >> BookDB.txt
echo "New Book successfully added!"
fi
}
This assumes that ':' cannot occur in authornames or booknames.

Batch Rename Files - Append Lines 1 & 3

I'm using Windows 7. I have a bunch of text files, each containing one email message. Each starts this way:
FROM: Person
TO: Another Person
DATE: 01-Jan-11 at 18:12:00
SUBJECT: Whatever
I want to rename these files so that their names look like this:
2011-01-01 18.12 Email from Person to Another Person re Whatever.txt
Batch programming is all I know, and I don't know it very well. For purposes of restraining this to a project that I can understand quickly, I think my best solution will be to extract the essential data into a text file that I can then massage into a batch renaming file.
In that case, what I'm looking for is a batch file that will extract the data into single lines in a text file that I can then massage into shape with global edits. In other words, I think I'm looking for text lines in this format:
[current filename] [extracted date and time string] [from] [to] [subject]
Example:
file01.txt 01-Jan-11 at 18:12:00 from Person to Another Person re Whatever
If I've got lines like that, I can parse them into renaming commands pretty quickly in Excel.
Thanks!
Given that your using Windows 7, I thought I'd suggest an alternative. Windows Powershell is a a very useful command tool that can be used for a ton of stuff. I think I solved your complete problem:
$folder = "C:\..."
$regex = "FROM: (.*) TO: (.*) DATE: (.*) at (.*) SUBJECT: (.*)"
$files = Get-ChildItem $folder *.txt
ForEach ($file in $files) {
$line = (Get-Content $file.FullName -TotalCount 1)
$match = ([regex]$regex).matches($line)[0]
$date = [DateTime]($match.Groups[3]).Value + [TimeSpan]($match.Groups[4]).Value
$from = ($match.Groups[1])
$to = ($match.Groups[2])
$subject = ($match.Groups[5])
# You can change the naming format in the brackets below
Rename-Item $file.FullName -NewName ( $date.ToString("yyyy-MM-dd_HH-mm-ss") + " Email From " + $from + " to " + $to + " RE " + $subject)
}
It makes a few assumptions (like a match will always be found). You can easily adjust naming format and other things. Save this code as a script (.ps1) and run it in the Powershell prompt (powershell.exe)

Resources