Comparing Strings in Batch inside a for (if statement)) - batch-file

I have a file1.txt with some file names.
I want to read from the file and if the file name contains "xyz" string i need to run some commands and if not some other commands
Not able to do this and need your help,tried a lot of methods but not able to figure out
Here's the sample code i tried:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2,3 delims=" %%i in (file1.txt) do (
set f=%%i
set z=XYZ
echo !f! >>test.txt
if /i "!f!" == "!z!" (
echo matched >> test.txt
) else (
echo nomatch >> test.txt
)
)

To compare whether a string contains another one, you can do it like this (note that this is case-insensitive, because the underlying sub-string expansion syntax is case-insensitive on its own):
if "!STRING!"=="!STRING:%SUB%=!" echo Sub-string "%SUB%" NOT found within "!STRING!"
Here the above approach is implemented into your script:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_INFILE=file1.txt"
set "_OUTFILE=test.txt"
set "_SUBSTR=XYZ"
>> "%_OUTFILE%" (
for /F "usebackq tokens=1-3 delims=" %%L in ("%_INFILE%") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
echo(!LINE!
if not "!LINE!"=="!LINE:%_SUBSTR%=!" (
echo matched
) else (
echo nomatch
)
endlocal
)
)
endlocal
exit /B
In addition, I improved the following things:
constant-like variables are predefined at the beginning;
the returned text is redirected once only; replace >> by > to overwrite an already existing file rather than appending to it;
delayed expansion is toggled within the loop in order not to lose exclamation marks in the text; note that the string in _SUBSTR must not contain such;
all file paths used in the script are enclosed within quotation marks;
the quoted set syntax is used throughout the script;
code indention is used for improved readability;
Alternatively, you could use the find command, which returns only those lines within file1.txt that contain the sub-string in %SUB%; add the /I option to do a case-insensitive search:
find "%SUB%" "file1.txt"

Related

How to remove spaces in the File name of the text file?

The file is not added in the destination path if it has spaces in the file name.
For example, if the filename is textfile1.txt -> it will be added in the destination path. However, if the directory filename has space like this text file4.txt it will not be added.
Is there a way to remove the spaces of the filename?
Here is the image:
Here is my main concern:
Here is my script:
#ECHO off
TITLE (c) ASDG
SETLOCAL EnableDelayedExpansion
SET locationPath=C:\Textfiles\
SET destinationPath=E:\Textfiles\
SET status=success
SET countMetadata=0
SET countPDF=0
SET countJPEG=0
ECHO Executing the program...
FOR /R %locationPath% %%g IN (*.txt) DO (
CD %%~dpg
IF EXIST *.txt* (
FOR /F "skip=1 tokens=1* delims=|" %%a IN (%%~nxg) DO (
SET /a countMetadata+=1
ECHO %%~dpa%%a^| %%b >> %destinationPath%^%%~nxg
)
IF %status% == success (
ECHO %%g has been successfully added
ECHO %%g >> %destinationPath%^logs.txt
)
)
)
After reformatting to use indentation to show code blocks (parenthesised code that is parsed, substituting %variables%, then executed), I then applied various recommendations:
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.
Use set /a to assign numeric values. No quotes required.
THIS CODE WILL NOT FULLY PERFORM THE REQUIRED TASK
Without comments to explain why some code is used, it's difficult to provide guidance.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE (c) ASDG
SET "locationPath=C:\Textfiles"
SET "destinationPath=E:\Textfiles"
SET "status=success"
SET /a countMetadata=0
SET /a countPDF=0
SET /a countJPEG=0
ECHO Executing the program...
FOR /R "%locationPath%" %%g IN (*.txt) DO (
CD %%~dpg
IF EXIST *.txt* (
FOR /F "usebackq skip=1 tokens=1* delims=|" %%a IN ("%%~nxg") DO (
SET /a countMetadata+=1
ECHO %%~dpa%%a^| %%b >> %destinationPath%\%%~nxg
)
IF !status! == success (
ECHO %%g has been successfully added
ECHO %%g >> %destinationPath%\logs.txt
)
)
)
GOTO :EOF
Modifications explanation
I prefer the setlocal to be the second line. YMMV.
Set commands modified as explained above
locationpath in the for /R quoted - it may contain separators like spaces.
%%~nxg quoted in for...%%a as it may contain spaces
usebackq added to for...%%a as %%~nxg is now quoted
I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables)
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs
(See `for/f` from the prompt for documentation)
Not sure what is going on with the following echo, but destinationPath no longer has the terminal \. Same comment applies to the following ECHO %%g.
!staus! replaces %status% (logically) as logically, status may change within the block - the delayed expansion trap
BUT status is not being varied within the block, so its value will always be its initial value, success.
Using Boolean
So - a few things for OP to clean up...

