Reputation: 11
I would like to remove lines from one document that already exists in another document.
Example:
Document 1
A
B
C
D
Document 2
A
C
New Document
B
D
Is there a way to write this as a batch file on Windows?
This question has been asked here before, but none of them helped me as all of them are for Linux or something else that didn't work in a batch file.
Any help appreciated.
Upvotes: 1
Views: 73
Reputation: 67206
@echo off
setlocal
rem Define the lines in Document2
for /F "delims=" %%a in (Document2.txt) do set doc2["%%a"]=1
rem Show lines in Document1 that are not in Document2
for /F "delims=" %%a in (Document1.txt) do (
if not defined doc2["%%a"] (
set /P "=%%a" < NUL
echo/
)
)
Previous Batch file may fail if a document contain quotes, although this depends on the position of the quotes vs. spaces. It is convenient that Document1 be the largest of the two files.
Upvotes: 1
Reputation: 2178
Windows batch file do not have file read capabilities that can handle this. It has no replacement for grep or any of the advanced text searching capabilities of Linux, so AFAIK, it can't be done using vanilla Windows batch script.
I won't recommend any custom windows batch scripter because they are all different and if you want to go that route, you can just use the Linux answers on windows by finding the right batch scripting program to install.
If you provide a full context of why you want to do this, maybe there is a better way to do this other than using Windows batch script? You can easily make self-contained executables using more powerful scripting engines like AutoHotKey and other similar programs.
Upvotes: 0