In batch script, output command in txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

In batch script, output command in txt file

#1 Post by dosbatch » 04 Dec 2016 05:45

Hi everyone!

I have .bat file

and file .vbs

How can I save the output of commands?

Thank you so much! Bye
Last edited by dosbatch on 06 Dec 2016 02:08, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: In batch script, output command in txt file

#2 Post by Squashman » 04 Dec 2016 08:32

Why not use a scriptable telnet client?

dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

Re: In batch script, output command in txt file

#3 Post by dosbatch » 04 Dec 2016 09:23

Thank you for the answer.

Because I think this is the simple way.

Can you explain how I modify the code for resolve my issue?

This script run ok but i need to receive output for one command.

If you think that scriptable telnet client is the best solution, can you explain how do you procede?

Sorry for my english :cry:

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: In batch script, output command in txt file

#4 Post by penpen » 04 Dec 2016 11:57

Maybe it suffices to create a logfile (using option -f LogFile) and use a for/F loop to set the desired output:
https://technet.microsoft.com/de-de/library/cc730934(v=ws.10).aspx.

penpen

dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

Re: In batch script, output command in txt file

#5 Post by dosbatch » 04 Dec 2016 12:17

(for /F "tokens=*" %%A in (router.txt) do (
start telnet.exe %%A
script command.vbs
telnet -f logfile.txt %%A
))

In this case I have all output of telnet command

Tnk
Last edited by dosbatch on 06 Dec 2016 02:12, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: In batch script, output command in txt file

#6 Post by penpen » 05 Dec 2016 03:08

I had rather thought of using the -f option in your (first) telnet command, and parse the result using a for/F loop to extract the needed information:

Code: Select all

(for /F "tokens=*" %%A in (router.txt) do (
start telnet.exe -f "logfile.txt" %%A
cscript command.vbs
for /F "usebackq tokens=* delims=" %%b in ("logfile.txt") do (
   rem:This step depends on the structure of "logfile.txt".
)
del "logfile.txt"
))


penpen

Edit: Added missing 'c' ("cscript").

dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

Re: In batch script, output command in txt file

#7 Post by dosbatch » 05 Dec 2016 03:50

This works fine:

Code: Select all

(for /F "tokens=*" %%A in (router.txt) do (
start telnet.exe -f log.txt %%A
cscript comandi.vbs
))


But I would be a list of output telnet command in one file.
With this code I have only one output telnet command in log.txt

In the same log.txt I want this structure:

%%A

output telnet;

%%A

output telnet

....

for anyone items of for loop.

Tnk

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: In batch script, output command in txt file

#8 Post by penpen » 05 Dec 2016 07:42

dosbatch wrote:But I would be a list of output telnet command in one file.
With this code I have only one output telnet command in log.txt
Because of that, i've added the "for/F %%b" part:
But without seeing the content of such a log-file, one can't give you detailed source code suggestions.

So if you want such, then you have to post at least one of these log files (you may disguise private data,
but list any character possible; also you may shorten text using "..." - but don't shorten important parts),
and outline the result you want to have:
You also should use code-blocks to separate text from data.


penpen

dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

Re: In batch script, output command in txt file

#9 Post by dosbatch » 05 Dec 2016 09:14

With this .bat

Code: Select all

(for /F "tokens=*" %%A in (router.txt) do (
start telnet.exe -f log.txt %%A
cscript command.vbs
))
Last edited by dosbatch on 06 Dec 2016 02:13, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: In batch script, output command in txt file

#10 Post by penpen » 05 Dec 2016 14:48

Basing on this log, there are some possibilities.

Lets try if the simplest solution is working:
This bases on the assumption, that the instruction "nvram commit" produces exact one output line,
which starts with a percentage character ('%'); also this is the only line starting with this character.

If the above is true, then (no "for /f" loop is needed and) this might help you (currently untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "telnetLogFile=logfile.txt"
set "logfile=log.txt"

for /F "usebackq tokens=*" %%A in ("router.txt") do (
   start telnet.exe -f "%telnetLogFile%" %%A
   cscript command.vbs
   >> "%logfile%" (
      echo(%%A
      echo(
      findstr /r /c:"^%%" "%telnetLogFile%"
      echo(
   )   
   del "%telnetLogFile%"
)
endlocal
goto :eof


penpen

Edit 1-2: Somehow i've lost the 'c' in "cscript". :oops:

dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

Re: In batch script, output command in txt file

#11 Post by dosbatch » 05 Dec 2016 15:54

"nvram commit" produces exact one output line, which starts with a percentage character ('%'). Exactly!

But your script don't work. I don't understand...

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: In batch script, output command in txt file

#12 Post by penpen » 05 Dec 2016 17:45

Hopefully the missing 'c' character was the only error in the script above.

penpen

dosbatch
Posts: 7
Joined: 04 Dec 2016 05:42

Re: In batch script, output command in txt file

#13 Post by dosbatch » 05 Dec 2016 18:48

Tnk you so much!! It works fine!

Post Reply