Solved

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

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:

[cpp]@ECHO OFF

FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A

>>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines STARTED on %date% @ %now%
>>c:\scripts\Restartme.log 2>&1 Echo.

set _=c:\scripts\restartmelist.txt
for /f %%i in (
%_%
) do (
>>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
>>c:\scripts\Restartme.log 2>&1 Echo . %%i
)

FOR /F "tokens=*" %%B IN ('TIME/T') DO SET NowDone=%%B
>>c:\scripts\Restartme.log 2>&1 Echo.

>>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines COMPLETED on %date% @ %nowdone%
>>c:\scripts\Restartme.log 2>&1 Echo ------------------------------------------------------------------
>>c:\scripts\Restartme.log 2>&1 Echo.

Copy c:\scripts\Restartme.log \\seaapps\apps\logfiles /y[/cpp]

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

--- Charles!
12 answers Last reply Best Answer
More about need write results seperate files file
  1. 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:

    [cpp]@ECHO OFF

    FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A

    >>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines STARTED on %date% @ %now%
    >>c:\scripts\Restartme.log 2>&1 Echo.

    set _=c:\scripts\restartmelist.txt
    for /f %%i in (
    %_%
    ) do (
    >>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
    >>c:\scripts\Restartme.log 2>&1 Echo . %%i
    )

    FOR /F "tokens=*" %%B IN ('TIME/T') DO SET NowDone=%%B
    >>c:\scripts\Restartme.log 2>&1 Echo.

    >>c:\scripts\Restartme.log 2>&1 Echo Restarting of machines COMPLETED on %date% @ %nowdone%
    >>c:\scripts\Restartme.log 2>&1 Echo ------------------------------------------------------------------
    >>c:\scripts\Restartme.log 2>&1 Echo.

    Copy c:\scripts\Restartme.log \\seaapps\apps\logfiles /y[/cpp]

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

    --- Charles!
  2. 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..
  3. 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...
  4. 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?
  5. 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
    
        for /F "tokens=1,2 delims=;" %%i in (
        %_%
        ) do (
        >>c:\scripts\Restartme.log 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
        >>c:\test\Restartme.log 2>&1 Echo . %%i - %%j
        )
    


    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
  6. 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!?
  7. 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
  8. I will try everything right now. Thank you...
  9. Best answer
    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
  10. 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!?
  11. Best answer selected by charlestwaters.
  12. 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

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


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

    Glad I could help
Ask a new question

Read More

Windows 7