Populate a batch from a file - variables

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)

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

Related

Retry a sql query in case of failure in batch

This script processes all the sql files in a given folder and outputs the result to a csv. Do you have any ideas how could I adapt it to retry the sql file in case of error or failure?
#ECHO OFF
SET SQLCMD="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
SET PATH="C:\Users\username\Desktop\Scripts\Reports\sql"
SET SERVER="localhost"
SET DB="database"
SET LOGIN="username"
SET PASSWORD="password"
SET OUTPUT="C:\Users\username\Desktop\Scripts\Reports\output_%date%-%time:~0,2%-%time:~3,2%-%time:~6,2%.csv"
CD %PATH%
ECHO %date% %time% > %OUTPUT%
for %%f in (*.sql) do (
%SQLCMD% -S %SERVER% -d %DB% -E -i %%~f >> %OUTPUT% -W -w 1024 -s";")
Thank you!
You cant test the ERRORLEVEL environment variable. typically a zero ERRORLEVEL value means success. also you need to enable delayed expansion to check it inside a block.
something like this may help,
SetLocal EnableDelayedExpansion
for %%f in (*.sql) do (
Set /a success=1
for /L %%w in (1,1,5) do ( rem retry five times
if !success! NEQ 0 (
%SQLCMD% -S %SERVER% -d %DB% -E -i %%~f >> %OUTPUT% -W -w 1024 -s";"
if !ERRORLEVEL! EQU 0 set /a success=0
)
)
if !success! NEQ 0 (
rem sql failed, log or advise
)
)
EndLocal
another important point, be careful with the PATH environment variable. you should better use another name as SQL_PATH or MY_PATH.
or you can use PUSHD & POPD to change your working dir
#echo off
SET ...
rem save current dir and jump to...
pushd "C:\Users\username\Desktop\Scripts\Reports\sql"
for %%f in (*.sql) do (
...
...
)
rem restore dir saved by pushd
popd

New to batch and having issues with multiple variables and for loops

Trying to find out which users have google chrome by searching the appdata folder of each user for the executable and then a series of actions to take if found. On our system some of the user folders are on a persistent disk, D:. I used a for loop for both instances but I'm sure there is a better way.
If I run it from the computer it seems to work but when ran as a start up nothing seems to happen. Wondering if someone could point out issues or inefficiencies, I know there in there.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%H IN ("D:\Users\*") DO (
IF EXIST "%%H\appdata\local\google\chrome\application\chrome.exe" (
DEL /Q "%%H\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%H Replaced >> "ChromeInstalls.txt"
)
)
FOR /D %%G IN ("C:\Users\*") DO (
IF EXIST "%%G\appdata\local\google\chrome\application\chrome.exe" (
DEL /Q "%%G\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%G Replaced >> "ChromeInstalls.txt"
)
)
EXIT /b
Any help is appreciated.
The problem is that you are parsing invalid path to the for body . Try using /r switch to lock on the parent drive containing the chome file to process all subdirectories inside the specified drive so that you dont have to use a full path in your set () eg ..
#echo off
For /r d: %%a in (*) do (
If "%%~nxa"=="Chrome.exe" (
DEL /Q "%%G\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%G Replaced >> "ChromeInstalls.txt"
)
)
For /r c: %%a in (*) do (
If "%%~nxa"=="Chrome.exe" (
DEL /Q "%%G\Desktop\Google Chrome.lnk"
msiexec /i "[MSI PATH]" /quiet
ECHO %DATE% %COMPUTERNAME% %%G Replaced >> "ChromeInstalls.txt"
)
)
Try that out . Hpe this helps .

Using a bat file to create a log

