Trying to scrub a file with AutoIt - search

I work with a piece of software that outputs a sql script during upgrades. I am trying to make a tool that will read the file and append " ON IndexDataFile" to the lines that contain "CREATE INDEX" or "CREATE UNIQUE INDEX"
I open that file and write each line to a new file using FileOpen and FileReadLine. Despite a good deal of searching via Google and specific forums I have not been able to figure out how to search each line and append to it when one of the two values are found.
Here is the loop I am using to write to the new file:
While 1
$line_read = FileReadLine($handle_read)
If #error Then ExitLoop
;Code to evaluate line and append text when true
FileWriteLine($handle_write, $line_read)
WEnd
Can someone give me a hint on how to test the string and append when needed or is there a better approach to this?

In one line it would be as easy as:
$line_read = StringRegExpReplace($line_read, "(CREATE\ (?:UNIQUE\ )?INDEX)", "\1 ON IndexData")
You can test your RegEx on regex101.com to make sure, it works as expected.
Or you can write your expression differently if you prefer it fo a better way of reading with an "or" symbol |:
$line_read = StringRegExpReplace($line_read, "(CREATE\ INDEX|CREATE\ UNIQUE\ INDEX)", "\1 ON IndexData")

I was able to resolve it with the following:
While 1
$line_read = FileReadLine($handle_read)
If #error Then ExitLoop
If StringRegExp ( $line_read, "CREATE INDEX") Then $line_read = $line_read & " ON IndexData"
If StringRegExp ( $line_read, "CREATE UNIQUE INDEX") Then $line_read = $line_read & " ON IndexData"
FileWriteLine($handle_write, $line_read)
WEnd

Related

Search txt and csv files for string using Excel VBA without opening files

I have a text file that is automatically generated from a machine. The machine writes the txt file in "chunks" (sorry I don't know the exact terminology). I need to pull data from this txt file, but I need the txt file to be finished before pulling data from it. I found a solution to verify that the machine has finished writing to the file... It is not as elegant as i had hoped, but seems to do the trick. Excel VBA opens a command prompt, the command prompt uses a Find command to find the string "End of Report"... This is basically one of the last lines of the txt file and pretty safe to assume the txt file is finished after this is found. This code runs in a loop 1000 times, every 10 seconds, until it finds this string or reaches 1000 tries...
The issue is that "result" returns some other characters besides just "End of Report" this is further complicated by the fact that I am attempting to run this on some csv files too... and "result" returns some additional characters also, but different from the ones returned from the txt files. For example, if I check the length of "result"... The length comes back as 43 on one file and 48 on another file... I think it is counting the file path + "End of Report" + a few more characters?
Anyways, I don't really need the "result"... I really only need a "true" / "false" if "Find" found "End of Report" or not... How can I accomplish this? Is there a different better way to do this? I am not familiar with command prompt programming.
Note: It is important that I search these files without opening them.
Sub test()
Dim SearchStr As String
Dim cmdLine As Object
Dim result As String
Dim FilePath As String
FilePath = "D:\test2.txt"
SearchStr = """End of Report"""
Set cmdLine = CreateObject("WScript.Shell")
result = cmdLine.Exec("%comspec% /C Find " & SearchStr & " " & Chr(34) & FilePath & Chr(34)).STDOut.ReadAll
Debug.Print (result)
End Sub
I am not really an expert in command line, but what I would do is export the result of the FIND command to a file, like here
Then I would check in your VBA code how many rows are in the file (either clean the file before, or check the number of rows before the export is done).
If the number of rows meets the criteria (probably 2 or more rows instead of 1), then you can set the flag to True.

Extracting Data with .selectitem VBA-SAP

I am trying to extract the text from a topdown analysis in SAP using VBA. I basically need to move down the column, evaluate if the extracted text contains a certain character string, then extract the cell adjacent to it.
First: Does anyone know how to change this line of code (the script for that specific cell) so that I can extract its text to a variable rather than just select it?
Second: Does anyone know how to make this line variable using i as the variable?
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]").selectItem " 5", "C 35"
In similar cases, I proceeded as follows:
myRow = right(space(10) & cstr(i) , 11)
myText = session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]").getItemText(myRow ,"C 35")
Regards,
ScriptMan

How to save manipulated WAV files in object list?

