Daugusto
Daugusto

Reputation: 33

How do I use a variable outside the for loop in batch file?

I have a list "A.txt" that has a list of remote servers.

I need to do a net time in all of them and then echo the result with the remote server name. Example : "server 10/10/2018 19:00"

This is my code :

FOR /f %%G IN (D:\A.txt) DO (
    net time \\%%G
)>> B.txt 

FOR  /f "tokens=5,7,8" %%H IN (D:\B.txt) DO (echo %%H %%I %%J >> nettime.csv)

The result of this is : 

"\\Server1 10/10/2018 10:40:09 

 com   

 \\Server2 10/10/2018 10:40:09 

 com

It reads the token of the "command successful" line(I have Portuguese language on OS)

Is there a way to put the %%G in the echo? or can I hide the "command successful"?

Thanks

Upvotes: 1

Views: 246

Answers (1)

Squashman
Squashman

Reputation: 14290

Just pipe the output of the NET command to the FIND command to search for the string Current or whatever it is in your language. You can capture all of this with a FOR /F command.

@ECHO off
FOR /f "delims=" %%G IN (D:\A.txt) DO (
    FOR  /f "tokens=5,7,8" %%H IN ('net time \\%%G ^| find "Current"') DO echo %%H %%I %%J
)>> nettime.csv

Upvotes: 2

Related Questions