+ Reply to Thread
Results 1 to 5 of 5

Output redirection from BAT or VBS to text file.

  1. #1
    Registered User
    Join Date
    07-26-2011
    Location
    Richmond, Virginia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Output redirection from BAT or VBS to text file.

    Hello again forum,

    I am working on a VBA routine that calc and verifies hash files for a series of folders and sub directories, using a command line prog called exactfile.exe (exactfile.com). It is very similar to M$'s FCIV utility, but it is actually supported on Win7/Win8. The issue I am having is that I cannot get the standard output/error/console stream to redirect to a text file. I've written the routine multiple ways, trying to figure out redirection. I was using this site
    HTML Code: 
    as a reference.

    Here is my VBS approach. This code is dependent on another sub passing folder_name as a string

    Please Login or Register  to view this content.
    The VBS then executes using wscript, however, since exf.exe does not return an exit code the same way FCIV.exe does, I need the results redirected to a text file. The results vary from a single line saying "No errors." or multiple lines indicating which files do not match their original hash.

    Here are a couple iterations of my BAT approach, where i use the BAT to call the VBS. I really thought I would be able to use the standard redirect > here, since the executable is launched using cmd.exe, but I've not gotten the results I want.

    Please Login or Register  to view this content.
    I've scoured more pages than I can count, but no approach seems to work, so far. I would appreciate a second or third set of eyes looking at this with me

    Thanks!

    WH

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Output redirection from BAT or VBS to text file.

    Hi wherbjr35,

    I struggled with the same problem for a long time. I have been successful obtaining the output of 'stdout' and have ignored 'stderr' output (because I didn't need 'stderr' output).

    The following seems to be an excellent reference: http://stackoverflow.com/questions/1...d-msaccess-vba

    Post #18 from the following shows how to know when the command has completed using API calls: http://www.mrexcel.com/forum/excel-q...cations-2.html

    I have been successsful using the following syntax taken out of context (courtesy of apo) - post #10 in the following link: http://www.excelforum.com/excel-prog...ml#post3637460
    Please Login or Register  to view this content.
    Array x() contains the contents of stdout. It is fast and seems to work well, but has the unwanted SIDE EFFECT that makes the cmd.exe Window flash while it is executing which can become annoying. I have seen some code on the Internet that uses .run (and is an order of magnitude more complicated) instead of .exec and may not cause the flashing side effect, however, I lost the link and do not have the code.




    Working example (found 38 matches on my Vista 32 bit system):
    Please Login or Register  to view this content.

    I've also developed and used the following construction successfully (when I was younger and stupider a few months ago). It is a lot more complicated and works as follows:
    a. Create a .bat file.
    b. Delete all temporary output files (that always use the same names).
    c. Run the .bat file [Call Shell(sRunString, vbHide) ].
    d. The .bat file redirects stdout to a temporary output file.
    e. Create a SECOND temporary file at the end of the .bat file.
    f. The .bat file has completed when the SECOND temporary file exists.
    g. The first temporary file now contains the contents of stdout which can be processed.

    The following code is NON-FUNCTIONAL and taken out of context for illustration purposes only. For working code
    see the file in post #2 of http://www.excelforum.com/excel-prog...is-lookup.html
    That post also has references to a similar application that runs Tracert, Ping, or Nslookup.
    Please Login or Register  to view this content.
    Lewis

  3. #3
    Registered User
    Join Date
    07-26-2011
    Location
    Richmond, Virginia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Output redirection from BAT or VBS to text file.

    Lewis--- MAN, you have some great stuff here. I've been on another project, but have only been thinking about getting this one finished. I started with the first part of your post #2, and from that (thanks by the way for posting your source as reference --very crucial for me) tried this code.

    Please Login or Register  to view this content.
    I tried all three sArray lines, and each completes, but the txt file does not update. The interesting part is that the txt file time stamp updates to coincide with the script execution, so it appears that output is being written, but its not. Any other ideas here?

    I am hoping to try the batch & scratch file approach next, once i rule this approach out.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Output redirection from BAT or VBS to text file.

    I'm no expert on this stuff. I just did a lot of research.

    From the following post it appears that the .run approach will be unsuccessful:http://www.vbaexpress.com/forum/show...rom-within-VBA

    Lewis

  5. #5
    Registered User
    Join Date
    07-26-2011
    Location
    Richmond, Virginia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Output redirection from BAT or VBS to text file.

    I'm back with a solution that i wanted to share--the suggestions where excellent VBA examples, but did not work out as I expected, so of course I kept digging and prodding. Eventually came to a stackoverflow link here and modded it to suit my needs. The code now captures stdout and redirects it to a text file of my choosing. This overcame my dilemma of having to shell an executable that did not return an exitcode. Now, i just read the text file and perform logic based on the results (logic performed on stdout not shown below)


    Brief Description: Launchshell accepts 2 parameters (the first is the full execution path of your program, including any arguments, and in my case, the second parameters was a folder name being passed from another function. The .exec method is used to launch the command, then HideWindow minimizes the command window --The OP on stackoverflow indicated no luck getting a full hidden window, and i couldn't improve on this either. Lastly, I added on a small function (CreateResultsFolder) to check for an output folder, or create one if need be.


    'usage

    Please Login or Register  to view this content.
    'Copy from here down and paste into a module
    Please Login or Register  to view this content.
    As always, i am grateful for the input and replies of current and former posters...these forums are invaluable resources. Lastly, I am open to any feedback on how to improve this code. Its 100% working.(well, 99.9 since the cmd window minimizes to taskbar instead of being fully hidden)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formatting number output to a text file
    By aeroman01 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-30-2012, 01:22 PM
  2. Need help to get rid of unwanted " in output text file
    By AdamHasslert in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-10-2008, 05:40 AM
  3. Output of Highlighted Rows to Text File
    By KenB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2008, 10:45 PM
  4. [SOLVED] Open CSV file, format data and write output to a text file.
    By BristolBloos in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2005, 11:05 AM
  5. A macro for output an excel file to text file
    By cmk_excel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-31-2005, 04:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1