I have the following problem: I want to low-pass filter 240 WAV files. The script is running only till the low-pass filtered sounds are created and shown in the object list ("..._band"). However, Praat does not export them as WAV files. After choosing the output folder, I get the warning message "Command 'Get number of strings' not available for current selection".
In short, my question is how can I save the WAV sounds in the object list individually with their new file names? See also Screenshot.
Script see below.
Thank you very much for your help!
Greetings,
#determine praat version
ver1$ = left$(praatVersion$, (rindex(praatVersion$, ".")-1));
ver1 = 'ver1$'
if ver1 < 5.2
exit Please download a more recent version of Praat
endif
if ver1 == 5.2
ver2$ = right$(praatVersion$, length(praatVersion$) - (rindex(praatVersion$, ".")));
ver2 = 'ver2$'
if ver2 < 4
exit Please download a more recent version of Praat (minor)
endif
endif
beginPause ("Low-Pass Filter Instructions")
comment ("1. Select a folder containing the wave files to be low-pass filtered")
comment ("2. Wave files will be low-pass filtered (0 - 400 Hz)")
comment ("3. Select an output folder for the low-pass filtered wave files to be saved to")
comment ("Click 'Next' to begin")
clicked = endPause("Next", 1);
#wavefile folder path
sourceDir$ = chooseDirectory$ ("Select folder containing wave files")
if sourceDir$ == ""
exit Script exited. You did not select a folder.
else
sourceDir$ = sourceDir$ + "/";
endif
Create Strings as file list... list 'sourceDir$'/*.wav
numberOfFiles = Get number of strings
levels$ = ""
for ifile to numberOfFiles
select Strings list
currentList = selected ("Strings")
filename$ = Get string... ifile
Read from file... 'sourceDir$'/'filename$'
currentSound = selected ("Sound")
filterFreqRange = Filter (pass Hann band)... 0 400 20
select currentSound
Remove
endfor
select currentList
Remove
#output folder path - where the wave files get saved
outputDir$ = chooseDirectory$ ("Select folder to save wave files")
if outputDir$ == ""
exit Script exited. You did not select a folder.
else
outputDir$ = outputDir$ + "/";
endif
numberOfFiles = Get number of strings
for ifile to numberOfFiles
select Strings list
currentList = selected ("Strings")
filename$ = Get string... ifile
currentSound = selected ("Sound")
endif
Save as WAV file... 'outputDir$'/'filename$'
select currentSound
Remove
endfor
#clean up
select currentList
Remove
#clear the info window
clearinfo
#print success message
printline Successfully low-pass filtered 'numberOfFiles' wave files.
At a glance, the command is not available because when you get to that point in the script the selection is empty. It is empty because at the end of the first loop, you select your Strings object and then Remove it.
More generally, your script is not handling the object selection properly, which is a big problem in Praat because available commands change depending on the active selection.
So even if you remove the line where you Remove the list, you will bump into a problem the second time you run Get number of strings because by then you have changed the selection. And even if you remove that line (which you don't really need), you'll still bump into a problem when you run selected("Sound") after selecting the Strings object, because by then you won't have any selected Sound objects (you didn't have them before anyway).
A more idiomatic version of your script, which also runs on a single loop where sounds are read, filtered, and removed one by one (which is also more memory efficient) would look like this:
form Low-Pass Filter...
real From_frequency_(Hz) 0
real To_frequency_(Hz) 400
real Smoothing_(Hz) 20
comment Leave paths empty for GUI selectors
sentence Source_dir
sentence Output_dir
endform
if praatVersion < 5204
exitScript: "Please download a more recent version of Praat"
endif
if source_dir$ == ""
source_dir$ = chooseDirectory$("Select folder containing wave files")
if source_dir$ == ""
exit
endif
endif
if output_dir$ == ""
output_dir$ = chooseDirectory$("Select folder to save filtered files")
if output_dir$ == ""
exit
endif
endif
list = Create Strings as file list: "list", source_dir$ + "/*.wav"
numberOfFiles = Get number of strings
levels$ = ""
for i to numberOfFiles
selectObject: list
filename$ = Get string: i
sound = Read from file: source_dir$ + "/" + filename$
filtered = Filter (pass Hann band): from_frequency, to_frequency, smoothing
Save as WAV file: output_dir$ + "/" + selected$("Sound")
removeObject: sound, filtered
endfor
Although you might be interested to know that, if you do not need the automatic file reading and saving (ie, if you are OK with having your script work on objects from the list) your script could be reduced to
Filter (pass Hann band): 0, 400, 20
which works on multiple selected Sound objects.
If you absolutely need the looping (ie, if you'll be working on really large sets of files) you might also be interested in using the vieweach plugin, with was written to try and minimise this sort of boilerplate (full disclaimer: I wrote the plugin). I wrote a longer blog post about it as well.

AppleScript to read strings from ".txt" files to variables

I am trying to build an AppleScript that reads each line of a “.txt” file (with a linefeed) and stores the contents of each line into AppleScript variables.
Here is what I mean:
Let’s say there was a “Test.txt” file with the contents:
Apples
Oranges
Pears
As you can see, the “Test.txt” file’s contents have a String on each line, a linefeed introducing the new String, and so on.
I would really like to know how an AppleScript could be made so that each line’s Strings are copied into individual AppleScript variables.
(This way, “Apples” in the first line, would be stored in variableA, “Oranges” in the next would be stored in variableB, “Pears” … variableC, etc.)
Please let me know, from your experience, how best to accomplish this. I know it’s slightly more involved, here is where I am:
(*
This portion of the AppleScript accesses the contents of the ".txt" file named "Test," though takes all of the content and places it into a single variable.
*)
set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to (read file newFile)
{ AppleScript code to read each line to individual variables }
I know there must be others trying to accomplish this.
This example is for a situation where you know the anticipated paragraphs you'll be looking for to assign to each of a known set of variables.
set newFile to ("Macintosh HD:Users:Username:Desktop:Test.txt")
set theFileContents to paragraphs of (read file newFile)
set recipientEmail to paragraph 1 of theFileContents as text
set senderEmail to paragraph 2 of theFileContents as text
set theSubject to paragraph 3 of theFileContents as text
set theBody to (paragraphs 4 thru -1 of theFileContents) as text
Another option would be to dynamically search for a string in the paragraph, and if it matches, then assign it to that variable. Something like:
set newFile to ("Macintosh HD:Users:jweaks:Desktop:horses.txt")
set theFileContents to paragraphs of (read file newFile)
set recipientEmail to ""
set senderEmail to ""
set theSubject to ""
set theBody to ""
repeat with p in theFileContents
if p contains "To:" then
set recipientEmail to p
else if p contains "From:" then
set senderEmail to p
else if p contains "Subject:" then
set theSubject to p
else
set theBody to theBody & p & return
end if
end repeat
Thank you so much for all of your effort to answer this question, jweaks. As I am still catching on to AppleScript best practices, I was thinking more about your recommendation of bringing the contents of the “.txt” file into a list, assigning the items to AppleScript variables (if needed), and began brainstorming how to accomplish it. I agree that it seems like the simplest and most efficient approach:
Reads the “.txt” file’s into a list of items: Apples, Oranges, and Pears
set paragraph_list to read file "Macintosh HD:Users:tombettinger:Desktop:Test.txt" using delimiter linefeed
Apples
set variableA to item 1 of paragraph_list
Oranges
set variableB to item 2 of paragraph_list
Pears
set variableC to item 3 of paragraph_list
Displays the contents of each variable (optional comment)
display dialog variableA & " " & variableB & " " & variableC
End of AppleScript
As long as the contents of the ".txt" file are stacked in a table, this approach will support the accessibility of information I was searching for. Thanks again!

Applescript file search using data from a spreadsheet

I am currently using this Applescript I found that searches for a file name and returns the file path in a text doc. This works fine for finding 1 or 2 files, but I would like to find 500 files that are spread over hundreds of folders. My ideal script would use data from an excel spreadsheet or csv, perform a search, find the file and make a copy of it in a designated folder on my desktop. Any help is appreciated.
Here is the script I found:
tell application "System Events"
activate
set thePattern to text returned of (display dialog "Search for" default answer "")
end tell
if thePattern = "" then return
try
set foundFiles to do shell script "mdfind -name " & quoted form of thePattern & " | /usr/bin/egrep -i " & quoted form of thePattern & "[^/]*/?$ | /usr/bin/grep -vi " & quoted form of thePattern & ".*" & quoted form of thePattern
on error
set foundFiles to "Nothing Returned"
end try
if foundFiles = "" then set foundFiles to "Nothing Returned"
tell application "TextEdit"
activate
delay 0.5
try
set theDoc to document 1
get text of theDoc
if result is not "" then
make new document
set theDoc to result
end if
on error
make new document
set theDoc to result
end try
set text of theDoc to foundFiles
end tell
You need to read the data from the text file, then turn it into a return or linefeed delimited list and do a repeat over the items of this list. Then turn each item (which is actually a line) into e.g. a tab delimited list and again do a (nested) repeat loop over the items of this list. If you know that e.g. item 3 is the file path, you can set a variable to item 3 of the line as text and use this variable in your shell script.
I think you need to show that you understand the concept of repeat loops by posting your own attempt of implementing this. If you do, I'll be happy to come back and help you with the next step.
Kind regards,
Mark

Resources