Hi I am having problems with one of my first CMD scripts, any suggestions on how to go about this loop with grabbing the results outputted the command window with the if statements?
#echo off
:BEGIN
T:
cd js
cd ddf
cd invent
for %%s in ("invstk" "labmas") do (
Call Isutl -r %%s
if result contains "%ISUTL=20: Cannot open index file" goto SendEmail else if the result = "Indexing Complete" continue to next file in
list, if result not contains "%ISUTL=20: Cannot open index file" or "Indexing Complete" then call Isutl -r on that file again
)
:SendEmail
:DONESCAN
Exit
See if this does what you need:
#echo off
:BEGIN
cd /d "t:\js\ddf\invent"
for %%s in ("invstk" "labmas") do call :routine "%%s"
del file.log 2>nul
pause
goto :EOF
:routine
Call Isutl -r "%~1" >file.log 2>&1
find /i "ISUTL=20: Cannot open index file" <file.log >nul && goto SendEmail
find /i "Indexing Complete" <file.log >nul && goto :EOF
goto :routine
:SendEmail
rem send email
goto :EOF
Related
I have two batch files one batch files calls another. The one that calls the other is called install.bat, and the other controller.bat.
The contents of install.bat are as follows:
start controller.bat a.exe b.exe
Then the controller.bat file operates as such:
#echo off
:: Check if the first argument is passed
if "%~1"=="" (
echo "No first argument passed"
) else (
set first_arg=%~1
)
:: Check if the second argument is passed
if "%~2"=="" (
echo "No second argument passed"
) else (
set second_arg=%~2
if not "%~1" == "" (
set current=one
) else(
echo twob >%~dp0current.txt
)
)
call :Resume
goto %current%
goto :eof
:one
::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
echo "update will start"
start /wait %second_arg%
echo "update done"
pause
shutdown -r -t 0
goto :eof
:two
echo three >%~dp0current.txt
echo -- Section two --
echo "updating now"
Start /wait %first_arg%
echo "Update Finish"
pause
shutdown -r -t 0
goto :eof
:twob
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo three >%~dp0current.txt
echo -- Section two --
echo "updating now"
Start /wait %first_arg%
echo "Update Finish"
pause
shutdown -r -t 0
goto :eof
:three
::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
pause
goto :eof
:resume
if exist %~dp0current.txt (
set /p current=<%~dp0current.txt
) else (
set current=one
)
The problem it is not working well it jumps into twob and then straight to three. It is supposed to show the intermediate steps to a user to show the updates are occurring.
I am expecting it to be able to read if one argument is supplied let's say the second and not the first and still do the jumps to right labels. Right now, the labels are a bit mixed up when I supply both args %~1 and %~2.
My requirement is simple, i just want to scan all the files in the current directory for a particular string and if that string is found i just want a display saying "String is found" otherwise "String not found"
#ECHO OFF
for %%f in (C:\Users\aalvoor\Desktop\BatchScript\*) do (
echo File is %%f
find /c "defaultModel" %%f >NUL
if %errorlevel% equ 1 (echo File is notfound) else (echo String is found)
)
But the problem is it works when i am not putting it in a for loop but when i put it in for loop for some reason for every file i get a message String is found which is not true.
Use conditional operators && and ||
&& being if errorlevel equ 0
|| being if errorlevel neq 0
#echo off
for %%f in ("%userprofile%\Desktop\BatchScript\*") do (
echo File is %%f
find /c "defaultModel" %%f >nul 2>&1 && echo String found || echo String NOT found
)
I need to read a file in outer loop line by line, take this value and use it in inner loop. But currently I am able to read first line from this file and do some required processing in inner loop but outer loop runs only once.
Why does the outer loop run only once?
myfile.txt contains:
AWC00201
AWC00202
AWC00203
DDDD
#echo off
setlocal EnableDelayedExpansion
for /F %%D in (myfile.txt) do (
echo %D%
S:
cd \#vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D
echo %%D
FOR /F %%i IN ('dir /b /ad-h /o-d') DO (
echo After Nested For
echo %%D
SET test=%%D
SET b=%%i
GOTO found
)
echo No subfolder found
goto done
:found
echo %D%
echo Most recent subfolder: %b%
cd %b%
echo %%D
find /c "O K" tooling.report
echo %D%
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done
echo %D%
echo now go up
echo !test!
echo %test%
)
pause
I am getting following output:
ECHO is off.
AWC00201
After Nested For
AWC00201
ECHO is off.
Most recent subfolder: 20141103_170658_wro_awc
%D
____________ TOOLING.REPORT: 0
ECHO is off.
notfound
ECHO is off.
now go up
AWC00201
AWC00201
Press any key to continue . . .
Your code has one big problem and one thing to change
The problem is that it is not possible to use goto while inside a for loop and keep the loop iterating. goto cancels the for looping.
The thing to change is your use of variables. You have the information you need inside the for replaceable parameters. Use them. Move the value to a variable when the replaceable parameters does not offer what you need, but this is not the case
#echo off
setlocal enableextensions disabledelayedexpansion
for /F "delims=" %%D in (myfile.txt) do (
cd /d "s:\#vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D"
for /d %%a in (.) do echo Current folder is "%%~fa"
set "file="
FOR /F "delims=" %%i IN ('dir /b /ad-h /o-d 2 >nul ') DO if not defined file (
set "file=1"
echo subfolder found : %%i
find /c "O K" ".\%%i\tooling.report" >nul 2>nul
if errorlevel 1 (
echo O K found
) else (
echo O K not found or file does not exist
)
)
if not defined file (
echo subfolder not found
)
)
pause
I am looking for help on creating a batch script which:
Asks for an input line(maybe a few words, may include wildcards, maybe just a keyword)
Goes through all the csv/xls files in a folder
Extracts the rows where that input line is found
Puts the results into a new workbook
Any help please would be great,
would love if the file is also annotated so i can learn to
Thank you in advance
#Echo OFF
#IF "%_Echo%" NEQ "" Echo %_Echo%
Echo xls file filter - select lines ending with the requested extension type
Echo v1.1
If "%1" NEQ "" goto -Process
Echo.
Echo Parameters
Echo 1 - Extension to search for
Echo.
Echo .csv Files in C:\Users\test\Desktop\output will be scanned for the input
Echo All matching extensions found in folder C:\Users\test\Desktop\output\*.csv
Echo Where ********* is the filteryou entered.
Echo.
Pause
Set /p _b= "Enter Extension "
if "%_b%" NEQ "" goto -Process2
Goto :EOF
:-Process
Set _b=%1
:-Process2
Set _Src="C:\Users\test\Desktop\output"
Echo Source %_Src% Files 20yy-mm-dd.csv
Set _TFN="C:\Users\test\Desktop\Files\%_b%.csv"
Echo Filtered File: %_TFN%
set _HDR=
set _cnt=0
Pushd %_src%
::dir /b "C:\Users\test\Desktop\output\*.csv"
FOR /F "usebackq tokens=1,2*" %%a IN (`dir /b "C:\Users\test\Desktop\output\**********.csv"`) DO Call :-FilterFl %%a
Echo %_cnt% Files processed
popd
pause
Goto :EOF
:-FilterFl
::Echo 1-%1 2-%2
if not exist %1 goto :-NF
if "%_HDR%" NEQ "Done" call :-DoHdr %1
Echo Processing %1
FIND /i "%_b%"<%1 >>%_TFN%
set /a _cnt=_cnt + 1
Goto :EOF
:-NF
Echo %1 is not found
goto :EOF
:-DoHdr
if "%_Debug%" NEQ "" Echo -DoHdr %1
FIND "---," <%1 >%_TFN%
set _HDR=Done
goto :EOF
Ok I have found a few questions on this, but each are saying make sure the use CALL and exit \b or goto eof in the 2nd bat file but for some reason I am not getting this to work, I have tried both, the batch file exits every time after executing the first call statement:
batch file 1 (myscript.bat):
:#echo off
del files
dir /B /O-D | find "test2" > tmp
dir /B /O-D | find "test3" > tmp2
CALL head 1 tmp > files
CALL head 1 tmp2 >> files
head.bat:
#echo off
if [%1] == [] goto usage
if [%2] == [] goto usage
call :print_head %1 %2
goto :eof
REM
REM print_head
REM Prints the first non-blank %1 lines in the file %2.
REM
:print_head
setlocal EnableDelayedExpansion
set /a counter=0
for /f ^"usebackq^ eol^=^
^ delims^=^" %%a in (%2) do (
if "!counter!"=="%1" goto :eof
echo %%a
set /a counter+=1
)
goto :eof
:usage
echo Usage: head.bat COUNT FILENAME
Execution:
C:\Users\ots>myscript.bat
C:\Users\ots>del files
C:\Users\ots>dir /B /O-D | find "test2" 1>tmp
C:\Users\ots>dir /B /O-D | find "test3" 1>tmp2
C:\Users\ots>CALL head 1 tmp 1>files
C:\Users\ots>
How can I get it to run the second "tmp2" Call line?
Thanks!
Your code is fine, both calls are indeed made.
The issue is that you are setting echo to OFF in head.bat, so after the first call, your command does not get echoed on the console, but that does not mean the file is not called.
To verify this, remove the #echo off from head.bat, and you will see your second CALL command.