How do I make this batch file?
September 15, 2010 11:43 AM   Subscribe

Batch file hell. I'm trying to write a batch file to...... three commands and output to a text file. Specifically, I want to run "ping" (ping an IP that has to be input by the user), then tracert (same IP), then "pathping" (same IP). This would have to be dumped out to a text file on the user's drive.

I've tried a number of variations cobbled together from my own feeble knowledge of batch files and what I've found on the Googles but can't seem to get it to work. The user imput part is where I'm stumbling.

Any ideas or samples? Thanks!
posted by KevinSkomsvold to Computers & Internet (8 answers total) 6 users marked this as a favorite
Best answer: Try:

@echo off
ping %1
tracert %1
pathping %1

...and run as "name.bat x.x.x.x".

Not sure about the file redirect bit.
posted by jquinby at 11:49 AM on September 15, 2010

Best answer: Ah, this will do it. Results are saved in 'output.txt'.

@echo off
if exist output.txt del output.txt
ping %1 >> output.txt
tracert %1 >> output.txt
pathping %1 >> output.txt

Every 10 years or so, my copy of "Concise Guide to MS-DOS Batch Files" comes in handy.
posted by jquinby at 11:54 AM on September 15, 2010

Best answer: You don't need to delete output.txt, if you use redirection operator instead of the append operator for the first command.

Here, this will let you pass in an IP and a file as two parameters, but will prompt the user for the values if they leave them out (like if they double click on it from explorer or something).
@echo off
goto :main

  set /P "ip=Enter an IP address: "
goto :eof

  set /P "output=Enter file name for results: "
goto :eof


    set ip=%1
    set output=%2

    if "%ip%" == "" call :get_ip
    if "%output%" == "" call :get_output
    echo Pinging ip %ip%
    ping "%ip%" > "%output%"
    echo Running tracert...
    tracert "%ip%" >> "%output%"
    echo Running pathping...
    pathping "%ip%" >> "%output%"
    echo Finished.  See %output% for results.

goto :eof

posted by aubilenon at 12:16 PM on September 15, 2010 [1 favorite]

To add to jquinby's batch, I would fully qualify the location of output.txt.

if exist c:\output.txt del c:\output.txt
ping %1 > c:\output.txt

that way if your batch file resides somewhere in your path (meaning they can run it regardless of what directory they're currently in) then you don't end up with output.txts all over the place.
posted by komara at 12:22 PM on September 15, 2010 [1 favorite]

Response by poster: Ok. Wow. These examples rock my world and are actually simpler than what I had. I seem to have an innate ability to complicate the hell out things. I really appreciate the expertise. This will help me a LOT. Seriously, you guys rock.
posted by KevinSkomsvold at 12:54 PM on September 15, 2010

It's worth noting that in the batch file, %1 represents the first command line argument after the script itself (which is represented as %0). You could pass multiple options after the script, along the lines of:

foo.bat A B

...and later referencing them as %1 and %2 respectively, to wit:

@echo off
if exist c:\%2 del c:\%2
ping %1 >> c:\%2
tracert %1 >> c:\%2
pathping %1 >> c:\%2

...invocation would be:

foo.bat x.x.x.x logfile.txt
posted by jquinby at 1:20 PM on September 15, 2010

Best answer: For giggles, you can add a:
date /t
after the @echo off, just to add some historical record in case you need to see when the last time you did this thing was.
posted by komara at 2:35 PM on September 15, 2010

Best answer: Sorry:
date /t >> c:\output.txt
posted by komara at 2:35 PM on September 15, 2010

« Older Lugging luggage in Tokyo   |   How can I buy a pair of shoes only sold in the UK? Newer »
This thread is closed to new comments.