Find & Replace string using for /f with if statement and variables - batch-file

I have written a batch file which I want to overwrite key strings with strings from another .txt file.
currently it copies the new File.txt file perfectly but does not replace the strings with the strings from OldFile.txt file.
example of strings in File.txt file:
...
# Password
Pword=
# AccountName
Account=
# TownName
Town=
# Postcode
Postcode=
# LocationChangedDate
LocationChanged=
example of strings in OldFile.txt file I want to replace from:
...
# Password
Pword=ABC
# AccountName
Account=123
# TownName
Town=LDN
# Postcode
Postcode=WS77TP
# LocationChangedDate
LocationChanged=01/01/2015
Can someone please point me in the right direction or explain where I have made a mistake?
#echo off
setlocal disableDelayedExpansion
::Variables
set InputFile=F:\EXCHANGE\3\Machine\File.txt
set OutputFile=F:\EXCHANGE\3\File-New.txt
set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt
set _strFindPword=Pword=.*
for /F "delims=" %%A in ('findstr /x "Pword=.*" %CopyFile%') do set _strInsertPword=%%A
echo.%_strInsertPword%
set _strFindAccount=Account=.*
for /F "delims=" %%B in ('findstr /x "Account=.*" %CopyFile%') do set _strInsertAccount=%%B
echo.%_strInsertAccount%
set _strFindTown=Town=.*
for /F "delims=" %%C in ('findstr /x "Town=.*" %CopyFile%') do set _strInsertTown=%%C
echo.%_strInsertTown%
set _strFindLocationChanged=LocationChanged=.*
for /F "delims=" %%D in ('findstr /x "LocationChanged=.*" %CopyFile%') do set _strInsertLocationChanged=%%D
echo.%_strInsertLocationChanged%
set _strFindPostcode=Postcode=.*
for /F "delims=" %%E in ('findstr /x "Postcode=.*" %CopyFile%') do set _strInsertPostcode=%%E
echo.%_strInsertPostcode%
(
for /F "delims=" %%L in ('findstr /n "^" "%InputFile%"') do (
set "line=%%L"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if "%%L" equ "_strFindPword" (echo.!_strInsertPword!) else (
if "%%L" equ "%_strFindAccount%" (echo.!_strInsertAccount!) else (
if "%%L" equ "%_strFindTown%" (echo.!_strInsertTown!) else (
if "%%L" equ "%_strFindLocationChanged%" (echo.!_strInsertLocationChanged!) else (
if "%%L" equ "%_strFindPostcode%" (echo.!_strInsertPostcode!) else (echo.!line!)
)
)
)
)
endlocal
)
) > "%OutputFile%"
del %InputFile%
ren %OutputFile% File.txt
pause

I think I finally got it...
What it does:
It goes through the OldFile.txt content, searching for markers, if found they are stored into environment variables to be used in the nest step (e.g. for _PWD marker (variable) which has a value of Pword=, it will create a _PWDCONTENTS variable with the content of Pword=ABC).
It goes through File.txt content, searching for the same markers, if one marker found, the corresponding CONTENTS variable is dumped in the OutFile.txt, else the original line. Because that happens in the inner for loop, I had to add some extra logic (the _WROTE var) to avoid writing the same lines more than once.
Notes:
It is supposed (well, besides doing what it's supposed to) to be "configurable" (the code is complicated, it's heading towards meta :) if you will), meaning that if there are changes between the markers the code shouldn't change (well there would be code changes, but not in the functional part only in variable definitions). Let me detail:
If you no longer need to replace the Town= string, then all you have to do is removing _TOWN from _ALL: set _ALL=_PWD _ACCT _POST _LOC.
The reverse: if you want to add some other tag (let's call it Name), you have to create a new environment variable: set _NAME=Name= and add it to _ALL: set _ALL=_PWD _ACCT _TOWN _POST _LOC _NAME.
As an indirect consequence, I didn't focus on performance, so it might run slow. Anyway I tried to keep the disk accesses (which are painfully slow) to a minimum (one example is when having 2 for loops the one that iterates on a file contents - assuming that each iteration takes a disk access; this might not be true, and Win has IO buffering - it's the outer one).
I "commented" out the last line in the file, to avoid overwriting the original file. If that behavior is needed, simply remove the rem at the beginning.
Here's the batch code:
#echo off
setlocal enabledelayedexpansion
set _INFILE="File.txt"
set _OUTFILE="NewFile.txt"
set _OLDFILE="OldFile.txt"
set _PWD=Pword=
set _ACCT=Account=
set _TOWN=Town=
set _POST=Postcode=
set _LOC=LocationChanged=
set _ALL=_PWD _ACCT _TOWN _POST _LOC
echo Parsing old file contents...
for /f "tokens=*" %%f in ('type !_OLDFILE!') do (
for %%g in (!_ALL!) do (
echo %%f | findstr /b /c:!%%g! 1>nul
if "!errorlevel!" equ "0" (
set %%gCONTENTS=%%f
)
)
)
copy nul %_OUTFILE%
echo Merging the old file contents into the new file...
set _WROTE=0
for /f "tokens=*" %%f in ('findstr /n "^^" !_INFILE!') do (
set _TMPVAR0=%%f
set _TMPVAR0=!_TMPVAR0:*:=!
for %%g in (!_ALL!) do (
echo !_TMPVAR0! | findstr /b /c:!%%g! 1>nul
if "!errorlevel!" equ "0" (
echo.!%%gCONTENTS!>>!_OUTFILE!
set _WROTE=1
)
)
if "!_WROTE!" equ "0" (
echo.!_TMPVAR0!>>!_OUTFILE!
) else (
set _WROTE=0
)
)
rem copy /-y %_OUTFILE% %_INFILE%
#EDIT0: Using #StevoStephenson suggestion (as part of the question snippet), I replaced the (2nd) outer for loop to ('findstr /n "^^" !_INFILE!') in order to include the empty lines, so the 3rd remark no longer applies (deleting). Also did some small changes to allow files that contain SPACE s in their paths.

