I am trying to figure out how to utilize findstr so that it will do the same thing as this line in awk:
wk.exe "begin {temp=0}/^stringMarker/{temp=1}{if (temp==1)print$0}" %TEMP%\input.txt >%TEMP%\output.txt
This is all I can think of right now:
#ECHO OFF
findstr /b /c:"Hello" Hello.bat > nul
if errorlevel 1 (
echo Search Failed
) else (
echo Search Sucessful
)
as you can see the latter half of the code is missing. I need help to find out how to get the lines after the stringMarker and then be able to redirect them into a file.
Cheers
I would suggest using a windows port of gawk if possible. If you can't for some reason, maybe a vbscript would work best?
If you must use findstr and cmd, the following should work.
find_thing.cmd "^stringMarket" in.txt > out.txt
find_thing.cmd
#echo off
if "%~1"=="" ((echo no arguments) && exit /b)
if "%~2"=="" ((echo insufficient arguments) && exit /b)
set query=%~1
set file_name=%~2
for /f "tokens=1,2* delims=:" %%a in ('findstr /r /n "%query%" "%file_name%"') do (
echo %%b
more +%%a "%file_name%"
exit /b
)
Related
I want to know how to use Findstr command to find a text in a file and make it a variable here what i tried :
#echo off
for /F "delims=" %%a in ('findstr /I /m "100" Config.dat') do set "Variable=%%a"
cls
if %errorlevel% == 0 (
goto found
) else (
goto nope
)
:found
cls
echo founded ! %Variable%
pause
exit
:nope
cls
echo not found!
pause
exit
Ok i explain : In the 2nd line the number "100" is what i want to find and the "Config.dat" is the file that have in it the number 100 and some other numbers and the "Variable" in there is the name of the variable that i want to store in it 100.
The problem is when it founded number 100 it goes to the function "found" and displays "Founded! 100" but when it not founded it also goes to "found" function and only display founded! without 100. So why when it didn't founded it it goes to "found" i need it to go to "nope".
So i hope you guys explain to me if i did something wrong and thanks!
This is because for /F calls command in a standalone cmd.exe process and does not return the error level into context of the caller:
#echo off
rem drop last error level
type nul>nul
for /F "usebackq delims=" %%a in (`cmd.exe /C #exit /b 123`) do rem
echo ERRORLEVEL=%ERRORLEVEL%
-
ERRORLEVEL=0
If you want just load config values into environment variables, then there is no need to search anything. Just create standalone configuration file for that.
config.vars
# loads by cmd.exe script
aaa=111
"bbb=111 222"
/A ccc=1+1
"ddd=%bbb% & 333"
load_config.bat
#echo off
for /F "usebackq eol=# tokens=* delims=" %%i in ("config.vars") do (
call set %%i
)
Trying to do a bit of scripting to run on a windows server. The aim of the code is to check a given directory F:\TestFolder for the arrival of 1 or more files that start with IB30321* in the name.
If the file(s) have not arrived in the given directory then the script sleeps for 5mins before checking again. Once the correct file(s) have been found it exits the script.
Unfortunatly i'm getting the following error, any ideas what i can do to fix this?
FINDSTR: Bad command line
Incorrect number of files found, 1 file expected
#Echo off
cd /D "%F:\TestFolder%"
Set numfiles=0
For /f "tokens=1,* delims=:" %%A in (
'Dir /B "IB30321*" ^| findstr /n ^ '
) DO Set numfiles=%%a&Set filename=%%B
If %numfiles% equ 1 (
echo %filename% found
exit /B 0
) else (
echo "Incorrect number of files found, 1 file expected"
set numfiles=0
Timeout /T 300
)
To elaborate a bit more on my comment.
dir outputs an error message if no matching file(s) found.
findstr doesn't care what lines it counts.
suppressing error output on no find will have the do part not executed as there isn't any output.
#Echo off
cd /D "%F:\TestFolder%"
:Loop
Set numfiles=0
For /f "tokens=1,* delims=:" %%A in (
'Dir /B "IB30321*" 2^>NUL ^| findstr /n ^ '
) DO Set numfiles=%%a&Set filename=%%B
If %numfiles% equ 1 (
echo %filename% found
exit /B 0
) else (
echo "Incorrect number of files found, 1 file expected"
Timeout /T 300
)
Goto :Loop
Hi I'm trying to create a batch file to filter out servers which has RDP/ICA listener down from a list of servers in a notepad file, I created this script with the below syntax, but for some reasons it won't work as expected, can some one help me fix the situation?
I've a list of servers in computer.txt file and I'm trying to find the one's which are down and if errorlevel is 0, meaning the string down is found, I want the server name to be printed in listenerdown.txt , but for some reasons, if I execute the batch file, all the servers in computer.txt gets written to listenerdown.txt file
below is the batch file
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr/i down >nul 2>&1
if %errorlevel% neq 1
echo %%i >>Listenerdown.txt
)
Move the echo onto the same line as the if statement or else use parentheses to establish scope and %ErrorLevel% will always be 0 because the variable does not get set in a loop without delayed expansion.
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr /i down >nul 2>&1
if !errorlevel! neq 1 echo %%i>>Listenerdown.txt
)
endlocal
or
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr /i down >nul 2>&1
if !errorlevel! neq 1 (
echo %%i>>Listenerdown.txt
)
)
endlocal
Cmd.exe parses batch files line by line and unless you tell it that the scope of the command continues onto the next line it will think the command is finished.
You should use setlocal EnableDelayedExpansion, and !errorlevel! instead of %errorlevel%:
#echo off
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do (
qwinsta /server:%%i | findstr/i down >nul 2>&1
if !errorlevel! neq 1 echo %%i >>Listenerdown.txt
)
otherwise the value of errorlevel would be expanded only once, before entering the loop, and would not have the correct value. You also have to make sure that echo is in the same row as the if.
I have a batch file that I'd like to compare the contents of two text files (test1.txt and test2.txt). I'd like it to search through each entry in test1.txt and look for each entry in test2.txt and advise whether the entry has been found or not. I've been searching around for some input, but can't seem to come up with anything helpful.
For example, test1.txt contains:
red
blue
green
orange
and test2.txt contains:
red
blue
orange
Ideally I'd like it to tell me it can't find "green"...
I was thinking it should be something like this:
#ECHO OFF
FOR /F %%A IN (C:\test1.txt) DO (
FIND /I %%A C:\test2.txt
IF %ERRORLEVEL% EQU 0 (ECHO Sring found!) ELSE (ECHO string not found!)
)
... but this doesn't prove to do much for me. Any help would be greatly appreciated!
Your code has two problems:
1) The FIND command requires the search string to be quoted.
2) Your logic is almost correct. The problem is %ERRORLEVEL% is expanded when the line is parsed, and the entire parenthesized DO() block is parsed in one pass. So it always expands to the value that existed before the FOR loop executes - not what you want.
There are a number of simple solutions.
Use delayed expansion
#ECHO OFF
setlocal enableDelayedExpansion
FOR /F %%A IN (C:\test1.txt) DO (
FIND /I "%%A" C:\test2.txt
IF !ERRORLEVEL! EQU 0 (ECHO Sring found^^!) ELSE (ECHO string not found^^!)
)
Use IF ERRORLEVEL (IF ERRORLEVEL 1 is true if errorlevel is 1 or greater)
#ECHO OFF
FOR /F %%A IN (C:\test1.txt) DO (
FIND /I "%%A" C:\test2.txt
IF NOT ERRORLEVEL 1 (ECHO Sring found!) ELSE (ECHO string not found!)
)
Use the && and || conditional operators
#ECHO OFF
FOR /F %%A IN (C:\test1.txt) DO (
FIND /I "%%A" C:\test2.txt && (ECHO Sring found!) || (ECHO string not found!)
)
You can just use findstr with parameter /V to print lines that don't contain a match, and /G to read search strings from a file. As dbenham suggested, it's better also to add the /L option to force a literal search, like this:
findstr /L /V /G:text2.txt text1.txt
This will print all lines in text1.txt that are not present in text2.txt
I like to have a typical "usage:" line in my cmd.exe scripts — if a parameter is missing, user is given simple reminder of how the script is to be used.
The problem is that I can't safely predict whether potential user would use GUI or CLI. If somebody using GUI double-clicks this script in Explorer window, they won't have chance to read anything, unless I pause the window. If they use CLI, pause will bother them.
So I'm looking for a way to detect it.
#echo off
if _%1_==__ (
echo usage: %nx0: filename
rem now pause or not to pause?
)
Is there a nice solution on this?
You can check the value of %CMDCMDLINE% variable. It contains the command that was used to launch cmd.exe.
I prepared a test .bat file:
#Echo Off
echo %CMDCMDLINE%
pause
When run from inside of open cmd.exe window, the script prints "C:\Windows\system32\cmd.exe".
When run by double-clicking, it prints cmd /c ""C:\Users\mbu\Desktop\test.bat" "
So to check if your script was launched by double-clicking you need to check if %cmdcmdline% contains the path to your script. The final solution would look like this:
#echo off
set interactive=1
echo %cmdcmdline% | find /i "%~0" >nul
if not errorlevel 1 set interactive=0
rem now I can use %interactive% anywhere
if _%1_==__ (
echo usage: %~nx0 filename
if _%interactive%_==_0_ pause
)
Edit: implemented fixes for issues changes discussed in comments; edited example to demonstrate them
:: exit if not interactive
echo %CMDCMDLINE% | find /i "/c"
if not ERRORLEVEL 1 goto:eof
Here, I wrote something...
Usage.bat
#echo off
if arg%1==arg goto help
goto action
:action
echo do something...
goto end
:help
set help1=This is Help line 1.
set help2=This is Help line 2.
cmd.exe /k "echo %help1% &echo %help2%"
goto end
:end
It's not perfect, but it works! :D
-joedf
This is only using the internal command. so effectively....
EnableDelayedExpansion
if "!cmdcmdline!" neq "!cmdcmdline:%~f0=!" pause >nul
or
if not "!cmdcmdline!" == "!cmdcmdline:%~f0=!" pause >nul
DisableDelayedExpansion
if "%cmdcmdline%" neq "%cmdcmdline:%~f0=%" pause >nul
or
if not "%cmdcmdline%" == "%cmdcmdline:%~f0=%" pause >nul
Start your batch checking for %WINDIR% in %cmdcmdline% like this:
echo "%cmdcmdline%" | findstr /ic:"%windir%" >nul && (
echo Interactive run of: %0 is not allowed
pause
exit /B 1
)
Please use findstr
echo %cmdcmdline% | findstr /ic:"%~f0" >nul && ( pause >nul )
or
setlocal EnableDelayedExpansion
.
.
echo !cmdcmdline! | findstr /ic:"%~f0" >nul && ( pause >nul )
.
.
endlocal
This is always worked...
for internal command
setlocal EnableDelayedExpansion
set "cmddiff=!cmdcmdline:~0,1!" & if !cmddiff! neq ^" ( pause >nul )
endlocal
or
setlocal EnableDelayedExpansion
set "cmddiff=!cmdcmdline:~28,1!" & if !cmddiff! neq ^" ( pause >nul )
endlocal
You can compare the different thing, but this is only worked within EnableDelayedExpansion. and I don't think that this will be always worked, cause windows version, etc...
Similar approach...
setlocal
set startedFromExplorer=
echo %cmdcmdline% | find /i "cmd.exe /c """"%~0""" >nul
if not errorlevel 1 set startedFromExplorer=1
...
if defined startedFromExplorer pause
goto :EOF
setlocal EnableDelayedExpansion
if "!cmdcmdline!" neq "!cmdcmdline:%comspec%=!" ( pause>nul )
Test is done in Windows 10. Using %windir%, it is a little dangerous or ambiguous. So %comspec% is super safe.