Batch Script not displaying entire output

I have an input text file with some server names
I am trying to replace the 'new line' for each server with |.
abcdef.abcdef.abcdef
testing
example
When executed in cmd.exe my script doesn't parse all lines from the input file. Say, if I have 1000 servers in my input file, I can see the output in the Command Prompt window for just around 500 servers.
#echo off
setlocal enableextensions
SETLOCAL EnableDelayedExpansion
rem Purpose - To remove domain name of servers and convert line by line strings to | seperated strings.
type nul > Del_Output.txt
for /f "tokens=1 delims=." %%a in (Downtime.txt) do (
echo(%%a>> Del_Output.txt
)
type Del_Output.txt
rem Initialize the output line
set "line="
rem Catenate all file lines in the same variable separated by "|"
for /F "delims=" %%a in (Del_Output.txt) do set "line=!line!|%%a"
rem Show final line, removing the leading "|"
rem echo !line:~1!>FinalOutput.txt
echo !line:~1!
rem type "C:\Users\AAithal\Desktop\MoogSoft_Downtime\FinalOutput.txt"
ENDLOCAL
Some pointers on the issue would be very helpful.
This should work:
#echo off
setlocal
set "first="
< nul (
for /F "delims=." %%a in (Downtime.txt) do (
if not defined first (
set "first=1"
set /P "=%%a"
) else (
set /P "=|%%a"
)
)
echo/
) > Output.txt
As others have indicated already, the maximum length of a batch variable is 8192 characters, so all names that exceed such a size are not included in your code...
The solution is not use a variable to collect the output. The set /P command allows to output a string with no LF at end, so the input lines are just joined in a long output line.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q56905805.txt"
SET "outline="
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
FOR /f "tokens=1*delims=." %%i IN ("%%a") DO IF "%%j"=="" (
SET "outline=!outline!|%%a"
) ELSE (
IF DEFINED outline ECHO !outline!
SET "outline=%%i"
)
)
IF DEFINED outline ECHO !outline!
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances. The listing uses a setting that suits my system.
I used a file named q56905805.txt containing your data for my testing.
The usebackq option is only required because I chose to add quotes around the source filename.
Assign each line to %%a, then reparse it with the delimiter. It it contains the delimiter, it's a new server, so output the previous one and start anew, else accumulate with the required separator.

Batch to insert a line in a text file

