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

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.

Related

Using strings for network path, not mapped drive, with Shell in VBA

QUESTION UPDATED, PLEASE SEE BELOW
I am trying to use string variables (exedir - the full network drive directory of an exe file, and inputdir - the location of the input file argument) to launch an exe with its argument using shell in VBA.
The commented out line in the script below does not work. If I manually set the drive using a letter I can get it to work, as shown below, but the .exe and .lam input file used here are both on networked drives (the variable comp is the unique name of the users PC, which sets the name of the .lam input file, so the input file name is different for every user).
I'm not too familiar with the syntax and borrowed it from elsewhere. Am I missing a quotation mark or maybe have too many in the commented out row?
Sub CallExeWithInput()
Set wsh = VBA.CreateObject("WScript.Shell")
'Statusnum = wsh.Run(Command:="""" & exedir & """""""" & inputdir & """""", windowstyle:=1, waitonreturn:=False)
SetCurrentDirectory "M:\path\"
Statusnum = wsh.Run(Command:="foo.exe ""M:\path\" & comp & ".lam""", windowstyle:=1, waitonreturn:=False)
End Sub
Example of exedir: \\network\path\foo.exe
Example of inputdir: \\network\path\compname.lam
Example of comp: compname << found using Environ$("computername")
I'm aware of some previous questions, having read many to get this far.
I don't want to provide the letter of the drive, and ideally want to launch the .exe with the input file using string variables to input everything. One major reason I want to only use strings, is that they can be controlled by a single variable, and when the directory changes (say with an upgrade of the .exe) it will be easy to update this script.
UPDATE
Following the comments kindly provided below I arrived here:
SetCurrentDirectory fldr
Statusnum = wsh.Run(Command:="foo.exe " & quote & inputdir & quote, windowstyle:=1, waitonreturn:=False)
Where quote = chr(34) and fldr = \\network\path\
Interestingly, if inputdir is defined as a path to a lettered drive (inputdir = M:\etc), this works. If it is to a network drive path (inputdir = \\network\etc), it launches but the .exe immediately crashes with an invalid floating point operation.
How can I alter this so the input file can be provided as a network path?
If this is not possible I will presumably need a subroutine that locates which letter the user has mapped the network drive onto in order to build my string, such as this:
path = ":\foo\bar\"
dim x as integer
for x = 65 to 90
If CBool(Len(Dir(Chr(x) & path))) Then
msgbox "It's in drive " & Chr(x)
exit for
end if
next x

VB: Highlight Excel cell in worksheet

I was wondering if it was possible to create a vbs file to highlight a cell in an excel workbook.
I have an excel worksheet with multiple computer host-names, I also run a script in batch that pings each host-name in a text document. I want to call the vbs file to highlight the cell in excel if the ping result was successful. Is this possible?
Thanks!
Dimitri
There are plenty of ways to do this, but I must ask why you're using a batch script and a text file to ping the hostnames when you can do that right in Excel?
There are two ways to do this... one is a bit more complex and correct and the other is quick and dirty. Frankly, I recommend the quick and dirty.
Correct Way:
Declare the ReadConsole & WriteConsole methods from your Windows kernel32.dll and utilize them to get the results of your ping. It's described well here:
http://visualbasic.about.com/od/learnvb6/l/bldykvb6dosa.htm
Q&D Way:
Use the built-in Shell() function in VBA and pipe the output of the ping to a text file. Parse said text file and delete it when you're done.
e.g.
for each currCell in hostnameRange
' Ping each hostname and pipe the results to a file
shell "ping " + currCell.value + " >> ping_result.txt"
next currCell
inFile = FreeFile()
Open "ping_result.txt" for Input as #inFile
fileBuffer = Input$(LOF(inFile ), inFile) ' Open and read the file to a buffer
for each currCell in hostnameRange
' Search for ping failures in the buffer
if instr(1, fileBuffer, "could not find host " + currCell.value) = 0 then
debug.print "Ping successful."
end if
next currCell

Retrieving last line of a txt file with a VBscript

Before all, I want to say that I am not a programmer, so this may be basic for some people but surely not for me!!
The task that I want to accomplish is to retrieve some characters of a data file that is imported automatically from a server.
Data is stored in lines in a CSV or tabbed .txt file, each line consists of date and some numeric values. The format is always the same, only the file grows in one line each time a new value is entered.
What I need the script to do, is open that file (wich adress is known and constant) search for the last line, and then extract a string from that line and write it on a different .TXT file, from where I can import it to another specific software as a raw value.
The part in the middle (extracting string) is fairly simple, but opening and isolating the last line is far too much for me.
Thanks everybody for helping!
dim path
path = "fileName.txt"
otherOption(path)
function otherOption(fileName)
const read = 1
dim arrFileLines()
set objArgs = CreateObject("scripting.FileSystemObject")
if objArgs.FileExists(fileName) then
set objFile = objArgs.OpenTextFile(fileName,read)
i=0
do until objFile.AtEndOfStream
redim preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
loop
objFile.Close
end if
wscript.Echo arrFileLines(i-1)
end function

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

access command line data in Excel VBA fails

I need to access the command line parameters in an Excel VBA macro, and found many variations but only one that worked with Excel-2010, seems that the API has changed over time. I tried this code I found "out there":
Dim pCmdLine As Long ' Pointer to the Comand-line string
Dim strCmdLine As String ' Command line string
Dim CmdLine As String ' Command line string
' Get the pointer to the command line string
pCmdLine = GetCommandLineA
' Fill the string with zeros
' (300 characters for command line seems to be enough)
strCmdLine = String$(300, vbNullChar)
' Copy from the pointer to VBA-style string
lstrcpynA strCmdLine, pCmdLine, Len(strCmdLine)
' At this point we got the string,
' now skip the rest of it filled with 0 characters.
CmdLine = Left(strCmdLine, InStr(1, strCmdLine, vbNullChar) - 1)
MsgBox "Length of the command line = " & Len(CmdLine) '' Debug
MsgBox "Command Line:: " & CmdLine '' Debug
Which I put into the Auto_open macro of a spreadsheet. If I try this call:
start excel TestMacro.xlsm /e/abcd/xyz
It seems to generally work and the macro reports:
Command line = " C:/.../excel.exe TestMacro.xlsm"
So I get the invocation part, but the parameters are lost.
Partial Solution:
I find that if I change the invocation to:
start excel /e/abcd/xyz TestMacro.xlsm
It works, except the parsing code has to be changed to ignore the file name which is not at the end, and also this form doesn't seem to allow any blanks in any parameter, even if quoted. The system seems to interpret them as file names for a target excel file and give an error. For example:
start excel /e/abc/"my sheet"/ TestMacro.xlsm
gives the error:
file 'sheet"/.xlsx' not found
Although after the spurious startup error, the intended sheet does open and get teh entire line to work with.

Resources