Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: .bat - looping through a text file  (Read 6048 times)

0 Members and 1 Guest are viewing this topic.

deville

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    .bat - looping through a text file
    « on: March 16, 2011, 09:11:01 PM »
    Hi,

    what i am trying to achieve, is to measure network performance using a batch file
    The batch file copies a test file (1meg.test) from from a central server to the c:\temp directory on a bunch of computers. by looping through a list (list.txt) of computers at remote sites to calculate the amount of time taken to copy the file to each computer, then outputs the results to a txt file (timer.txt)

    i have two batch files
    the first (run.bat) calls the main batch file and reads from the list.txt

    ---RUN.BAT---

    FOR /f %%u IN (list.txt) DO call c:\temp\timer.bat %%u >> c:\temp\timer.txt

    --------------------------------------------------------------------------
    which then should then run the second batch file (timer.bat)

    ---timer.bat---

    @echo off
     
    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
     set Milisecond=%time:~9,2% 
     set Day=%%A
     set Hour=%%B
     set Minute=%%C
     set Second=%%D
    )
    set /a Start=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
     
    copy c:\temp\1meg.test \\%comp%\c$\temp [this is where i am having a problem]

    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
     set Day=%%A
     set Hour=%%B
     set Minute=%%C
     set Second=%%D
    )
    set Milisecond=%time:~9,2% 
    set /a End=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
    set /a Diff=%End%-%Start%
    set /a DiffMS=%Diff%%%100
    set /a Diff=(%Diff%-%DiffMS%)/100
    set /a DiffSec=%Diff%%%60
    set /a Diff=(%Diff%-%Diff%%%60)/60
    set /a DiffMin=%Diff%%%60
    set /a Diff=(%Diff%-%Diff%%%60)/60
    set /a DiffHrs=%Diff%
     
    :: format with leading zeroes
    if %DiffMS% LSS 10 set DiffMS=0%DiffMS!%
    if %DiffSec% LSS 10 set DiffMS=0%DiffSec%
    if %DiffMin% LSS 10 set DiffMS=0%DiffMin%
    if %DiffHrs% LSS 10 set DiffMS=0%DiffHrs%
     
    echo %DiffHrs%:%DiffMin%:%DiffSec%.%DiffMS% >> c:\temp\timer.txt
    ---------------------------------------------------------------------------

    ---list.txt---
    computer1
    computer2
    computer3
    computer4
    computer5
    .etc..

    the output that i am trying to get is something like
    ----output.txt------
    computer1
    00.200 sec
    computer2
    00.300 sec
    computer3
    00.400 sec
    etc ....

    the time function calculates correctly my problem is getting the batch to loop through the list of remote computers
    if you can help, thanks in advance

    Dan O



      Rookie
      • Yes
    • Experience: Experienced
    • OS: Windows 7
    Re: .bat - looping through a text file
    « Reply #1 on: June 23, 2011, 08:37:01 PM »
    Have you considered making this into one file? You could call your list and then use a nested for loop to run the functions of the timer.bat file.

    For instance:


    @echo off
    FOR /f %%u IN (list.txt) DO (

    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
     set Milisecond=%time:~9,2% 
     set Day=%%A
     set Hour=%%B
     set Minute=%%C
     set Second=%%D
    )
    set /a Start=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
     
    copy c:\temp\1meg.test \\%%u\c$\temp

    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
     set Day=%%A
     set Hour=%%B
     set Minute=%%C
     set Second=%%D
    )
    set Milisecond=%time:~9,2% 
    set /a End=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
    set /a Diff=%End%-%Start%
    set /a DiffMS=%Diff%%%100
    set /a Diff=(%Diff%-%DiffMS%)/100
    set /a DiffSec=%Diff%%%60
    set /a Diff=(%Diff%-%Diff%%%60)/60
    set /a DiffMin=%Diff%%%60
    set /a Diff=(%Diff%-%Diff%%%60)/60
    set /a DiffHrs=%Diff%
     
    :: format with leading zeroes
    if %DiffMS% LSS 10 set DiffMS=0%DiffMS!%
    if %DiffSec% LSS 10 set DiffMS=0%DiffSec%
    if %DiffMin% LSS 10 set DiffMS=0%DiffMin%
    if %DiffHrs% LSS 10 set DiffMS=0%DiffHrs%
     
    echo %%u     %DiffHrs%:%DiffMin%:%DiffSec%.%DiffMS% >> c:\temp\timer.txt
    )