Batch file to output last line of findstr - batch-file

I am trying to find a list of machines in files in folders, and print out the last line of the output only.
#echo off
for /f %%a in (computers.txt) do findstr /xs "%%a" unhealthy.txt
pause
The computers.txt file has a list of 300 machines.
I want that to output only the last line of each instance it finds.
Right now the command displays and outputs all instances of the computer name, not just the tail end. I've tried to use "tail for Windows" but am getting errors as well.
Current output:
2013\10-Oct\28\unhealthy.txt:WIN57505
2013\10-Oct\29\unhealthy.txt:WIN57505
2013\10-Oct\30\unhealthy.txt:WIN57505
2013\10-Oct\31\unhealthy.txt:WIN57505
2013\11-Nov\1\unhealthy.txt:WIN57505
2013\11-Nov\4\unhealthy.txt:WIN57505
2013\11-Nov\5\unhealthy.txt:WIN57505
2013\11-Nov\6\unhealthy.txt:WIN57505
I only want:
2013\11-Nov\6\unhealthy.txt:WIN57505

#echo off
setlocal enableextensions disabledelayedexpansion
for /f %%a in (computers.txt) do (
set "line="
for /f "tokens=*" %%b in ('findstr /xs "%%a" *') do set "line=%%b"
setlocal enabledelayedexpansion
echo(!line!
endlocal
)
pause
endlocal

setLocal enableDelayedExpansion
for /f %%a in (computers.txt) do for /f "tokens=*" %%A in ('findstr /xs "%%a"') do set lastFound=%%A
echo !lastFound!

Related

Batch: split values into separate line with specific pipe delimiter

I'm trying to create a report that will split into next row for each line. My data:
I,have,a,report,to,split,|,into,next,row
my,second,line,should,also,|,split,like,before
then it should be like this:
I,have,a,report,to,split
into,next,row
my,second,line,should,also
split,like,before
I have tried the script:
#echo off
setLocal
for /f "tokens=1 delims=.|" %%a in (input.csv) do echo %%a
for /f "tokens=2 delims=.|" %%a in (input.csv) do echo %%a
but the result is:
I,have,a,report,to,split
my,second,line,should,also
into,next,row
split,like,before
Anyone can help me with this? thanks in advanceā€¦
Your result example is wrong. This is the output from your code:
I,have,a,report,to,split,
my,second,line,should,also,
,into,next,row
,split,like,before
Note the comma at end of line 1 and 2, and at beginning of line 3 and 4
This method works:
#echo off
setlocal EnableDelayedExpansion
rem Define a variable with CR+LF ASCII chars:
for /F %%a in ('copy /Z "%~F0" NUL') do set CRLF=%%a^
%empty line 1/2%
%empty line 2/2%
rem Change each ",|," string by CR+LF characters
for /F "delims=" %%a in (input.csv) do (
set "line=%%a"
for %%N in ("!CRLF!") do echo !line:,^|,=%%~N!
)
Of course, you may also do it in the "traditional" :/ way:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=|" %%a in (input.csv) do (
set "left=%%a"
set "right=%%b"
echo !left:~0,-1!
echo !right:~1!
)

findstr /v removing space and dote not working

I have the below batch-file:
cd /d "T:\R\YOU"
for /r T:\R\YOU %%i in (.) do echo %%~nxi>>D:\MultiThreading\ReadFile.txt
cd /d D:\MultiThreading
rem I want to remove dots and spaces for the file content
findstr /v "." ReadFile.pbd >> 11.txt
findstr /v " " 11.pbd >> 12.txt
pause
I am getting the correct output from read file, however the output of 12.txt is empty, what I am doing wrong?
This is the output of ReadFile.txt file:
YOU
YOU 14.1.33333
YOU 14.1.44444
YOU 14.1.55555
YOU 14.1.44444
I want such output (I want to remove the first line):
YOU14133333
YOU14144444
YOU14155555
YOU14144444
The following code snippet read the file ReadFile.txt skipping the first line, removes spaces and dots from every line, and outputs the result into a file called ReturnFile.txt:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "ReturnFile.txt" (
for /F "usebackq skip=1 delims=" %%L in ("ReadFile.txt") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "LINE=!LINE: =!"
set "LINE=!LINE:.=!"
echo(!LINE!
endlocal
)
)
endlocal
exit /B
#Echo off
For /f "usebackq Tokens=1-10 delims=. " %%A in ("ReadFile.txt"
) Do If "%%B" Neq "" Echo:%%A%%B%%C%%D%%E%%F%%G%%H%%%I%%J
A bit primitive and rather short for answer.

How to remove full path name and add comma for the first two columns created?

