Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:55:03+00:00 2026-05-26T22:55:03+00:00

I have successfully made a script that filters out duplicate lines in a file

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:55:03+00:00Added an answer on May 26, 2026 at 10:55 pm

    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%
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully created an app that reads from a bundled .plist file and
I have successfully made contact with the internet and parsed a few documents via
I have successfully made, committed, and pushed changes to a central git repository. I
I have a categories model made with the fantastic awesome_nested set. I have successfully
I have successfully made a join function, which joins an array to a string
I have successfully made an facebook application which shows real time data from facebook
I have successfully made an application but whenever I try installing it in the
I have successfully made my 4 CRUD actions restful by using mapResources in the
I have a function, 'redirect_to()' written on php script that is called after a
I have successfully connected to an Oracle database (10g) from C# (Visual Studio 2008)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.