Some portions of code in batch files are not executing - batch-file

I have a problem in this batch file:
#echo off
setlocal enableextensions EnableDelayedExpansion
for /f "tokens=*" %%l in (input1.txt) do (
ping %%l> "Result.txt"
set "var=HI"
set "var1=hi"
set "var2=1";
FIND /c "Destination host unreachable." Result.txt && ( set "var2=2") || ( echo HI)
FIND /c "Request timed out." Result.txt && ( set "var2=2" ) || (echo HI)
if "!var2!" EQU "2" (echo %%l>>"failure.txt")
# This block doesn't work
if "!var2!" EQU "1" (
for /f "tokens=*" %%i in (Result.txt) do ( set var=%%i)
for /f "tokens=9" %%j in ("%var%") do (set var1=%%j)
set var1="!var1:~0,-2!"
if "!var1!" LSS "1000" (echo %%l >> "success.txt") ELSE (echo %%l >>"timeout.txt")
)
)
endlocal
The above code is designed for ping bulk list of servers and redirect the servers to successful or failure text files based on the test results. Here the problem is the code marked by a rem remark is not working. It seems that portion is not executed. Also var1 is not being evaluated. Thanks in advance.

This will give you success or failure.
#echo off
for /f "delims=" %%a in (input1.txt) do (
ping %%l >nul
if errorlevel 1 (
>>"failure.txt" echo %%l
) else (
>>"success.txt" echo %%l
)
)
A problem in your code is that you are forcing string compares by using quotes around "!var1!" and the numeral.
You also have two sets of quotes around !var1!

Related

Batch File Filter loop output