I am struggling to edit the below command line to only give the name of the file name instead of the full path name and file. I would also want a comma straight after that and another comma straight after the second column (which is the created date done in do, as its a delimited notepad file.
#echo off
setlocal enabledelayedexpansion
set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source
set _DSTDIR=C:\Users\hyea\Desktop\Testing\Target
for /f "delims=" %%f in ('dir /b /a-d "!_SRCDIR!\*.cos"') do (
type "!_SRCDIR!\header.txt" > "!_DSTDIR!\%%f"
call :handle_file "!_SRCDIR!\%%f" "!_DSTDIR!\%%f"
)
goto :eof
:handle_file
set _TMPVAR0=%~t1
setlocal disabledelayedexpansion
for /f "usebackq delims=" %%g in (`"findstr /n ^^ %1"`) do (
echo %%g
set "_TMPVAR1=%%g"
setlocal enabledelayedexpansion
set "_TMPVAR1=!_TMPVAR1:*:=!"
echo.%~1 !_TMPVAR0: =-! !_TMPVAR1!>> %2
endlocal
)
goto :eof
[untested, since you give no indication of the data involved and present a confused list of the outputs you need]
#echo off
setlocal enabledelayedexpansion
set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source
set _DSTDIR=C:\Users\hyea\Desktop\Testing\Target
for /f "delims=" %%f in ('dir /b /a-d "!_SRCDIR!\*.cos"') do (
type "!_SRCDIR!\header.txt" > "!_DSTDIR!\%%f"
set "_TMPVAR0=%~tf"
set "_tmpvar0=!_tmpvar0: =-!"
for /f "tokens=1*delims=:" %%g in (`findstr /n ^^ ""`) do (
echo(%~nf,!_TMPVAR0!,%%h>> "!_DSTDIR!\%%f"
)
)
goto :eof
which should insert the header then copy each line, including blank lines, prefixing each line with
filenameonly,the filedate/time (with spaces replaced by -),linebody

testing variables from a text list to set value in batch

what I have is a text document full of variables to set and test called test.txt
ab <-example variable
and a batch program that pulls from the list
for /f %%a in ('type test.txt') do set %%a=0
for /f %%a in ('type test.txt') do echo %%%a%
expected output
0
actual output
%ab%
this is a really stripped down example but that is the problem
I have tried typing
setlocal enabledelayedexpansion
in the command line but that alone dose nothing and
for /f %%a in ('type test.txt') do echo !%%a!
just outputs
!ab!
You have an error:
setlocal enabledelayedexpantion
^ Here!
It should be:
setlocal enabledelayedexpansion
EDIT: Example session added
C:\ type test.txt
ab
C:\ type test.bat
#echo off
setlocal enabledelayedexpansion
for /f %%a in ('type test.txt') do set %%a=0
for /f %%a in ('type test.txt') do echo !%%a!
C:\ test
0
#ECHO OFF
SETLOCAL
for /f %%a in ('type q27262112.txt') do set %%a=0
for /f %%a in ('type q27262112.txt') do echo %%%a%
for /f %%a in ('type q27262112.txt') do CALL echo %%%%a%%
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%a in ('type q27262112.txt') do set %%a=w
for /f %%a in ('type q27262112.txt') do echo !%%a!
GOTO :EOF
I used a file named q27262112.txt containing your data for my testing.
This produced the following output:
%
0
w
This differs from your results but demonstrates how to retrieve the value of variable ab.
I set the value of ab deliberately in the delayedexpansion instance to some-value-other-than-0 to demonstrate that the value retrieved was indeed being set, not a hangover.

I want to find the word and replace the 3 word after the search string with my desired word in batch script

#ECHO OFF
#setlocal enabledelayedexpansion
SET InFile=test.txt
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
:FindString
SET String=%~1
SET String=%String:*wordA =%
SET String=%String: wordB=`%
FOR /F "tokens=1 delims=`" %%A IN ('%String%') DO ECHO %%A
Here I just written a code to replace the word with my desired character
When I am running these code I am getting the error saying
. was unexpected at this time
You were close. See mods to your code below. If you really want the first word after wordB... this will do it.
Don't enable delayed expansion if you don't need it because that just messes you up if there are ! in the text.
#echo off
SET InFile=test.txt
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
goto :eof
:FindString
SET String=%~1
SET String=%String:*wordA =%
SET String=%String:wordB =%
FOR /F "tokens=1" %%A IN ('echo.%String%') DO ECHO.%%A
:goto :eof
I'm not sure, what you want, but you can try this:
#echo off &setlocal
SET "InFile=test.txt"
echo(wordA wordB>"%InFile%"
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
goto:eof
:FindString
SET "String=%~1"
SET "String=%String:*wordA =%"
SET "String=%String: wordB=`%"
FOR /F "tokens=1 delims=`" %%A IN ("%String%") DO ECHO %%A

Resources