Sign in with
Sign up | Sign in
Your question
Solved

Need write results from 2 seperate files to one log file via .bat

Tags:
Last response: in Windows 7
Share
May 31, 2012 2:08:08 PM

I am in need of assistance. I have a batch file that runs nightly to restart machines in a specified list (restartmelist.txt) which works flawlessly. What I am looking to do, is have another file (restartmenames.txt) with a list of usernames, so when I look at the results file, I can not only see the results from each machine, but also whose machine it is. The names correlate in order with the machines. So restartmelist.txt looks like this:

MACHINE1
MACHINE2
MACHINE3

And restartmenames.txt looks like this:

Bob Lastname
Bill Lastname
Jason Lastname

Right now, my log-file writes like this:

.Machine1
.Machine2
.Machine3

What I would like to see, is this:

.Machine1 (Bob Lastname)
.Machine2 (Bill Lastname)
.Machine3 (Jason Lastname)

What I don't know how to do, is call that 2nd file to write the log entries, or if it's even possible. Here is what my code currently looks like:

  1. @ECHO OFF
  2.  
  3. FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A
  4.  
  5. >>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines STARTED on %date% @ %now%
  6. >>c:\scripts\Restartme.log 2>&1 Echo.
  7.  
  8. set _=c:\scripts\restartmelist.txt
  9. for /f %%i in (
  10. %_%
  11. ) do (
  12. >>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
  13. >>c:\scripts\Restartme.log 2>&1 Echo . %%i
  14. )
  15.  
  16. FOR /F "tokens=*" %%B IN ('TIME/T') DO SET NowDone=%%B
  17. >>c:\scripts\Restartme.log 2>&1 Echo.
  18.  
  19. >>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines COMPLETED on %date% @ %nowdone%
  20. >>c:\scripts\Restartme.log 2>&1 Echo ------------------------------------------------------------------
  21. >>c:\scripts\Restartme.log 2>&1 Echo.
  22.  
  23. Copy c:\scripts\Restartme.log \\seaapps\apps\logfiles /y


Is this something easy to accomplish!? I appreciate the help from anyone.

--- Charles!
May 31, 2012 3:51:05 PM


What are you using to gather the "Owner" of the system?

Are these systems a part of a Windows domain?

charlestwaters said:
I am in need of assistance. I have a batch file that runs nightly to restart machines in a specified list (restartmelist.txt) which works flawlessly. What I am looking to do, is have another file (restartmenames.txt) with a list of usernames, so when I look at the results file, I can not only see the results from each machine, but also whose machine it is. The names correlate in order with the machines. So restartmelist.txt looks like this:

MACHINE1
MACHINE2
MACHINE3

And restartmenames.txt looks like this:

Bob Lastname
Bill Lastname
Jason Lastname

Right now, my log-file writes like this:

.Machine1
.Machine2
.Machine3

What I would like to see, is this:

.Machine1 (Bob Lastname)
.Machine2 (Bill Lastname)
.Machine3 (Jason Lastname)

What I don't know how to do, is call that 2nd file to write the log entries, or if it's even possible. Here is what my code currently looks like:

  1. @ECHO OFF
  2.  
  3. FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A
  4.  
  5. >>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines STARTED on %date% @ %now%
  6. >>c:\scripts\Restartme.log 2>&1 Echo.
  7.  
  8. set _=c:\scripts\restartmelist.txt
  9. for /f %%i in (
  10. %_%
  11. ) do (
  12. >>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
  13. >>c:\scripts\Restartme.log 2>&1 Echo . %%i
  14. )
  15.  
  16. FOR /F "tokens=*" %%B IN ('TIME/T') DO SET NowDone=%%B
  17. >>c:\scripts\Restartme.log 2>&1 Echo.
  18.  
  19. >>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines COMPLETED on %date% @ %nowdone%
  20. >>c:\scripts\Restartme.log 2>&1 Echo ------------------------------------------------------------------
  21. >>c:\scripts\Restartme.log 2>&1 Echo.
  22.  
  23. Copy c:\scripts\Restartme.log \\seaapps\apps\logfiles /y


Is this something easy to accomplish!? I appreciate the help from anyone.

--- Charles!

m
0
l
May 31, 2012 4:13:17 PM

newbcakes said:
What are you using to gather the "Owner" of the system?