The code runs a very simple process of pinging multiple addresses endlessy and outputting the results to a log file until manually stopped.
#echo off
title ping_logger
set d1=%date:~4%
set d2=%d1:/=-%
set t1=%time::=.%
set t2=%t1: =%
set host=X.X.X.1;X.X.X.162
set hostname=%host:;=+%
set pinghostname=%host:;= and %
set logfile=Log_%hostname%_%ComputerName%_%d2%_%t2%.csv
setlocal enableextensions ENABLEDELAYEDEXPANSION
set counter=0
for %%A IN (%host%) DO (
set /a counter+=1
)
endlocal && set counter=%counter%
echo Target Host(s) = %host%>%logfile%
echo Pinging %pinghostname% with 32 bytes of data: >>%logfile%
timeout %counter% >NUL
:Ping
FOR %%A IN (%host%) DO (
for /F "tokens=* skip=2" %%A in ('ping %%A -n 1 ') do (
echo %date:~4%, %time:~0,2%:%time:~3,2%:%time:~6,2%, %%A>>%logfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2%, %%A
)
)
IF (%counter% LSS 2) timeout 1 >NUL
GOTO Ping
The output is capturing too much into the log file. I'm looking to only get the first line from each response such as the highlighted lines
:DoPing
FOR %%A IN (%host%) DO (
set "pung="
for /F "tokens=* skip=2" %%A in ('ping %%A -n 1 ') do if not defined pung (
echo %date:~4%, %time:~0,2%:%time:~3,2%:%time:~6,2%, %%A>>%logfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2%, %%A
set "pung=Y"
)
)
IF (%counter% LSS 2) timeout 1 >NUL
GOTO DoPing
(I'm allergic to using keywords/executable names as labels)
Simply set a flag value pung to nothing so that it is undefined when the pings start. On reporting the first line, set pung to non-empty, so it is then defined and the remining report lines are suppressed.
Next loop of %%A clears pung before issuing the ping for the next host...
Another way:
:DoPing
FOR %%A IN (%host%) DO (
for /F "tokens=1* skip=2" %%A in ('ping %%A -n 1 ') do if "%%A" == "Reply" (
echo %date:~4%, %time:~0,2%:%time:~3,2%:%time:~6,2%, %%A %%B>>%logfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2%, %%A %%B
)
)
IF (%counter% LSS 2) timeout 1 >NUL
GOTO DoPing
This time, see whether the first token is the string Reply and only write the report if this is so.

Batch script not outputting files with this code

This script should output each word in a 10-word sentence stored in line 1 (When called with SeparateLine.bat (filename) 1, but it shows no errors nor outputs or makes the file.
I have already tried removing the parenthesis in the (echo %%a > 1.txt) and the ones below, with no avail. I've also tried different formats for where the outputs go, still no avail.
#echo off
if not exist "%~1" echo file not found & exit /b 1
if "%~2"=="" echo line not defined & exit /b
if "%~2"=="1" set line=1 & goto start
set /a line=%~2-1
:start
For /f "tokens=1,2,3,4,5,6,7,8,9,10* usebackq skip=%line% delims= " %%A in ("%~1") do (
if not "%%A"=="" (echo %%A > 1.txt)
if not "%%B"=="" (echo %%B > 2.txt)
if not "%%C"=="" (echo %%C > 3.txt)
if not "%%D"=="" (echo %%D > 4.txt)
if not "%%E"=="" (echo %%E > 5.txt)
if not "%%F"=="" (echo %%F > 6.txt)
if not "%%G"=="" (echo %%G > 7.txt)
if not "%%H"=="" (echo %%H > 8.txt)
if not "%%I"=="" (echo %%I > 9.txt)
if not "%%J"=="" (echo %%J > 10.txt)
GOTO endforloop
)
:endforloop
With the file that contains one, two, three... ten separated by spaces, it should output 10 files (maximum word limit is 10) each containing word 1-10 of the line (I specified line 1 in the second parameter as the words are on the first line), but the actual results are... nothing. Literally, it acts as if it completed successfully but no files are made and, well, no error codes.
I think this will be what you are looking for. Note you cannot use the pre-defined variable in skip=%line% you need to instead use more +%line%, there is not a need to define each token token=1,2,3,4... etc. simply use tokens1-10 and and last, but not least no need to use delims= as whitespace is the default delimeter:
#echo off
if not exist "%~1" echo file not found & exit /b 1
if "%~2"=="" echo line not defined & exit /b
if "%~2"=="1" set line=1 & goto start
set /a line=%~2-1
:start
For /f "tokens=1-10" %%A in ('type "%~1" ^| more +%line%') do (
if "%%A" NEQ "" echo %%A
if not "%%B"=="" echo %%B
if not "%%C"=="" echo %%C
if not "%%D"=="" echo %%D
if not "%%E"=="" echo %%E
if not "%%F"=="" echo %%F
if not "%%G"=="" echo %%G
if not "%%H"=="" echo %%H
if not "%%I"=="" echo %%I
if not "%%J"=="" echo %%J
goto :EOF
)
There is a flaw however with your line variable.
if a user selects 0 it will do 0 - = -1and then set the lines to skip-1if a user selects2it will set it to skip1` which is not what a user requested, so I suggest you clarify what your intensions are with the skip lines, but perhaps just get rid of it completely, like:
#echo off
if not exist "%~1" echo file not found & exit /b 1
if "%~2"=="" echo line not defined & exit /b
set /a line=%~2
:start
For /f "tokens=1-10" %%A in ('type "%~1" ^| more +%line%') do (
if "%%A" NEQ "" echo %%A
if not "%%B"=="" echo %%B
if not "%%C"=="" echo %%C
if not "%%D"=="" echo %%D
if not "%%E"=="" echo %%E
if not "%%F"=="" echo %%F
if not "%%G"=="" echo %%G
if not "%%H"=="" echo %%H
if not "%%I"=="" echo %%I
if not "%%J"=="" echo %%J
goto :EOF
)

Batch script to check the content and file name

I have a code in batch script which will check if the name of the file is same or content of the file is same or not. Also it will updat the ini file to 0 or 1
0 - if the file names are different
1 - if the file names are different
Below is my code
#ECHO OFF
CLS
del /s C:\deep\output.log > NUL
for %%i in (C:\deep\*.DAT) do (
for /f "tokens=1,2 delims= " %%G in (app.ini) do set %%G=%%H
echo Rungmis %rungmis%
fc C:\deep\MAI_ZSYS_MOVE.DAT %%i > NUL
if errorlevel 1 (
CALL :error
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are different >>output.log
set /a rungmis=0
echo Rungmis %rungmis%
timeout 5
) ELSE (
CALL :next
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are same >>output.log
set /a rungmis=1
echo Rungmis %rungmis%
timeout 5
)
)
for %%I in (rungmis) do (
setlocal enabledelayedexpansion
type app.ini | find /v "%%I=">settings.tmp
move /y settings.tmp gmisapp.ini
echo %%I=!%%I!>>app.ini
)
type app.ini
timeout 5
It is updating the .ini flag (rungmis flag) to 0 or 1. But the problem which i am facing is whenever the ini is getting updated the flag (rungmis) is getting updated at the last line of the ini
Actual app.ini file
[TO_RUN_GMIS]
rungmis=1
;0 means GMIS will run
;1 means GMIS will not run
[Registry_Directories]
ArchivePath=D:\maibackup\
ImportPath=D:\gmisdata\
ExportPath=D:\www\GMIS\excel\
DataSource=GMIS_DEV_NEW
app.ini after getting updated
[TO_RUN_GMIS]
;0 means GMIS will run
;1 means GMIS will not run
[Registry_Directories]
ArchivePath=D:\maibackup\
ImportPath=D:\gmisdata\
ExportPath=D:\www\GMIS\excel\
rungmis=1
can anyone please help me out with that?
Please give this a try.
#ECHO OFF
CLS
del /s C:\deep\output.log > NUL
for %%i in (C:\deep\*.DAT) do (
for /f "tokens=1,2 delims= " %%G in (app.ini) do set %%G=%%H
echo Rungmis %rungmis%
fc C:\deep\MAI_ZSYS_MOVE.DAT %%i > NUL
if errorlevel 1 (
CALL :error
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are different >>output.log
set /a rungmis=0
echo Rungmis %rungmis%
timeout 5
) ELSE (
CALL :next
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are same >>output.log
set /a rungmis=1
echo Rungmis %rungmis%
timeout 5
)
)
for /f "tokens=*" %%a in ('type "app.ini" ^| find /v /n "" ^& break ^> "app.ini"') do (
set "str=%%a"
setlocal enabledelayedexpansion
set "str=!str:*]=!"
if "!str:~0,7!"=="rungmis" set "str=!str:~0,-1!%rungmis%"
>>app.ini echo(!str!
endlocal
)
type app.ini
timeout 5
So we simply do a type on the file and add some additional characters to ensure we replicate newlines as well. We then get rid of those characters before printing to file. But first we search for the string rungmis and replaces its value with the value you determined earlier in the script. Then each line gets printed back into the app.ini file with the replacement value.

How can we split string using windows bat

How can we split string using windows bat script?
for below .bat code snippet
#echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
rem mkdir !a!
) || (
echo +)
rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause
below config file
Q
What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
How can I get the /path/to/case and value as a pair?
for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"
set j=0
for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
echo mkdir !a!
) || (
echo +)
for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q43407067.txt containing your data for my testing.
(These are setting that suit my system)
SO - to address your problems:
because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----
You can't delete that ) because it's participating in a parenthesis-pair.
You need a space between the in and the (.
I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)
In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.
Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.
I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:
#Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
Echo=MD %%A
Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
) Do (Set/A "i+=1"
Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF
remove Echo= on line 6 if you are happy with the output and really want to create those directories

Dos batch script error? Error occurred while processing: .exe

In a DOS script that I wrote, I am unable to figure out what causes this error that I get:
The system cannot find the file specified.
Error occurred while processing: .exe.
Here is the script. Any help would be greatly appreciated. I tried to ask for help on the DosTips forum but I am getting no answer. :
#echo off
:: script to edit property files
CALL :PROPEDIT # Key4 Value446 test.properties
GOTO :END
:PROPEDIT [#] PropKey PropVal File
IF "%~1"=="#" (
:: Passing a first argument of "#" will disable the line while editing
SET "_PREFIX=#"
SHIFT
)
IF NOT "%~4"=="" (
ECHO Too many arguments.
EXIT /B 1
)
IF "%~3"=="" (
ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
EXIT /B 1
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%_FILE%.bak" ^|FINDSTR /N /I "%_PROPKEY%="`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=:" %%S IN ("%LINE%") DO SET LINE=%%S
SET /A COUNT=1
FOR /F "USEBACKQ delims=" %%A IN (`TYPE "%_FILE%.bak" ^|FIND /V /N ""`) DO (
SET "LN=%%A"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "LN=!LN:*]=!"
IF "!COUNT!" NEQ "%LINE%" (
ECHO(!LN!>>%_FILE%
) ELSE (
ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
)
ENDLOCAL
SET /A COUNT+=1
)
EXIT /B 0
:END
ECHO --- Finished Test ---
pause
Remove the .EXE of FIND and TYPE
You don't need TYPE. You can do just this:
FOR /F "tokens=*" %%A IN (`FIND /N /I "%_PROPKEY%=" "%_FILE%.bak"`) DO (
If FIND spoils your results (by not using TYPE) then consider using FINDSTR instead and use 'DELIMS=:' instead of 'DELIMS=]'
If I'm right my assumption that the following is helpful, take a look at the 'MORE +nnn' command (note the '+nnn' which outputs lines from a specific location in the file).
Why not just place your 'SETLOCAL ENABLE.. etc' at the top of your code?
If you explain what it is you're trying to attempt, then I might be in a better position to help.
Just a few thoughts :)
Here is the working code after getting some help from Paul Tomasi:
#echo off
SETLOCAL DISABLEDELAYEDEXPANSION
CALL :PROPEDIT # Key4 Value446 test.properties
GOTO :END
:PROPEDIT [#] PropKey PropVal File
IF "%~1"=="#" (
:: Passing a first argument of "#" will disable the line while editing
SET "_PREFIX=#"
SHIFT
)
IF NOT "%~4"=="" (
ECHO Too many arguments.
EXIT /B 1
)
IF "%~3"=="" (
ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
EXIT /B 1
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%_FILE%.bak" ^|FINDSTR /N /I "%_PROPKEY%="`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=:" %%S IN ("%LINE%") DO SET LINE=%%S
SET /A COUNT=1
FOR /F "USEBACKQ delims=" %%A IN (`TYPE "%_FILE%.bak" ^|FIND /V /N ""`) DO (
SET "LN=%%A"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "LN=!LN:*]=!"
IF "!COUNT!" NEQ "%LINE%" (
ECHO(!LN!>>%_FILE%
) ELSE (
ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
)
SETLOCAL DISABLEDELAYEDEXPANSION
SET /A COUNT+=1
)
EXIT /B 0
:END
ECHO --- Finished Test ---
pause

Resources