Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a text file. I have to swap odd and even lines.
I made a batch script that writes even lines into testfile2.txt and odd lines into testfile3.txt.
#echo off
setlocal EnableDelayedExpansion
set "filepath1=C:\\Users\\andyb\\Desktop\\testfile.txt"
set "filepath2=C:\\Users\\andyb\\Desktop\\testfile2.txt"
set "filepath3=C:\\Users\\andyb\\Desktop\\testfile3.txt"
set counter=0
set B=0
for /F %%A in (%filepath1%) do (
set /a B=!counter!%%2
if !B! equ 0 (echo %%A>>%filepath2%) else (echo %%A>>%filepath3%)
set /A counter=counter+1
)
And I want to take 1 line from file that contains odd lines, then 1 line from the file with even lines and write it to my first file. But I don't understand how to do it in FOR loop because it reads a line from only one file and I can't work with another file in this loop.
Example of input file:
1a
2b
3c
4d
Example of output file:
2b
1a
4d
3c
Try the following:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem // Define constants here:
set "_FILE=textfile.txt"
rem // Count number of lines:
for /F %%C in ('^< "!_FILE!" find /C /V ""') do set "COUNT=%%C"
rem // Divide by two, round up:
set /A "COUNT=(COUNT+1)/2"
< "!_FILE!" > "!_FILE!.tmp" (
rem // Read files in blocks of two lines:
for /L %%I in (1,1,%COUNT%) do (
set "LINE1=" & set "LINE2="
set /P LINE1=""
set /P LINE2=""
echo(!LINE2!
echo(!LINE1!
)
)
rem // Overwrite original file:
> nul move /Y "!_FILE!.tmp" "!_FILE!"
endlocal
exit /B
There are several solutions for this task.
The first one uses delayed expansion on execution of all lines of batch file exchanging odd and even lines. This means it does not work right for lines with an exclamation in line because ! is removed from line.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SourceFile=%USERPROFILE%\Desktop\TestFile.txt"
if not exist "%SourceFile%" goto EndBatch
set "TargetFile=%USERPROFILE%\Desktop\TestFile2.txt"
del "%TargetFile%" 2>nul
set "LineOdd="
for /F "usebackq delims=" %%I in ("%SourceFile%") do (
if not defined LineOdd (
set "LineOdd=%%I"
) else (
echo %%I>>"%TargetFile%"
echo !LineOdd!>>"%TargetFile%"
set "LineOdd="
)
)
if defined LineOdd echo !LineOdd!>>"%TargetFile%"
move /Y "%TargetFile%" "%SourceFile%"
:EndBatch
endlocal
Blank and empty lines are skipped by FOR and therefore missing in target file. And lines starting with a semicolon ; are ignored on reading each line by FOR and for that reason are missing also in output file. But those limitations should not matter here according to input example.
The limitations of first solution could be avoided using this batch code which is of course much slower:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFile=%USERPROFILE%\Desktop\TestFile.txt"
if not exist "%SourceFile%" goto EndBatch
set "TargetFile=%USERPROFILE%\Desktop\TestFile2.txt"
del "%TargetFile%" 2>nul
set "LineOdd="
for /F "tokens=1* delims=:" %%H in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%SourceFile%"') do (
if not defined LineOdd (
set "LineOdd=_%%I"
) else (
if "%%I" == "" (
echo/>>"%TargetFile%"
) else (
echo %%I>>"%TargetFile%"
)
setlocal EnableDelayedExpansion
if "!LineOdd!" == "_" (
echo/>>"%TargetFile%"
) else (
echo !LineOdd:~1!>>"%TargetFile%"
)
endlocal
set "LineOdd="
)
)
if defined LineOdd (
setlocal EnableDelayedExpansion
if "!LineOdd!" == "_" (
echo/>>"%TargetFile%"
) else (
echo !LineOdd:~1!>>"%TargetFile%"
)
endlocal
)
move /Y "%TargetFile%" "%SourceFile%"
:EndBatch
endlocal
It would be also possible to use hybrid batch file JREPL.BAT written by Dave Benham:
call jrepl.bat "^(.*)\r\n(.*)\r\n" "$2\r\n$1\r\n" /M /X /F "%USERPROFILE%\Desktop\TestFile.txt" /O "%USERPROFILE%\Desktop\TestFile2.txt"
move /Y "%USERPROFILE%\Desktop\TestFile2.txt" "%USERPROFILE%\Desktop\TestFile.txt"
The last line of the file must have a DOS/Windows line termination (carriage return \r and line-feed \n) if being an even line on using this solution.
For understanding the used commands/executables/batch files and how they work, open a command prompt window, execute there the following command lines, and read entirely all help pages displayed for each command/executable/batch file very carefully.
del /?
echo /?
endlocal /?
findstr.exe /?
for /?
goto /?
if /?
jrepl.bat /?
move /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul and >>.
The following example will create two files from testfile.txt, file0.out containing the even lines, and file1.out containing the odd lines.
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Tokens=1* Delims=:" %%A In ('FindStr/N "^" "testfile.txt"') Do (
Set/A "_=%%A%%2"
(Echo(%%B)>>file!_!.out)
Rename the output files according to your requirements.
I think to reinterleave the odd and even versions in reversed order isn't that difficult. Appending to Compo's try:
#Echo Off
SetLocal EnableDelayedExpansion
Set File=testfile
For /F "Tokens=1* Delims=:" %%A In ('FindStr/N "^" "%File%.txt"'
) Do Set/A "_=%%A%%2"&>>%File%_!_!.txt Echo(%%B
<%File%_0.txt (For /f "delims=" %%A in (%File%_1.txt) Do (
Set "B="&Set /P "B="
Echo(!B!
Echo(%%A
)) >%File%-new.txt
Del %File%_*
In case of an uneven total the second last line will be empty. sample Output:
2b
1a
4d
3c
5e
Related
The script I had been working on reads all folders on the CD-ROM drive "i" and searches for mp3 files, copying them onto the destination "e:\MP3\new".
#ECHO off
setlocal enabledelayedexpansion
cd /d "i:\"
set count=1
for /r %%d in (*.mp3) do (
set /a count+=1
)
echo There were %count% files found
set countb=1
for /r %%g in (*.mp3) do (
set /a countb+=1
echo|set /p = File: !countb!/%count%
copy "%%g" "e:\MP3\new" > nul
)
endlocal
Let´s suppose in this example that 115 files were found.
What I get from the above code:
There were 115 files found
File: 1/115 File: 2/115 File: 3/115 File: 4/115 File: 5/115 (...) File: 115/115
What I want:
There were 115 files found
File: X/115 where X will be constantly updated on the screen each time an mp3 file is successfully copied
Any help to fix that?
Firstly, you are initialising your counters wrongly, they should be set to zero (like set count=0) rather than one to get the correct numbers.
Secondly, you should replace echo|set /p = by < nul set /P =, because the pipe (|) is slower than simple (input) redirection (<) since it creates new cmd instances for either side.
To move the cursor in the Command Prompt window back to the beginning of the current line, you need to write the carriage-return character first. However, you cannot use this character as the first one with set /P, because it is going to be removed, together with other leading white-space characters. So you need another invisible character preceding the carriage-return that is not going to be removed; let us choose the back-space character.
Therefore, the fixed code may look like this, for instance:
#echo off
setlocal EnableDelayedExpansion
rem // Gather the back-space character:
for /F %%B in ('prompt $H ^& for %%Z in ^(.^) do rem') do set "BS=%%B"
rem // Gather the carriage-return character:
for /F %%C in ('copy /Z "%~f0" nul') do set "CR=%%C"
cd /D "I:\"
set /A "count=0"
for /R %%d in ("*.mp3") do (
set /A "count+=1"
)
echo There were %count% files found.
set /A "index=0"
for /R %%g in ("*.mp3") do (
set /A "index+=1"
< nul set /P ="%BS%!CR!File: !index!/%count%"
copy "%%~g" "E:\MP3\new\" > nul
)
endlocal
Need to delete the first 5 lines and the last 2 lines of a txt file using a bat, been trying with a script that works on win 10 but not on win server 2012 r2.
Any idea why?
heres the script, i have
#echo off
setlocal EnableDelayedExpansion
set "FileToModify=getdoc.txt"
if not exist "%FileToModify%" goto:leavenow
set "TempFile=%TEMP%\FileUpdate.txt"
if exist "%TempFile%" del "%TempFile%"
if exist newFile.txt del newFile.txt
if exist Testfinal.txt del Testfinal.txt
if exist Test1.txt del Test1.txt
if exist myOriginalFile.txt del myOriginalFile.txt
set "Line=0"
for /F "useback delims=" %%L in ("%FileToModify%") do (
if !Line! GTR 4 (
echo %%L>>"%TempFile%"
) else (
set /A Line+=1
if !Line! GTR 4 echo %%L>>"%TempFile%"
)
)
copy /Y "%TempFile%" "%FileToModify%" >nul
set row=
for /F "delims=" %%j in (%FileToModify%) do (
if defined row echo.!row!>>Test1.txt
set row=%%j
)
set row=
for /F "delims=" %%j in (Test1.txt) do (
if defined row echo.!row!> Testfinal.txt
set row=%%j
)
echo { > newFile.txt
type %FileToModify% >> newFile.txt
type newFile.txt > getdoc.txt
del FileUpdate.txt
del newFile.txt
del Testfinal.txt
del Test1.txt
:leavenow
echo no file to clean!!!!
pause
exit
Ps- im sure there is a simple way to do this thanks
I suggest to use the following commented batch file for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileToModify=getdoc.txt"
if not exist "%FileToModify%" (
set "ErrorMessage=No file "%FileToModify%" to clean"
goto ErrorOutput
)
rem Get number of lines in file to modify by searching with FINDSTR for begin
rem of line which is true for every line in the file. Output by FINDSTR is the
rem line number a colon and the line itself for each line in file. For interest
rem is here just the line number left to first colon from last output line.
for /F "delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%FileToModify%"') do set "LastLineNumber=%%I"
rem Has the file at least 8 lines to remove five lines at top and two
rem lines at bottom and have finally a file with at least one line?
if %LastLineNumber% LSS 8 (
set "ErrorMessage=File "%FileToModify%" has less than 8 lines"
goto ErrorOutput
)
rem Decrement the number of last line because the
rem last two lines should be removed from the file.
set /A LastLineNumber-=1
rem Derive name of temporary file to create in folder for temporary files
rem from name of file to modify by replacing its file extension by "tmp".
for %%I in ("%FileToModify%") do set "TempFile=%TEMP%\%%~nI.tmp"
rem Make sure the temporary file does not already exist from a previous
rem run of this batch file broke during processing by command interpreter.
del "%TempFile%" 2>nul
for /F "skip=5 tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%FileToModify%"') do (
if %%I == %LastLineNumber% goto ReplaceFile
echo/%%J>>"%TempFile%"
)
:ReplaceFile
move /Y "%TempFile%" "%FileToModify%" 2>nul
if errorlevel 1 (
del "%TempFile%"
set "ErrorMessage=File "%FileToModify%" is write-protected"
goto ErrorOutput
)
endlocal
exit /B
:ErrorOutput
cls
echo %~f0
echo/
echo Error: %ErrorMessage%.
echo/
pause
Note: This batch file does not produce the right output for a file containing lines starting with 1 or more : because on second FOR option delims=: is used which results in splitting up such lines in line number output by FINDSTR assigned to loop variable I and everything after 1 or more colons assigned to loop variable J being the next character in ASCII table after specified loop variable I.
But this batch file handles files containing command line critical characters like &()[]{}^=;!'+,`~|<> well.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~f0 (name of batch file with file extension and full path).
cls /?
del /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
goto /?
if /?
move /?
pause /?
rem /?
set /?
setlocal /?
See DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ to understand why echo/ is used instead of echo. in batch file.
In below code i am tring to fetch the line no of string "AXX0000XXXA" from file data.txt,then fetching line by line and printing target.txt file,in between if the line reach the find line no i am adding one more line from file temp.txt.The code is working fine with the less nos of records(tested with 150 lines-File Size 100 kb),but when i am processing with 50K records(File Size 25MB) it is taking more then 25 minutes to process.could you please help me how i will process same in less time.
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('findstr /n "AXX0000XXXA" "C:\Users\23456\Desktop\data.txt"') do (set find_line=%%a)
set /a counter=0
for /f "usebackq delims=" %%b in (`"findstr /n ^^ C:\Users\23456\Desktop\data.txt"`) do (
set curr_line=%%b
set /a counter=!counter!+1
if !counter! equ !find_line! (
type temp.txt >> target.txt
)
call :print_line curr_line
)
endlocal
:print_line
setlocal enabledelayedexpansion
set line=!%1!
set line=!line:*:=!
echo !line!>>target.txt
endlocal
Your code uses three Batch file constructs that are inherently slow: call command, >> append redirection and setlocal/endlocal, and these constructs are executed once per each file line! It would be faster to include the subroutine into the original code to avoid the call and setlocal commands, and an echo !line!>>target.txt command imply open the file, search for the end, append the data and close the file, so it is faster to use this construct: (for ...) > target.txt that just open the file once. An example of a code with such changes is in Compo's answer.
This is another method to solve this problem that may run faster when the search line is placed towards the beginning of the file:
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('findstr /n "AXX0000XXXA" "C:\Users\23456\Desktop\data.txt"') do (set /A find_line=%%a-1)
call :processFile < "C:\Users\23456\Desktop\data.txt" > target.txt
goto :EOF
:processFile
rem Duplicate the first %find_line%-1 lines
for /L %%i in (1,1,%find_line%) do (
set /P "line="
echo !line!
)
rem Insert the additional line
type temp.txt
rem Copy the rest of lines
findstr ^^
exit /B
This should create target.txt with content matching data.txt except for an inserted line taken from tmp.txt immediately above the line matching the search string, AXX0000XXXA.
#Echo Off
Set "fSrc=C:\Users\23456\Desktop\data.txt"
Set "iSrc=temp.txt"
Set "sStr=AXX0000XXXA"
Set "fDst=target.txt"
Set "iStr="
Set/P "iStr="<"%iSrc%" 2>Nul
If Not Defined iStr Exit/B
Set "nStr="
For /F "Delims=:" %%A In ('FindStr/N "%sStr%" "%fSrc%" 2^>Nul') Do Set "nStr=%%A"
If Not Defined nStr Exit/B
( For /F "Tokens=1*Delims=:" %%A In ('FindStr/N "^" "%fSrc%"') Do (
If "%%A"=="%nStr%" Echo %iStr%
Echo %%B))>"%fDst%"
I have made it easy for you to change your variable data, you only need to alter lines 3-6.
I have assumed that this was your intention, your question was not clear, please accept my apologies if I have assumed incorrectly.
So I'm building a messaging program in batch (I know, it's newbish) and the program takes user input, puts it in my .txt file log.txt, and types it on the screen. I want the output to look like this...
Title
----------------------
contents
of
the
file
here
----------------------
User input here>>
This may seem simple, but the file will be constantly updated by users and I want the program to only display a range of lines to keep that message area stays the same size. I found a simple program to display specific lines, but I can't make them move down one line each time log.txt is changed. Here it is:
#setlocal enableextensions enabledelayedexpansion
#echo off
set lines=1
set curr=1
for /f "delims=" %%a in ('type bob.txt') do (
for %%b in (!lines!) do (
if !curr!==%%b echo %%a
)
set /a "curr = curr + 1"
)
endlocal
(By the way, this program is called lines.bat. I just call it in cmd to test it.)
To return a defined number of lines starting from a certain line number, you can do the following:
#echo off
setlocal EnableExtensions
rem define the (path to the) text file here:
set "TEXT_FILE=log.txt"
rem define the line number here:
set /A "LINE_NUMBER=1"
rem define the number of lines here:
set /A "LINE_COUNT=5"
set /A "LINE_LIMIT=LINE_NUMBER+LINE_COUNT-1"
for /F delims^=^ eol^= %%L in ('findstr /N /R "^" "%TEXT_FILE%"') do (
setlocal DisableDelayedExpansion
set "LINE=%%L"
setlocal EnableDelayedExpansion
for /F "tokens=1 delims=:" %%N in ("!LINE!") do set "LNUM=%%N"
set "LINE=!LINE:*:=!"
if !LNUM! GEQ !LINE_NUMBER! (
if !LNUM! LEQ !LINE_LIMIT! (
echo !LINE!
)
)
endlocal
endlocal
)
endlocal
The findstr command with the /R "^" search pattern returns all lines. The findstr switch /N lets every line precede with a line number (starting from 1) and a colon. The : is used to split the line in two parts: the first part representing the line number is checked whether it is in the range to be returned; the second part is the original line of text which is simply output in case. Even empty lines are taken into account.
You might ask why not simply using the above mentioned : as a delims delimiter option for for /F, but this would cause problems with lines of text starting with :.
The toggling of delayed expansion is necessary to avoid trouble with special characters like !, for instance.
To return the last defined number of lines, the following approach can be used:
#echo off
setlocal EnableExtensions
rem define the (path to the) text file here:
set "TEXT_FILE=log.txt"
rem define the number of lines here:
set /A "LINE_COUNT=5"
for /F "tokens=1 delims=:" %%L in ('findstr /N /R "^" "%TEXT_FILE%"') do (
set /A "LINE_SKIP=%%L"
)
set /A "LINE_SKIP-=LINE_COUNT"
if %LINE_SKIP% GTR 0 (
set "LINE_SKIP=skip^=%LINE_SKIP%^ "
) else (
set "LINE_SKIP="
)
for /F %LINE_SKIP%delims^=^ eol^= %%L in ('findstr /N /R "^" "%TEXT_FILE%"') do (
setlocal DisableDelayedExpansion
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "LINE=!LINE:*:=!"
echo !LINE!
endlocal
endlocal
)
endlocal
Again, the findstr /N /R "^" command is used. But here, we have an additional for /F loop first, which merely counts the number of lines in the text file, extracting the line number preceded by findstr. The second for /F loop is quite similar to the above approach, but a dynamic skip option is introduced, so that the loop starts iterating through the last lines only; the rest is almost the same as above, except that the conditions concerning the current line number have been removed.
I know I could do the counting of lines also by using find /C /V "" rather than looping through the findstr /N /R "^" output, but if there are one or more empty lines at the end of the file, find returns a number one less as the findstr method, so I went for findstr consistently.
Also here, delayed expansion is toggled to avoid trouble with the ! character.
Is it possible to remove duplicate rows from a text file? If yes, how?
Sure can, but like most text file processing with batch, it is not pretty, and it is not particularly fast.
This solution ignores case when looking for duplicates, and it sorts the lines. The name of the file is passed in as the 1st and only argument to the batch script.
#echo off
setlocal disableDelayedExpansion
set "file=%~1"
set "sorted=%file%.sorted"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^
::The 2 blank lines above are critical, do not remove
sort "%file%" >"%sorted%"
>"%deduped%" (
set "prev="
for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%sorted%") do (
set "ln=%%A"
setlocal enableDelayedExpansion
if /i "!ln!" neq "!prev!" (
endlocal
(echo %%A)
set "prev=%%A"
) else endlocal
)
)
>nul move /y "%deduped%" "%file%"
del "%sorted%"
This solution is case sensitive and it leaves the lines in the original order (except for duplicates of course). Again the name of the file is passed in as the 1st and only argument.
#echo off
setlocal disableDelayedExpansion
set "file=%~1"
set "line=%file%.line"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^
::The 2 blank lines above are critical, do not remove
>"%deduped%" (
for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%file%") do (
set "ln=%%A"
setlocal enableDelayedExpansion
>"%line%" (echo !ln:\=\\!)
>nul findstr /xlg:"%line%" "%deduped%" || (echo !ln!)
endlocal
)
)
>nul move /y "%deduped%" "%file%"
2>nul del "%line%"
EDIT
Both solutions above strip blank lines. I didn't think blank lines were worth preserving when talking about distinct values.
I've modified both solutions to disable the FOR /F "EOL" option so that all non-blank lines are preserved, regardless what the 1st character is. The modified code sets the EOL option to a linefeed character.
New solution 2016-04-13: JSORT.BAT
You can use my JSORT.BAT hybrid JScript/batch utility to efficiently sort and remove duplicate lines with a simple one liner (plus a MOVE to overwrite the original file with the final result). JSORT is pure script that runs natively on any Windows machine from XP onward.
#jsort file.txt /u >file.txt.new
#move /y file.txt.new file.txt >nul
you may use uniq http://en.wikipedia.org/wiki/Uniq from UnxUtils http://sourceforge.net/projects/unxutils/
Some time ago I found an unexpectly simple solution, but this unfortunately only works on Windows 10: the sort command features some undocumented options that can be adopted:
/UNIQ[UE] to output only unique lines;
/C[ASE_SENSITIVE] to sort case-sensitively;
So use the following line of code to remove duplicate lines (remove /C to do that in a case-insensitive manner):
sort /C /UNIQUE "incoming.txt" /O "outgoing.txt"
This removes duplicate lines from the text in incoming.txt and provides the result in outgoing.txt. Regard that the original order is of course not going to be preserved (because, well, this is the main purpose of sort).
However, you sould use these options with care as there might be some (un)known issues with them, because there is possibly a good reason for them not to be documented (so far).
The Batch file below do what you want:
#echo off
setlocal EnableDelayedExpansion
set "prevLine="
for /F "delims=" %%a in (theFile.txt) do (
if "%%a" neq "!prevLine!" (
echo %%a
set "prevLine=%%a"
)
)
If you need a more efficient method, try this Batch-JScript hybrid script that is developed as a filter, that is, similar to Unix uniq program. Save it with .bat extension, like uniq.bat:
#if (#CodeSection == #Batch) #then
#CScript //nologo //E:JScript "%~F0" & goto :EOF
#end
var line, prevLine = "";
while ( ! WScript.Stdin.AtEndOfStream ) {
line = WScript.Stdin.ReadLine();
if ( line != prevLine ) {
WScript.Stdout.WriteLine(line);
prevLine = line;
}
}
Both programs were copied from this post.
set "file=%CD%\%1"
sort "%file%">"%file%.sorted"
del /q "%file%"
FOR /F "tokens=*" %%A IN (%file%.sorted) DO (
SETLOCAL EnableDelayedExpansion
if not [%%A]==[!LN!] (
set "ln=%%A"
echo %%A>>"%file%"
)
)
ENDLOCAL
del /q "%file%.sorted"
This should work exactly the same. That dbenham example seemed way too hardcore for me, so, tested my own solution. usage ex.: filedup.cmd filename.ext
Pure batch - 3 effective lines.
#ECHO OFF
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
FOR /f "delims=" %%a IN (q34223624.txt) DO SET $%%a=Y
(FOR /F "delims=$=" %%a In ('set $ 2^>Nul') DO ECHO %%a)>u:\resultfile.txt
GOTO :EOF
Works happily if the data does not contain characters to which batch has a sensitivity.
"q34223624.txt" because question 34223624 contained this data
1.1.1.1
1.1.1.1
1.1.1.1
1.2.1.2
1.2.1.2
1.2.1.2
1.3.1.3
1.3.1.3
1.3.1.3
on which it works perfectly.
Did come across this issue and had to resolve it myself because the use was particulate to my need.
I needed to find duplicate URL's and order of lines was relevant so it needed to be preserved. The lines of text should not contain any double quotes, should not be very long and sorting cannot be used.
Thus I did this:
setlocal enabledelayedexpansion
type nul>unique.txt
for /F "tokens=*" %%i in (list.txt) do (
find "%%i" unique.txt 1>nul
if !errorlevel! NEQ 0 (
echo %%i>>unique.txt
)
)
Auxiliary: if the text does contain double quotes then the FIND needs to use a filtered set variable as described in this post: Escape double quotes in parameter
So instead of:
find "%%i" unique.txt 1>nul
it would be more like:
set test=%%i
set test=!test:"=""!
find "!test!" unique.txt 1>nul
Thus find will look like find """what""" file and %%i will be unchanged.
I have used a fake "array" to accomplish this
#echo off
:: filter out all duplicate ip addresses
REM you file would take place of %1
set file=%1%
if [%1]==[] goto :EOF
setlocal EnableDelayedExpansion
set size=0
set cond=false
set max=0
for /F %%a IN ('type %file%') do (
if [!size!]==[0] (
set cond=true
set /a size="size+1"
set arr[!size!]=%%a
) ELSE (
call :inner
if [!cond!]==[true] (
set /a size="size+1"
set arr[!size!]=%%a&& ECHO > NUL
)
)
)
break> %file%
:: destroys old output
for /L %%b in (1,1,!size!) do echo !arr[%%b]!>> %file%
endlocal
goto :eof
:inner
for /L %%b in (1,1,!size!) do (
if "%%a" neq "!arr[%%b]!" (set cond=true) ELSE (set cond=false&&goto :break)
)
:break
the use of the label for the inner loop is something specific to cmd.exe and is the only way I have been successful nesting for loops within each other. Basically this compares each new value that is being passed as a delimiter and if there is no match then the program will add the value into memory. When it is done it will destroy the target files contents and replace them with the unique strings