Are these systems a part of a Windows domain?


I pulled machine names and log in info via SCCM. I have to text files, and manually went line by line and made the name on the machines in the 2nd text file, match up with the order of the machines in the 1st text file.

Yes, they are all part of a domain, however, I am rebooting them via manual textfile per the code listed..
m
0
l
Related resources
May 31, 2012 4:16:48 PM

probably easily accomplished with a VBScript rather than .bat file.

Sounds like you want/need to have 2 separate files with various functions in each?

I'm not sure I follow why you're approaching it this way...
m
0
l
May 31, 2012 4:36:19 PM

Okay here's what I have going on.. Maybe there IS a better way to approach this.

I reboot machines nightly that are listed in a text file. The listed DOS Batch file pulls each line, and just re-runs the same SHUTDOWN restart command over and over until the last line on the text file is complete.

I have a different text file that is simply a list of names on each line, which are listed in accordance with which line the machine is listed on in the other text file.

What I would like to do, is while the script that's listed above runs as flawlessly as it does, when it writes the results to the results log, instead of just seeing

.MACHINE1
.MACHINE2
.MACHINE3 on each line of the file, I can see

.MACHINE1 (Joe Schmoe)
.MACHINE2 (Bob Schmoe)
.MACHINE3 (Jason Schmoe)

The MACHINEx names are pulled from 1 text file, and the names would be pulled from another text file.

Does that make a little better sense?
m
0
l
May 31, 2012 4:58:42 PM

I don't know how to do it with 2 files as you initially requested but I might have an alternative

If you can use a single source file you could set it up as a CSV file and use this kind of loop to display your results

Assuming a source file with the following format

Machine1;Joe Schmoe
  1. for /F "tokens=1,2 delims=;" %%i in (
  2. %_%
  3. ) do (
  4. >>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
  5. >>c:\test\Restartme.log 2>&1 Echo . %%i - %%j
  6. )


You also avoid possible issues with misaligned entries within your 2 files if you do it with a single file

The trick here is to use the tokens to split the source data in distinct fields, and knowing that %%i is your first field, %%j is the 2nd

m
0
l
May 31, 2012 5:02:12 PM

That's EXACTLY what I was trying to figure out how to do. So then my CSV file would need to be:

.MACHINE1 - (Username)
.MACHINE2 - (Username)

correct!?
m
0
l
May 31, 2012 5:11:41 PM

In my previous example I used
delims=;

So the semicolon is the field separator here

As a source file I'd suggest using a standard csv format

So this would be the recommended format
MachineName1;Username1
MachineName2;Username2

With the code I provided above this will produce something like
. MachineName1 - Username1
. MachineName2 - Username2

You can probably change the format in the echo command if you want something else although when I did it with

>>c:\test\Restartme.log 2>&1 Echo . %%i (%%j)

The last parenthesis wouldn't show for some reason which is why I used a dash


m
0
l
May 31, 2012 5:20:18 PM

I will try everything right now. Thank you...
m
0
l

Best solution

May 31, 2012 5:20:46 PM

Actually just found a way to make it work with parenthesis

>>c:\test\Restartme.log 2>&1 Echo . %%i (%%j^)

Will give you exactly the format you wanted
Share
May 31, 2012 5:26:01 PM

THANK YOU!! I worked on the code for 2 or 3 days to get it to work correctly, I've been out of DOS programming for so many years.

The only thing I couldn't get was the damn 2nd file. Now, *if* need be, I can continue to add delimiters in the same format you provided, correct!?
m
0
l
May 31, 2012 5:26:30 PM

Best answer selected by charlestwaters.
m
0
l
May 31, 2012 5:39:53 PM

The delimiter should stay the same [delims=;] but you could add new fields to the source file yes

Let say you want to add a phone extension

The source file would become
MachineName1;Username1;101
MachineName2;Username2;102
...

And in the loop you'd have to add a new token and %%k could be used to access it

  1. for /F "tokens=1-3 delims=;" %%i in (
  2. %_%
  3. ) do (
  4. >>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
  5. >>c:\test\Restartme.log 2>&1 Echo . %%i (%%j, #%%k^)
  6. )


Would output
. MACHINE1 (UserName1, #101)
. MACHINE2 (UserName2, #102)

Glad I could help
m
0
l
!