My goal is to make a script which is able to insert a string under a specific line. This is what I have done:
SETLOCAL ENABLEDELAYEDEXPANSION
set outputFile=C:\Utilisateurs\a669884\Documents\script22.txt
set "_strInsert=import java.interceptor.ReportInterceptor;"
set "_strFind=import javax.interceptor.Interceptors;"
:Ask
echo Are you in the good folder (wlp-cas-provider)?(Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If /I "%INPUT%"=="y" goto yes
If /I "%INPUT%"=="n" goto no
echo Incorrect input & goto Ask
:yes
for %%i in (*.java) DO (
FOR /F "usebackq delims=" %%A IN ("%%i") DO (
Echo %%A | Find "%_strFind%" && ECHO %%A>>"%outputFile%" && ECHO %_strInsert%>>"%outputFile%"
IF [!errorlevel!] == [1] ECHO %%A>>"%outputFile%"
)
MOVE /Y "%outputFile%" "%%i" && DEL /F /Q "%outputFile%"
)
:no
echo Oh no! Go to the right folder and comeback
cls
pause
The main idea is to copy the modified text in a temporary file and paste it in the original file. After that, the temporary file is deleted.
The code is working perfectly when it's done on only one file. With the for in order to do it on all .java files in a folder, it's not working anymore. The temporary file script22.txt is not deleted anymore and the text is piled up in it.
How about something along these lines: (untested)
#ECHO OFF
FOR %%A IN ("%CD%") DO IF /I NOT "%%~nxA"=="wlp-cas-provider" (
ECHO=Please change directory to wlp-cas-provider
ECHO= before running this script again.
ECHO=
ECHO=Press any key to exit ...
PAUSE>NUL
GOTO :EOF)
SET "_strFind=import javax.interceptor.Interceptors;"
SET "_strInsert=import java.interceptor.ReportInterceptor;"
SETLOCAL ENABLEDELAYEDEXPANSION
SET NL=^
SET "str2Add=%_strFind%!NL!%_strInsert%"
FOR /F "TOKENS=1-2* DELIMS=:" %%A IN ('FINDSTR/BLINC:"%_strFind%" *.java 2^>NUL'
) DO (SET/A "i=1+%%B"
FINDSTR/BLINC:"%_strInsert%" "%%A"|FINDSTR/B "!i!:">NUL
IF ERRORLEVEL 1 (( FOR /F "TOKENS=1* DELIMS=:" %%D IN ('FINDSTR/N $ "%%A"'
) DO IF %%D EQU %%B (ECHO=!str2Add!) ELSE ECHO=%%E)>>"%%A.tmp"))
FOR %%A IN (*.java.tmp) DO IF EXIST "%%~nA" (DEL "%%~nA" && REN "%%A" "%%~nA")
There is no heavy logical error in your script why the loop should fail. I suspect it is the pipe | that is causing the unexpected behaviour, because it initiates a new command prompt (cmd) instance for either side, where the left one receives an already expanded string (echo %%A), which may contain characters that have special meanings to cmd.
Here is a possible improvement of your script -- see all the explanatory remarks (rem):
#echo off
setlocal DisableDelayedExpansion
rem // Define constants here:
set "targetFolder=C:\Utilisateurs\wlp-cas-provider"
set "fileMask=*.java"
set "outputFile=C:\Utilisateurs\a669884\Documents\script22.txt"
set "_strInsert=import java.interceptor.ReportInterceptor;"
set "_strFind=import javax.interceptor.Interceptors;"
rem // Enumerate all matching files:
for %%I in ("%targetFolder%\%fileMask%") do (
rem /* Redirect the output of the whole `for /F` loop at once;
rem this improves the performance, and there is no appending `>>`,
rem so it does not matter whether the file already exists before: */
> "%outputFile%" (
rem /* Use `findstr /N` to precede every line by its line number
rem and a colon, so no line appears empty to `for /F` any more;
rem remember that `for /F` ignores empty lines otherwise: */
for /F "delims=" %%A in ('findstr /N "^" "%%~I"') do (
rem // Store the current line string with line number prefix:
set "line=%%A"
rem // Toggle delayed expansion to maintain exclamation marks:
setlocal EnableDelayedExpansion
rem // Remove the line number prefix and the colon:
set "line=!line:*:=!"
rem // Return the original line unconditionally:
echo(!line!
rem /* Explicitly initiate another child `cmd` instance on the
rem left side of the pipe with delayed expansion enabled;
rem escape the delayed expansion like `^^!` not to be
rem processed by the parent `cmd` instance, so the child
rem `cmd` instance receives the variable `!line!` only;
rem therefore strings potentially containing special
rem characters are expanded at the very latest point: */
(cmd /V /C echo(^^!line^^!| find "!_strFind!" > nul) && (
rem /* The string to search has been found, so return the
rem string to insert after it at this point: */
echo(!_strInsert!
)
endlocal
)
)
rem // Move the result file over the currently iterated one:
move /Y "%outputFile%" "%%~I" > nul
)
endlocal
exit /B
This allows literally all characters to appear in the search string. Note that quotation marks " need to be doubled to be processed correctly by find. If you want the whole line to match the search string, replace the find command line by findstr /X /C:"!_strFind!". In this case, if quotation marks appear in the search string, escape them like \"; literal backslashes need to be doubled like \\. Add the /I option to find or findstr if you want to search case-insensitively.
You can avoid the block containing the pipe...:
(cmd /V /C echo(^^!line^^!| find "!_strFind!" > nul) && (
echo(!_strInsert!
)
...when you use if statements to compare strings.
For example, use this instead if the line is expected to contain the search string:
if not "!line!"=="!line:*%_strFind%=!" echo(!_strInsert!
This always does a case-insensitive search because of the used sub-string replacement approach. This method may fail if any of the following characters occur in the search string: ^, !, %, =, ".
If the whole line is expected to match the search string, use this instead:
if "!line!"=="!_strFind!" echo(!_strInsert!
This does a case-sensitive search, unless you add the /I switch to the if statement.

Extract Part of a text file in BAT

I am capturing a m3U file on a daily basis but wish to parse part of it to another file with the few channels I need.
For example I have renamed my m3U to Test.txt file which say has the following fictional structure:
#EXTINF:0,ABC
#live link 1
#EXTINF:0,XYZ
#live link 2
#EXTINF:0,UVW
#live link 3
I would just like to capture say the line staring from "#EXTINF:0,XYZ" and say the line beneath it to end up with a Output.txt as follows:
#EXTINF:0,XYZ
#live link 2
I know that one needs to use the For loop but I am a bit of a noob on this area.
Put this code into the file filter.cmd.
#echo off
set INPUT=%1&set MATCH=%2& set MATCHED=0
for /f "delims=" %%a in (%INPUT%) do call :line "%%~a"
goto :eof
:line
set EXT=&TITLE=&
for /f "tokens=1 delims=:" %%a in ("%~1") do set EXT=%%~a
for /f "tokens=1,2,* delims=:," %%a in ("%~1") do set TITLE=%%~c
if "%EXT%" == "#EXTM3U" echo %~1
if "%EXT%" == "#EXTINF" (
set MATCHED=0
echo %TITLE%| findstr /l %MATCH% >nul && set MATCHED=1
)
if %MATCHED%==1 echo %~1
Use example:
filter.cmd input_file.m3u XYZ > output_file.m3u
Here is some explanation:
Every input line is split using for /f with tokens and delims.
MATCHED is set if the line begins with #EXTINF and the rest contains the string to match (second argument).
if MATCHED is set, the lines are output until next #EXTINF.
I would do it like this, supposing the .m3u file does not contain trailing white-spaces in the lines preceded by #EXTINF, like your sample data does:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "FILE=%~1"
set "HEADER=#EXTM3U"
set "PREFIX=#EXTINF"
set "MATCH=%~2"
set "FLAG="
for /F usebackq^ delims^=^ eol^= %%L in ("%FILE%") do (
if defined FLAG (
echo(%%L
set "FLAG="
)
for /F "delims=:" %%P in ("%%L") do (
if "%%P"=="%HEADER%" (
echo(%%L
) else if "%%P"=="%PREFIX%" (
set "LINE=%%L"
setlocal EnableDelayedExpansion
if /I "!LINE:*,=!"=="!MATCH!" (
echo(!LINE!
endlocal
set "FLAG=#"
) else endlocal
)
)
)
endlocal
exit /B
Call the script like this, supposing it is saved as extract-entry.bat:
extract-entry.bat "input_file.m3u" "XYZ" > "output_file.m3u"
The script walks through the given .m3u file line by line. It returns the current line unedited and resets variable FLAG, if variable FLAG is set, which is not the case at the beginning.
Then it looks for #EXTINF. If found (e. g., #EXTINF:0,XYZ), the string after the comma (XYZ) is compared against the given search string. If matched, the current line is output and FLAG variable is set now in order to get the following line too.
The header line #EXTM3U is always output.
Toggling delayed expansion makes this script robust against all characters that have special meaning to the command interpreter without losing them.

how to remove duplicate entry from the text file using batch script or vb script?

how can I remove the duplicate entry from the text file using batch script. All i want to remove the duplicates before "=" sign and "%%" is exist in every single text file. Text file look likes below
%%B05AIPS_CDDOWNLOAD_IBDE_UNC=\\%%B05AIPS_UPLOAD_NODE.\F$\DATA\IPSL\CDFILES\B05_NAG\CD\INCOMING
%%B05AIPS_CDDOWNLOAD_FTS_UNC=\\%%B05AIPS_UPLOAD_NODE.\B05_NAG\FTS\To_Clearpath\%%DATE_CCYYMMDD.
%%B05AIPS_CDDOWNLOAD_FTS_UNC=%%B05AIPS_CDDOWNLOAD_FTS_UNC.
I got about 30 plus different text files which contains above kind of entries and want to remove the duplicate line and want to keep the first occurrence. Remember duplicate line should be identified before "=" sign only and removal required for the entire line.Each of the different text files have got "%%" sign. Please guide me if there is way to do through batch script or vbscript? Thanks
Here is a simple batch-file solution; let us call the script rem-dups.bat. Supposing your input file is test.txt and your output file is result.txt, you need to provide these files as command line arguments, so you need to call it by: rem-dups.bat "test.txt" "results.txt". Here is the script:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE set "OUTFILE=con"
for /F "usebackq tokens=1,* delims==" %%K in ("%INFILE%") do (
set "LEFT=%%K"
set "RIGHT=%%L"
set "LEFT=!LEFT:*%%%%=__!"
rem Remove `if` query to keep last occurrence:
if not defined !LEFT! set "!LEFT!=!RIGHT!"
)
> "%OUTFILE%" (
for /F "delims=" %%F in ('set __') do (
set "LINE=%%F"
echo(!LINE:*__=%%%%!
)
)
endlocal
exit /B
The script is based on the fact that there cannot occur duplicate environment variables, that are such with equal names.
This code only works if the following conditions are fulfilled:
the file content is treated in a case-insensitive manner;
the order of lines in the output file does not matter;
the partial strings before the first = sign start with %% and contain at least one more character other than %;
the partial strings before the first = contain only characters which may occur within environment variable names, besides the leading %%;
the partial strings after the first = must not be empty;
the partial strings after the first = must not start with = on their own;
no exclamation marks ! are allowed within the file, because they may get lost or lead to other unexpected results;
Here is an alternative method using a temporary file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE set "OUTFILE=con"
set "TEMPFILE=%TEMP%\%~n0_%RANDOM%.tmp"
> "%TEMPFILE%" break
> "%OUTFILE%" (
for /F usebackq^ delims^=^ eol^= %%L in ("%INFILE%") do (
for /F tokens^=1^,*^ delims^=^=^ eol^= %%E in ("%%L") do (
> nul 2>&1 findstr /I /X /L /C:"%%E" "%TEMPFILE%" || (
echo(%%L
>> "%TEMPFILE%" echo(%%E
)
)
)
)
> nul 2>&1 del "%TEMPFILE%"
endlocal
exit /B
Every unique (non-empty) token left to the first = sign is written to a temporary file, which is searched after having read each line from the input file. If the token is already available in the temporary file, the line is skipped; if not, it is written to the output file.
The file content is treated in a case-insensitive manner, unless you remove the /I switch from the findstr command.
Update: Improved Scripts
Here are two scripts which are improved so that no special character can bring them to fail. They do not use temporary files. Both scripts remove lines with duplicate keywords (such is the partial string before the first = sign).
This script keeps the first line in case of duplicate keywords have been encountered:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE exit /B 1
> "%OUTFILE%" break
for /F usebackq^ delims^=^ eol^= %%L in ("%INFILE%") do (
for /F tokens^=1^ delims^=^=^ eol^= %%E in ("%%L") do (
set "LINE=%%L"
set "KEY=%%E"
setlocal EnableDelayedExpansion
if not "!LINE:~,1!"=="=" (
set "KEY=!KEY: = !"
set "KEY=!KEY:\=\\!" & set "KEY=!KEY:"=\"!"
more /T1 "%OUTFILE%" | > nul 2>&1 findstr /I /M /B /L /C:"!KEY!=" || (
>> "%OUTFILE%" echo(!LINE!
)
)
endlocal
)
)
endlocal
exit /B
This script keeps the last line in case of duplicate keywords have been encountered:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE exit /B 1
> "%OUTFILE%" (
for /F delims^=^ eol^= %%L in ('findstr /N /R "^" "%INFILE%"') do (
set "LINE=%%L"
for /F "delims=:" %%N in ("%%L") do set "LNUM=%%N"
setlocal EnableDelayedExpansion
set "LINE=!LINE:*:=!"
if defined LINE if not "!LINE:~,1!"=="=" (
for /F tokens^=1^ delims^=^=^ eol^= %%E in ("!LINE!") do (
setlocal DisableDelayedExpansion
set "KEY=%%E"
setlocal EnableDelayedExpansion
set "KEY=!KEY: = !"
set "KEY=!KEY:\=\\!" & set "KEY=!KEY:"=\"!"
more /T1 +!LNUM! "%INFILE%" | > nul 2>&1 findstr /I /M /B /L /C:"!KEY!=" || (
echo(!LINE!
)
endlocal
endlocal
)
)
endlocal
)
)
endlocal
exit /B
For both scripts, the following rules apply:
the order of lines with non-duplicate keywords is maintained;
empty lines are ignored and therefore removed;
empty keywords, meaning lines starting with =, are ignored and therefore removed;
non-empty lines that do not contain an = at all are treated as they would be ended with an = for the check for duplicates, hence the entire line is used as the keyword;
for the check for duplicates, each TAB character is replaced by a single SPACE;
every line that is transferred to the returned file is copied from the original file without changes (hence the aforementioned attachment of = or replacement of TAB is not reflected there);
the check for duplicates is done in a case-insensitive manner, unless you remove the /I switch from the findstr command;
Amendment: Processing Multiple Files
All of the above scripts are designed for processing a single file only. However, if you need to process multiple files, you could simply write a wrapper that contains a for loop enumerating all the input files and calls one of the scripts above (called rem-dups.bat) for every item -- like this:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "INPATH=D:\Data\source" & rem (location of input files)
set "OUTPATH=D:\Data\target" & rem (location of output files)
set INFILES="source.txt" "test*.txt" & rem (one or more input files)
set "OUTSUFF=_no-dups" & rem (optional suffix for output file names)
set "SUBBAT=%~dp0rem-dups.bat"
pushd "%INPATH%" || exit /B 1
for %%I in (%INFILES%) do if exist "%%~fI" (
call "%SUBBAT%" "%%~fI" "%OUTPATH%\%%~nI%OUTSUFF%%%~xI"
)
popd
endlocal
exit /B
You must not specify the same locations for the input and output files. If you want to overwrite the original input files, you need to write the modified output files to another location first, then you can move them back to the source location -- supposing you have set OUTSUFF in the wrapper script to an empty string (set "OUTSUFF=" instead of set "OUTSUFF=_no-dups"). The command line to overwrite the original input files would be: move /Y "D:\Data\target\*.*" "D:\Data\source".
You could read the file into Excel without splitting it into multiple columns. Use Excel functionality to eliminate duplicates and save it back. You could do all this in VBScript.
Create an Excel Object
Loop
Load text file
Remove duplicates
Save text file
Until there are no more files
Dispose of the Excel Object
Code for the individual pieces should be easily available on the web. Do ask for any additional, specific, pointers you might need.

Resources