Maybe it works like this
set CopyFile=oldfile.txt
set InputFile=newfile.txt
set str_search="Pword"
for /f "delims=" %%i in ('findstr %str_search% %copyfile%') do set str_replace=%%i
set str_replace="%str_replace%"
echo %str_search%
echo %str_replace%
pause
CALL :far %InputFile% %str_search% %str_replace%
EXIT /B 0
:far
setlocal enableextensions disabledelayedexpansion
set "search=%2"
set "replace=%3"
::remove quotes
set search=%search:"=%
set replace=%replace:"=%
echo %search%
echo %replace%
set "textFile=%1"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
EXIT /B 0
At for /f "delims=" %%i in ('findstr %str_search% %copyfile%') do set str_replace=%%i you write the line with the variable that has the needed info to str_replace.
After that you the program calls an embeded find-and-replace-function (:far) whitch i shemelessly stole from Batch script to find and replace a string in text file without creating an extra output file for storing the modified file
This function finds the string "Pword" and replaces it by the line find in the old file.
Attention:
This doesn't solve your problem completely since your new file has to be s.th like this.
#Password
Pword
so if you loose the = it works otherwise it doesn't. I hope this helps you with your problem.

It's not perfect but this may be okay for you:
#Echo Off
Setlocal EnableExtensions DisableDelayedExpansion
(Set InputFile=F:\EXCHANGE\3\Machine\File.txt)
(Set OutputFile=F:\EXCHANGE\3\File-New.txt)
(Set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt)
For /F "Delims=" %%I In (
'FindStr/B "Pword= Account= Town= LocationChanged= Postcode=" "%CopyFile%"'
) Do Set %%I
(For /F "Tokens=1-2* Delims=]=" %%I In ('Find /V /N ""^<"%InputFile%"') Do (
Echo(%%J|FindStr/B # || (If Defined %%J (Call Echo=%%J=%%%%J%%) Else (
If "%%J" NEq "" (Echo=%%J=%%K) Else (Echo=)))))>%OutputFile%
Timeout -1
EndLocal
Exit/B
I've left the delete and rename for you to add at the end.