I am trying to monitor the install of software on our servers remotly with a script by checking the .dll file version. I am not sure if this can be done but after the install on all of the servers I would like to run a bat file to look at all of the servers and create a log file with the server name and that .dll file version number. Suggestions would be great. Thanks.
First download STRINGS.EXE from here (to handle unicode).
http://technet.microsoft.com/en-us/sysinternals/bb897439.aspx
Here is a little something I have been using for quite a while. It does a bit more than you asked for but maybe someone else would prefer to have all of this. You can easily delete the parts you don't want. Add/remove file types as you like and remove the output sections you don't want. It does not do exactly what you asked but the hard part is done and you can adapt as you see fit. Save the code as FileVersions.bat (or whatever). Put this bat file and the downloaded STRINGS.EXE in the same folder. Execute this bat file to get output in FileVersions.txt (or whatever you name bat file). It will look something like this. Gets versions for all specified file types in folders underneath the current folder. Get this modified as you desire and then handle multiple machines as you see fit.
====== Output ============
Versions for *.dll *.ocx *.exe files - Thu 08/08/2013 21:13:28.17
File=C:\Drivers\storage\strings.exe
FileDesc=strings
FileVer=2.41
ProductName=Sysinternals Strings
ProductVer=2.41
File=C:\Drivers\storage\R159108\TEACico2.dll
FileDesc=TEACico2.DLL
FileVer=1, 1, 0, 0
ProductName= TEACico2 DLL
ProductVer=1, 1, 0, 0
====== End of Output ============
#ECHO OFF
TITLE %~n0
PUSHD %~dp0
SET FileTypes=*.dll *.ocx *.exe
SET OutFile=%~n0.txt
IF EXIST %OutFile% DEL /q %OutFile%
ECHO.Retrieving file versions for %FileTypes% files
ECHO.
ECHO.Versions for %FileTypes% files - %Date% %Time% > %OutFile%
ECHO.
FOR /R "%CD%" %%A IN (%FileTypes%) DO ECHO.%%A & CALL :FileVersion "%%A" >> %OutFile%
ECHO.
ECHO.Results are in %OutFile%
pause
GOTO :eof
REM ========================= Subroutines =========================
:FileVersion
SETLOCAL
ECHO.
ECHO.File=%~1
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "FileDescription"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :FileVer
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET FileDesc=%%B
IF NOT "%FileDesc%"=="FileVersion" SET FileDesc
:FileVer
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "FileVersion"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :ProductName
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET FileVer=%%B
SET FileVer
:ProductName
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "ProductName"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :ProductVer
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET ProductName=%%B
IF NOT "%ProductName%"=="ProductVersion" SET ProductName
:ProductVer
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "ProductVersion"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :Done
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET ProductVer=%%B
SET ProductVer
:Done
ENDLOCAL
GOTO:EOF

Batch: for /f +xcopy output - Save to .log

I have the next script, and I need it to save all the xcopy files copy outputs to one log file,
:tmdeploy
title Deploying Edithor - %deployer%
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\CompilacionQA
set dst_folder=S:\ApliTelinver\Ambientes-Edithor\Sincronizacion\Test\Test-Mantenimiento
set filelist=filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
)
echo.
pause
goto end
The problem is that I only get the last file copy in the output. And how to properly do a timestamp for .log file?
Thank you
You should use the appended redirection operator, >> instead of >.
So, your for loop will look like this:
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
)

Howto add a string to the 'type <filename>' DOS command before merging into file?

Question: I've several text files containing sql create table/column/view/storedProcedure textfiles. Now I want to merge the textfiles into one textfile.
I go into the directory, and type:
type *.sql >> allcommands.sql
Now to problem is I should add the text ' GO ' after every file's content.
I can append Go by doing
type *.sql >> allcommands.sql & echo GO >> allcommands.sql
But this only inserts go once.
How can I accomplish this with DOS commands ?
You want something like this:
for %%f in (*.sql) do type %%f >>allcommands.sql & echo GO >> allcommands.sql
The %% is for use in a batch file. If you're not running it from a batch file you only need single % signs.
Try this one
for %%f in (*.sql) do (
type %%f >>allcommands.sql
echo. >> allcommands.sql
echo GO >> allcommands.sql
echo. >> allcommands.sql )
it adds newline then go for each SQL file. it works for me try it.
Use copy to concatenate the first file with a file with "GO" text, then concatenate again with the second file.
#echo off
CLS
::concat.bat outfile.sql
setlocal EnableDelayedExpansion
If EXIST GOTMP.TMP DEL /Q GOTMP.TMP
Echo GO>GOTMP.TMP
ECHO.>>GOTMP.TMP
If EXIST "%~1" DEL /Q "%~1"
Echo.>"%~1"
for /f "tokens=*" %%A in ('dir /a-d /on /b "*.sql"') do call :perfaction "%%A%" "%~1"
ECHO Done Generating Output "%~1"
ECHO.
pause
Goto :EOF
:perfaction
ECHO "%~1"
copy "%~2"+"%~1"+GOTMP.TMP "%~2"
GOTO :EOF