You are not logged in.
Pages: 1
Hi,
Ive been searching everywhere for hints about how to load a text file into a variable but has been very confused. My goal is the same as in powershell where I set the variable name to load each line of the text file and loop it through a process but i dont quite understand it when reading about batch files. Below is the code i want to use but getting the server names from a text file and looping this process on each line of the text file.
echo Checking: SERVERNAME ... (Display message on the screen)
echo ***SERVERNAME***>>%report% (Add server name to the report file location specified on the report variable)
rdp /v:SERVERNAME /u:%user% /p:%pass% /disconnect:2 /log:SERVERNAME.tmp (call 3rd party rdp app to test rdp connectivity and disconnect after 2 seconds then register the log events on a temp file)
type "SERVERNAME.tmp" | find /i "Normal" (find in the temp file for the word "Normal)
if ErrorLevel 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%) (Save Status of the RDP test into the report)
del SERVERNAME.tmp (Delete temp file previously created)
cls
Offline
Depending on how you need to do it, you don't necessarily need to load the text file as a variable. You can call a 'for' loop to run commands against the contents of a specific text file instead.
for /f %%a in (textfile.txt) do (call :check %%a)
goto :eof
:Check
set Serv=%1 [this will set the variable "Serv" with the server name from the text file]
echo Checking: %Serv%
echo ***%Serv%***>>%report%
rdp /v:%Serv% /u:%user% /p:%pass% /disconnect:2 /log:%Serv%.tmp
type "%Serv%.tmp" | find /i "Normal"
if %errorlevel% EQU 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%)
del %Serv%.tmp
cls
goto :eof
Hope this helps.
Offline
Depending on how you need to do it, you don't necessarily need to load the text file as a variable. You can call a 'for' loop to run commands against the contents of a specific text file instead.
for /f %%a in (textfile.txt) do (call :check %%a) goto :eof :Check set Serv=%1 [this will set the variable "Serv" with the server name from the text file] echo Checking: %Serv% echo ***%Serv%***>>%report% rdp /v:%Serv% /u:%user% /p:%pass% /disconnect:2 /log:%Serv%.tmp type "%Serv%.tmp" | find /i "Normal" if %errorlevel% EQU 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%) del %Serv%.tmp cls goto :eof
Hope this helps.
I have just tested but it did not worked. Do i need to put the whole path location of the text file? Also I was wondering the call:check is before :check, is this correct?
Offline
If "textfile.txt" is not in the same directory as the script, you'll need to provide a full (or relative) path to it.
The sequencing of "call :check" and ":check" should work just fine.
What output and/or errors were produced when you ran the script?
cmd | *sh | ruby | chef
Offline
If "textfile.txt" is not in the same directory as the script, you'll need to provide a full (or relative) path to it.
The sequencing of "call :check" and ":check" should work just fine.
What output and/or errors were produced when you ran the script?
This is the original batch script that works. The only thing I want to change is on the RDP Status section where instead of specifying each server name is to load the server name from a text file and loop the rdp testing on each line of the text file except for the blank rows. So I did replace that part with the code you provided but it is not working.
@echo off
cd %HOMEPATH%\Desktop\Checklist_Script
for %%A in (%Date%) do (
for /F "tokens=1-3 delims=/-" %%B in ("%%~A") do (
set today=%%B%%C%%D
)
)
set report=Checklist_Report_%today%.txt
set web1=http://server19:51908/default.aspx
set web2=http://server17/default.aspx
set web3=https://server16/opscenter/StandardReportsOverview.do
set web4=http://server15/
set /p user1=Type your User1 user account [domain\user]:
set /p pass1=Type your User1 password:
cls
set /p user2=Type your User2 user account [domain\user]:
set /p pass2=Type your User2 password:
cls
echo __________________________ URL Checklist _________________________>%report%
echo URL STATUS DATE TIME>>%report%
echo Checking: web1...
wget.exe %web1% --http-use=%User1% --output-file=web1.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web1.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo web1...................UN......%date%....%time%>>%report%) else (echo web1...................OK......%date%....%time%>>%report%)
del web1.tmp
cls
echo Checking: web2...
wget.exe %scomna% --http-use=%User2% --output-file=web2.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web2.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo web2.....................UN......%date%....%time%>>%report%) else (echo web2......................OK......%date%....%time%>>%report%)
del web2.tmp
cls
echo Checking: Web3...
wget.exe %analystic% --http-use=%User1% --output-file=web3.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web3.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo web3..........UN......%date%....%time%>>%report%) else (echo web3..........OK......%date%....%time%>>%report%)
del web3.tmp
cls
echo Checking: Web4...
wget.exe %web4% --http-use=%User1% --output-file=web4.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web4.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo Web4.....................UN......%date%....%time%>>%report%) else (echo web4.....................OK......%date%....%time%>>%report%)
del web4.tmp
cls
echo . .>>%report%
echo _______________________________ (RDP Status) _____________________________>>%report%
echo Checking: Server1...
echo ***server1***>>%report%
rdp /v:server1 /f /u:%userNA% /p:%passNA% /disconnect:2 /log:server1.tmp
type "server1.tmp" | find /i "Normal"
if ErrorLevel 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%)
del server1.tmp
cls
echo . .>>%report%
echo Checking: server2 ...
echo ***server2***>>%report%
rdp /v:server2 /f /u:%userNA% /p:%passNA% /disconnect:2 /log:server2.tmp
type "server2.tmp" | find /i "Normal"
if ErrorLevel 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%)
del server2.tmp
cls
echo . .>>%report%
echo Checking: server3 ...
echo ***server3***>>%report%
rdp /v:server3 /f /u:%userNA% /p:%passNA% /disconnect:2 /log:server3.tmp
type "server3.tmp" | find /i "Normal"
if ErrorLevel 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%)
del server3.tmp
cls
echo . .>>%report%
echo _______________________________ (Disk Space Status) _____________________________>>%report%
call disk_space_servers.ps1
type diskspace.tmp >>%report%
del diskspace.tmp
copy %report% "Reports"
del %report%
cls
@echo URL Check completed successfully.
@echo Please check the report in Reports\%report%
pause
Offline
If "textfile.txt" is not in the same directory as the script, you'll need to provide a full (or relative) path to it.
The sequencing of "call :check" and ":check" should work just fine.
What output and/or errors were produced when you ran the script?
There is no output errors.
Offline
Try this. I've included your entire script with an edit to the RDP Section.
As was mentioned in a prior post, in order for the 'for' loop to work the "Servers.txt" file will either need to be in the same path as the script, or its full drive and path location specified.
@echo off
cd %HOMEPATH%\Desktop\Checklist_Script
for %%A in (%Date%) do (
for /F "tokens=1-3 delims=/-" %%B in ("%%~A") do (
set today=%%B%%C%%D
)
)
set report=Checklist_Report_%today%.txt
set web1=http://server19:51908/default.aspx
set web2=http://server17/default.aspx
set web3=https://server16/opscenter/StandardReportsOverview.do
set web4=http://server15/
set /p user1=Type your User1 user account [domain\user]:
set /p pass1=Type your User1 password:
cls
set /p user2=Type your User2 user account [domain\user]:
set /p pass2=Type your User2 password:
cls
echo __________________________ URL Checklist _________________________>%report%
echo URL STATUS DATE TIME>>%report%
echo Checking: web1...
wget.exe %web1% --http-use=%User1% --output-file=web1.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web1.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo web1...................UN......%date%....%time%>>%report%) else (echo web1...................OK......%date%....%time%>>%report%)
del web1.tmp
cls
echo Checking: web2...
wget.exe %scomna% --http-use=%User2% --output-file=web2.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web2.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo web2.....................UN......%date%....%time%>>%report%) else (echo web2......................OK......%date%....%time%>>%report%)
del web2.tmp
cls
echo Checking: Web3...
wget.exe %analystic% --http-use=%User1% --output-file=web3.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web3.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo web3..........UN......%date%....%time%>>%report%) else (echo web3..........OK......%date%....%time%>>%report%)
del web3.tmp
cls
echo Checking: Web4...
wget.exe %web4% --http-use=%User1% --output-file=web4.tmp --spider --connect-timeout=2 --tries=2 --waitretry=1 --wait=1
type "web4.tmp" | find /i "connecting" | find /i "connected"
if ErrorLevel 1 (echo Web4.....................UN......%date%....%time%>>%report%) else (echo web4.....................OK......%date%....%time%>>%report%)
del web4.tmp
cls
echo . .>>%report%
echo _______________________________ (RDP Status) _____________________________>>%report%
for /f %%a in (Servers.txt) do (call :CheckRDP %%a)
goto DiskSpace
:CheckRDP
set Serv=%1
echo Checking: %Serv%...
echo ***%Serv%***>>%report%
rdp /v:%Serv% /f /u:%userNA% /p:%passNA% /disconnect:2 /log:%Serv%.tmp
type "%Serv%.tmp" | find /i "Normal"
if ErrorLevel 1 (echo RDP: UNREACHABLE - %date% %time%>>%report%) else (echo RDP: OK - %date% %time%>>%report%)
del %Serv%.tmp
cls
echo . .>>%report%
goto :eof
:DiskSpace
echo _______________________________ (Disk Space Status) _____________________________>>%report%
call disk_space_servers.ps1
type diskspace.tmp >>%report%
del diskspace.tmp
copy %report% "Reports"
del %report%
cls
@echo URL Check completed successfully.
@echo Please check the report in Reports\%report%
pause
I did not test
Offline
Pages: 1