Question

I have been trying to work this 1 out it's a fairly simple batch but I'm pretty much lost, the searches got my close but none seemed to work so I'm hoping somebody can help with this.

I have a batch file, it is far from optimized but basically what I am trying to do now is use the ping I have running in the batch which is written to check.csv should then populate the individual IP's at the beginning (hope that makes sense).

in a nut shell The variables at the beginning should be populated from check.csv

Here is the code

'@echo off
rem set the individual IP addresses

Rem these are manually set at present, I want these set automatically from check.csv which is created at the end of this file at present but will be moved infront..
set sarah=10.1.14.106
set richard=10.1.15.135
set kathh=10.1.12.79

edited out a number of these to reduce the code on screen for you, but they are all the same format, name & IP

rem set the individual profile name
set n1=sharman
set n2=rburrell

removed more of the same (just duplicates the above code for a different user)

rem set the computer name
set sarahPC=PB7B237     
set richardPC=PB1VAL9   

removed more of the same

REM set the main install paths
set M3="C:\Users\dclare\Documents\VW Compiler\Installers\M3.exe"
set H3="C:\Users\dclare\Documents\VW Compiler\Installers\H3.exe"
set MooresInst="C:\Users\dclare\Documents\VW Compiler\Catalogues\PD PS\Catalogue"
Rem menu
@echo off
cls
:start
echo.
echo 1   Ping
echo 2   List       
echo 3   DBase Installers   
echo 4   EXE Installers 
echo 5   End        
echo.
@CHOICE /C:12345

if errorlevel 5 goto End
if errorlevel 4 goto EXEInstallers
if errorlevel 3 goto DBase
if errorlevel 2 goto List   
if errorlevel 1 goto Ping   
goto End

:End
Pause
Exit


:EXEInstallers
echo EXE Installers
echo.
echo Upload folder
Xcopy %M3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
Xcopy %H3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
echo.
echo %sarah%
Xcopy %M3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
Xcopy %H3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
echo.
echo %richard%
Xcopy %M3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
Xcopy %H3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
echo.

removed more of the same (just duplicates the above code for a different user)

 goto Start

:DBase
echo Database Installers
echo.
echo %sarah%
Xcopy %MooresInst% "\\%sarah%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
echo %richard%
Xcopy %MooresInst% "\\%richard%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.

removed more of the same (just duplicates the above code for a different user)

echo 
Goto Start

:List
 echo.
 echo This is a list of the IP's used currently, check against any that fail.
 echo.
echo Name       Puter       IP
echo sarah      %sarahPC%%sarah%
echo richard        %richardPC% %richard%
echo kathh      %kathhPC%       %kathh%
echo amarie     %amariePC%      %amarie%

removed more of the same (just duplicates the above code for a different user)

echo.
Pause
Goto Start

:Ping
@echo off
if exist "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" Del  /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv"
Echo Pinging list...

set ComputerList=list.txt
pause
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion

 for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
 for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply ^|^| echo Not found Failed:')  do (
 set IPadd=%%B
 echo %%A,!IPadd:~0, -1!>>Check.csv
))

 pause
 Goto Start'

here is the check.csv file contents

PB1VAL9 10.1.15.135 
PB7B218 Failed 
PB1VAL8 10.1.15.111 
PB7B210 10.1.5.253 

removed more of the same (just duplicates the above code for a different user)

Was it helpful?

Solution

Try this routine out to set your variables to ip addresses:

echo off
for /f "usebackq tokens=1,2" %%a in ("check.csv") (
    echo setting %%a=%%b
    set "%%a=%%b"
)

With this ping routine it shows if it is online or offline. I had to guess what you were trying to do as we don't have your files to test it.

Give us a sample of Check.csv if you want to populate a set of variables with the IP addresses inside it.

:Ping
@echo off
Del  /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" 2>nul
Echo Pinging list...

set "ComputerList=list.txt"
pause
Echo Computername,IP Address>Final.csv

 for /f "usebackq delims=" %%A in ("%ComputerList%") do (
    ping -n 1 -l 1 %%A >nul
       if not errorlevel 1 (
             >>Check.csv echo %%A,online
          ) else (
             >>Check.csv echo %%A,offline
        )
 )

 pause
 Goto Start
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top