This solution should be much faster than the other solutions.
It will also preserve empty lines and lines containing ! and ^.
It only needs one findstr call for collecting the old values for all words.
A second findstr determines all lines (by line number) in the infile which needs an update.
#echo off
setlocal EnableDelayedExpansion
set "_INFILE=File.txt"
set "_OUTFILE=NewFile.txt"
set "_OLDFILE="OldFile.txt"
set "_WORDS=Pword= Account= Town= Postcode= LocationChanged="
REM *** get all values for the key words
for /F "tokens=1,* delims==" %%L in ('findstr "!_WORDS!" "!_OLDFILE!"') do (
for /F %%S in ("%%L") do (
set "word[%%S]=%%M"
)
)
REM *** Find all lines which needs an update
set wordIdx=0
for /F "tokens=1,2,* delims=:= " %%1 in ('findstr /n "!_WORDS!" "!_INFILE!"') do (
set "lines[!wordIdx!].line=%%1"
set "lines[!wordIdx!].word=%%2"
set "replace=!word[%%2]!"
set "lines[!wordIdx!].replace=!replace!"
set /a wordIdx+=1
)
REM *** copy the infile to the outfile
REM *** Replace only the lines which are marked by line numbers
echo Parsing old file contents...
set nextWordIdx=0
set /a searchLine=lines[!nextWordIdx!].line
set lineNo=0
setlocal DisableDelayedExpansion
(
for /f "tokens=*" %%L in ('findstr /n "^" "%_INFILE%"') do (
set "line=%%L"
set /a lineNo+=1
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if !lineNo! equ !searchLine! (
(echo(!line!!lines[0].replace!)
set /a nextWordIdx+=1
for /F %%R in ("!nextWordIdx!") do (
endlocal
set /a nextWordIdx=%%R
set /a searchLine=lines[%%R].line
)
) ELSE (
(echo(!line!)
endlocal
)
)
) > "!_OUTFILE!"

Related

IS this possible to modify multiple variable from same file using a .batch file without effecting other things like space, special char?

I have a file rev.ini having multiple variable to update:
s1=10
s2=20
s3=30
Here I am using separate loop for finding string in a file. there are 3 times loop are running for same file. Is it possible to find these three string in same loop?
#Echo Off
cd /d D:\xyz
setlocal enabledelayedexpansion
set s1=10
set s2=11
set s3=12
set "file=rev.ini"
for /F "tokens=1,* delims==" %%i in ('findstr "s1= " rev.ini') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set sequence=%s1%
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
for /F "tokens=1,* delims==" %%i in ('findstr "s2= " rev.ini') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set sequence=%s2%
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
for /F "tokens=1,* delims==" %%i in ('findstr "s3= " rev.ini') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set sequence=%s3%
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
Goto :EOF
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74276740.txt"
SET "outfile=outfile.txt"
set "s1=10"
set "s2=11"
set "s3=12"
REM (
FOR /f "usebackqtokens=1*delims==" %%b IN ("%filename1%") DO (
SET "#keep=Y"
FOR /f "tokens=1*delims==" %%u IN ('set') DO (IF /i "%%b"=="%%u" SET "#keep="&ECHO %%b=%%v)
IF DEFINED #keep IF "%%c"=="" (ECHO %%b) ELSE (ECHO %%b=%%c)
)
REM )>"%outfile%"
GOTO :EOF
Always verify against a test directory before applying to real data.
Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.
Simply read each line. If the line contains = and the part before the = contains any variablename in the environment (I made it case-insensitive with /i) then generate a line using the matching value. If #keep remains set after the for..%%u has been executed, then there is no match, so either reproduce the original x=y or the original line, if it had no =.
The output file can be generated by removing the rem before the ( and ).
The output file must be different from the input file, and should be moved over the input file when the batch finishes (not shown)
--- After comment ---
So, if you follow the instructions provided in the last two paragraphs, you get
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74276740.txt"
SET "outfile=outfile.txt"
set "s1=10"
set "s2=11"
set "s3=12"
(
FOR /f "usebackqtokens=1*delims==" %%b IN ("%filename1%") DO (
SET "#keep=Y"
FOR /f "tokens=1*delims==" %%u IN ('set') DO (IF /i "%%b"=="%%u" SET "#keep="&ECHO %%b=%%v)
IF DEFINED #keep IF "%%c"=="" (ECHO %%b) ELSE (ECHO %%b=%%c)
)
)>"%outfile%"
move "%outfile%" "%filename1%"
GOTO :EOF
== revision of processing section in light of new requirement to retain empty lines ==
REM (
FOR /f "tokens=1,2*delims=:=" %%g IN ('findstr /n ^^^^ "%filename1%"') DO IF "%%h"=="" (ECHO.) ELSE (
SET "#keep=Y"
FOR /f "tokens=1*delims==" %%u IN ('set') DO (IF /i "%%h"=="%%u" SET "#keep="&ECHO %%h=%%v)
IF DEFINED #keep IF "%%i"=="" (ECHO %%h) ELSE (ECHO %%h=%%i)
)
REM )>"%outfile%"
Once again, the rem keywords need to be removed to output to the nominated file.
The changes simply feed the file through a findstr command which produces a listing of the file with a prefix of linenumber:. The extra : in the delims causes the line number to be parsed to %%g. I've changed the metavariable letters because three are required for the processing, and I prefer to not use letters that are also modifiers.
If findstr produces only a line number then it was a blank line, so %%h will be empty and we simply produce an empty line, otherwise, processing as before.

How to search and replace strings that contain "=" characters in text file with Windows CMD (not powershell)

I wrote this code based on some other examples but just can't get it to work? (it's a .bat file)? The code writes the new file with all the old lines just won't edit the three lines right with the "=" character. Can someone point me in the right direction please.
This is what the INTOUCH.INI file looks like to start:
[InTouch]
AppMode=2
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New InTouch application
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
WinFullScreen=1
WinLeft=-4
WinTop=-4
WinWidth=1032
WinHeight=748
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
And this is what is written:
[InTouch]
AppMode=2
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New InTouch application
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
1=WinFullScreen=0=1
WinLeft=-4
WinTop=-4
1032=WinWidth=1000=1032
748=WinHeight=700=748
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
This is my .bat file code:
Set "OldString1=WinFullScreen=1"
Set "NewString1=WinFullScreen=0"
Set "OldString2=WinWidth=1032"
Set "NewString2=WinWidth=1000"
Set "OldString3=WinHeight=748"
Set "NewString3=WinHeight=700"
#ECHO OFF &SETLOCAL
cd /d F:\
for %%x in (INTOUCH.INI) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1_edited%~x1"
(for /f "skip=2 delims=:" %%a in ('find /n /v "" "INTOUCH.INI"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldString1%=%NewString1%!"
set "ln=!ln:%OldString2%=%NewString2%!"
set "ln=!ln:%OldString3%=%NewString3%!"
)
echo(!ln!
endlocal
))>"%outFile%"
Exit /b
If you use the file's format to your advantage you can set the values of the new variables at the top of the script and then as you are reading the variable names from the settings file you can see if those variables are defined. If they are defined then output the new value, otherwise output the original value.
The trick to this is the double variable expansion you get when you use the CALL and ECHO commands together. First the for variable is expanded the name of the variable and then in the second phase of expansion the value of the variable is then expanded. That is the reason for the extra sets of percent symbols.
#echo off
Set "WinFullScreen=0"
Set "WinWidth=1000"
Set "WinHeight=700"
REM cd /d F:\
for %%F in (INTOUCH.INI) do set "outFile=%%~nF_edited%%~xF"
REM Read first line of file
set /p line1=<INTOUCH.INI
(echo %line1%
for /f "usebackq skip=1 tokens=1,2 delims==" %%G in ("INTOUCH.INI") do (
if defined %%G (
CALL echo %%G=%%%%G%%
) else (
echo %%G=%%H
)
))>"%outFile%"
Exit /b
#echo off
Set "AppMode=x"
Set "WinFullScreen=0"
Set "WinWidth=1000"
Set "WinHeight=700"
for /f "skip=1 usebackq tokens=1,2 delims==" %%G in ("a.INI") do call :proc "%%G" %%H
exit /b
:proc
set val=%2
for /F "tokens=* eol= " %%S in ("%~1") do set trimmed=%%S
call :getoverrideval %trimmed%
if "%override%" == "" (
echo %~1=%2%
) else (
echo %~1=%override%
)
goto :EOF
:getoverrideval
call set override=%%%1%%
Output:
C:\Users\w16coreeval>cmd /c a.bat
AppMode=x
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
WinFullScreen=0
WinLeft=-4
WinTop=-4
WinWidth=1000
WinHeight=700
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0

How to change parametes in notes.ini file with batch file

I have a notes.ini file with the following starting line:
[Notes]
Directory=c:\lotus\notes\data
I would like to change the "Directory=" parameter to Directory = D:\Users\fr21466\AppData\Roaming\notes
I´m trying this code:
#echo off
set file=c:\lotus\notes\notes.ini
set newline=Directory=%appdata%\notes
set insertline=2
set output=%appdata%\notes\notes.ini
(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "%file%"') do (
if "%%~a"=="%insertline%" (
echo %newline%
REM ECHO.%%b
) ELSE (
echo.%%b
)
)) > %output%
But the output file is generating this:
It is generating the file, but cutting the symbol: "[" in the first column
Notes]
Directory=D:\Users\fr21466\AppData\Roaming\notes
Can someone please help me to solve this?
Thank You!
Try ini.bat (the second one, under the Update section).
copy "c:\lotus\notes\notes.ini" "%appdata%\notes"
ini.bat /s Notes /i Directory /v "%appdata%\notes" "%appdata%\notes\notes.ini"
To fix your current script, try replacing find with findstr and delims=:. The /N option with findstr prefaces lines with nn: rather than [nn], so your ini sections won't be clobbered when their surrounding brackets are treated as a successive delimiter.
#echo off & setlocal
set "file=c:\lotus\notes\notes.ini"
set "newline=Directory=%appdata%\notes"
set "insertline=2"
set "output=%appdata%\notes\notes.ini"
(for /f "tokens=1* delims=:" %%a in ('findstr /n /v "##" ^< "%file%"') do (
if "%%~a"=="%insertline%" (
echo(%newline%
REM ECHO.%%b
) ELSE (
echo(%%b
)
)) > "%output%"
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q45036613.txt"
SET "outfile=%destdir%\outfile.txt"
set file=%filename1%
set newline=Directory=%appdata%\notes
REM set insertline=2
set output=%outfile%
SET "notessection="
(FOR /f "delims=" %%x IN (%file%) DO (
for /f "tokens=1* delims==" %%a in ("%%x") do (
IF NOT DEFINED notessection ECHO(%%x
IF DEFINED notessection (
IF /i "%%a"=="Directory" (
ECHO(%newline%
SET "notessection="
) ELSE (
ECHO(%%x
)
)
IF /i "%%a"=="[Notes]" SET "notessection=Y"
)
)) > %output%
GOTO :EOF
I used a file named q45036613.txt containing your data + more for my testing. I've also adjusted the paths to suit my system.
This version doesn't rely on the position of the directory entry; it looks for the [Notes] line and adjusts the directory entry that follows by using the characteristic that if defined interprets the run-time value of a variable. Hence, wait for [Notes] then wait for directory
To fix it remove [ from line 6

batch command to search and output the content between the first and last occurrence of the search key

I have a log file and I have a keyword to search the log file. Now I have to extract all the contents from that log file between the first and the last occurrence of the search key. ie everything in between the first and last occurrence. the findstr command only lists out the lines that are having the search key and not the other contents in between. I need batch commands to do that.
eg.
log.txt
[mave123]sddasnsdaskdasddansnmdmsmdasdasda
[mave123]dfdfdf
fsffasf
safaaf
fasfssfdfdsfdsf
[mave123]dfsfsdfsdfssdfssfd
[mave124]rdfsdfsfsfsf
[mave124]dfdfsdfsfsdfs
now the "findstr mave123 log.txt" will return as below
[mave123]sddasnsdaskdasddansnmdmsmdasdasda
[mave123]dfdfdf
[mave123]dfsfsdfsdfssdfssfd
but I want all the contents between the fist and last occurrences of mave123. ie like this. Can you please help?
[mave123]sddasnsdaskdasddansnmdmsmdasdasda
[mave123]dfdfdf
fsffasf
safaaf
fasfssfdfdsfdsf
[mave123]dfsfsdfsdfssdfssfd
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q35976147.txt"
SET "outfile=%destdir%\outfile.txt"
SET "target=%~1"
IF NOT DEFINED target GOTO :eof
SET "startline="
FOR /f "delims=:" %%a IN ('findstr /N /L /c:"%target%" "%filename1%" ') DO (
IF NOT DEFINED startline SET /a startline=%%a
SET /a endline=%%a
)
IF NOT DEFINED startline ECHO NOT found&GOTO :EOF
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /N /r "." "%filename1%" ') DO (
IF %%a geq %startline% (
IF %%a leq %endline% (ECHO %%b) ELSE (GOTO nextstep)
)
)
)>"%outfile%"
:nextstep
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q35976147.txt containing your data for my testing.
Produces the file defined as %outfile%
The first for prefixes each line number matching the target string with linenumber: then assigns the first line number found to startline and surprisingly the last to endline
The second for repeats the operation, this time outputting the lines in the range selected.
This will suppress any leading : on lines. I haven't tested blank lines.
Revision for "all matching files"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "target=%~1"
IF NOT DEFINED target GOTO :eof
FOR %%j IN ("%sourcedir%\log_*.txt".) DO (
ECHO %%j
SET "startline="
FOR /f "delims=:" %%a IN ('findstr /N /L /c:"%target%" "%%j" ') DO (
IF NOT DEFINED startline SET /a startline=%%a
SET /a endline=%%a
)
IF DEFINED startline (
SETLOCAL enabledelayedexpansion
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /N /r "." "%%j" ') DO (
IF %%a geq !startline! IF %%a leq !endline! (ECHO %%b)
)
ENDLOCAL
)>"%destdir%\%%~nj.out"
) ELSE (
ECHO "%target%" NOT found IN %%j
)
)
GOTO :EOF
Here, %%j receives the name of the file to be processed. The destination file is constructed from the destination directory name, the name part of the source file (%%~nj) and .out A SETLOCAL/ENDLOCAL bracket is established to allow the run-time values of startline and endline to be accessed.
always best to state the complete problem as it often radically changes the approach.

Batch filter duplicate lines and write to a new file (semi-finished)

I have successfully made a script that filters out duplicate lines in a file and saves the results to a variable semi-colon separated (sort of an "array"). I could not find any real good solution to it.
#echo off
setlocal enabledelayedexpansion
rem test.txt contains:
rem 2007-01-01
rem 2007-01-01
rem 2007-01-01
rem 2008-12-12
rem 2007-01-01
rem 2009-06-06
rem ... and so on
set file=test.txt
for /f "Tokens=* Delims=" %%i in ('type %file%') do (
set read=%%i
set read-array=!read-array!;!read!
)
rem removes first trailing ";"
set read-array=!read-array:*;=!
echo !read-array!
for /f "Tokens=* Delims=" %%i in ('type %file%') do (
set dupe=0
rem searches array for the current read line (%%i) and if it does exist, it deletes ALL occurences of it
echo !read-array! | find /i "%%i" >nul && set dupe=1
if ["!dupe!"] EQU ["1"] (
set read-array=!read-array:%%i;=!
set read-array=!read-array:;%%i=!
)
rem searches array for the current read line (%%i) and if it does not exist, it adds it once
echo !read-array! | find /i "%%i" >nul || set read-array=!read-array!;%%i
)
rem results: no duplicates
echo !read-array!
Contents of !read-array! is 2008-12-12;2007-01-01;2009-06-06
I now want to take out each item in the array and write them to a new file, with line breaks after each item. Example:
2008-12-12
2007-01-01
2009-06-06
So this is what I've come up with so far.
The problem I'm having is that the second for-loop doesn't accept the !loop! variable as a token definition when being nested. It does however accept %loop% if it's not nested.
The reason I'm doing it this way is that the !read-array! may have a unknown number of items, therefore I count them as well.
Any ideas?
rem count items in array
set c=0
for %%i in (!read-array!) do set /a c+=1
echo %c% items in array
for /l %%j in (1,1,%c%) do (
set loop=%%j
for /f "Tokens=!loop! Delims=;" %%i in ("!read-array!") do (
echo %%i
rem echo %%i>>%file%
)
)
exit /b
At end of your first section, when contents of !read-array! is 2008-12-12;2007-01-01;2009-06-06, you may directly separate the elements of your "list" with a simple for because the standard separators in Batch files may be, besides spaces, comma, semicolon or equal signs:
for %%i in (%read-array%) do echo %%i
However, may I suggest you a simpler method?
Why not define a "real" array with the subscript value of the lines? This way, several repeated lines will store its value in the same array element. At end, just display the values of the resulting elements:
#echo off
set file=test.txt
for /F "Delims=" %%i in (%file%) do (
set read-array[%%i]=%%i
)
rem del %file%
for /F "Tokens=2 Delims==" %%i in ('set read-array[') do (
echo %%i
rem echo %%i>>%file%
)
EDIT
Alternative solution
There is another method that assemble a list of values separated by semicolon as you proposed. In this case each value is first removed from previous list content and immediately inserted again, so at end of the cycle each value is present just once.
#echo off
setlocal EnableDelayedExpansion
set file=test.txt
for /F "Delims=" %%i in (%file%) do (
set read-array=!read-array:;%%i=!;%%i
)
rem del %file%
for %%i in (%read-array%) do (
echo %%i
rem echo %%i>> %file%
)

Resources