Output difference of 2 txt files to a 3rd txt file - cmd

I'm trying to run a bat file that will compare 1 file to another and output the differences
I've tried using gnu diff utilites, fc, and endless googleing to find a solution but I cant seem to figure it out
File 1
C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub
File 2
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub
I want file 3 to be
C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
Any one have any ideas?

You could use diff from the DiffUtils and something like this:
diff file1.txt file2.txt | findstr /r /c:"^<" /c:"^>" >file3.txt
The output lines will be preceded by < or >, depending on which file the respective line was missing in. If you want to remove those indicators as well, use something like this:
for /f "tokens=1*" %a in (
'diff file1.txt file2.txt ^| findstr /r /c:"^<" /c:"^>"'
) do #echo %b >>file3.txt
Change %a and %b into %%a and %%b if you want to run this in a batch file.

FINDSTR /v /b /e /l /g:file2. file1. >file3.
should produce the required results - lines in file1 missing from file2.
/v says 'not found', /b /e forces exact match - not part-of-line-matches /l literal.

Related

Use Windows findstr to list files not containing a string

From Windows CMD I can use
findstr -m subroutine *.f90
to list all files with suffix .f90 containing "subroutine". To list all .f90 files not containing the string I can do something like
dir /b *.f90 > files.txt
findstr -m subroutine *.f90 > files_with_string.txt
and then write a script to list the lines in files.txt not found in files_with_string.txt. Is there a more elegant way?
There is a /v option in findstr, but that wouldn't help here.
Process each file with a for loop, try to find the string and if it doesn't find it (||), echo the filename:
for %a in (*.f90) do #findstr "subroutine" "%a" >nul || echo %a
(above is command line syntax. For use in a batchfile, use %%a instead of %a (all three occurences))
I needed to search for filenames which contained one specific string ("up"), but did not contain another string ("packages").
However, I was hoping to run it from the command line.
This is actually possible to do, you just have to call findstr twice.
Mine looked like:
dir /B /S up | findstr /I "up" | findstr /I /v "packages"
That means:
search all directories (/S & subdirs)
give me the bare formatting (/B)
and pass it through (| pipe) findstr (/I ignore case) to find ones that have "up" then
pass the results (| pipe) through findstr again but this time ignore all that
contain (/v) "packages"
If you have items like:
c:\test\packages\up
c:\extra\thing\up
c:\extra\thing\packages\up
c:\extra\test\up
c:\extra\test\nothing
The results would be only the ones that contain "up" but do not contain "packages"
c:\extra\thing\up
c:\extra\test\up
Call findstr /v multiple times on result
In other words you can keep passing the result into another findstr with /v to remove the ones that have additional words you don't want.

Batch file to compare the differences in two csv files

I use windows 7.
I have two csv files file1.csv and file2.csv
file1.csv
emp_id;salary
1;1000
2;2000
3;3000
file.csv
emp_id;salary
1;1000
2;2000
3;3000
4;4000
5;5000
I'm confused how to write a batch file.
The batch file should output the should be a csv file showing the changes.
Sample output:
emp_id;salary
4;4000
5;5000
You can use findstr to look for differences, and the /v parameter to display differences. Like so:
findstr /v /g:"file1.csv" "file2.csv"
Also:
for /f "delims=" %%a in (file1.csv) do (
findstr "^%%a$" "file2.csv" >nul ||echo %%a
)
And using the fc command:
fc "file1.csv" "file2.csv"
For fc im sure you can use an if not errorlevel 1 echo No difference

findstr displays matches in one line

I have two files:
1.txt:
abc
def
2.txt:
abc
Please note that 2.txt contains only 3 characters, no empty lines. Now if I do:
findstr /S /I /L /A:02 "abc" *
I get this result:
1.txt:abc
2.txt:abc
Which is what I expect. However after renaming 1.txt to uno.txt and 2.txt to duo.txt (thus changing file search order) and running the same command I get this:
duo.txt:abcuno.txt:abc
Result is in one line and I was expecting two lines as before. Of course if I add new line at the end of duo.txt then output is ok but how to do that without modifing files? Is there some "force result in new line" option in findstr?
You could try using a FOR loop to go through the files and pipe the contents of each to FINDSTR.
Something like this:
#ECHO OFF
FOR /R %%f IN (*.txt) DO (
TYPE %%f | FINDSTR /S /I /L "abc"
)

Split text file into multiple files using windows batch scripting

I need to split one text file into multiple files using windows batch script, could anybody light me up?
sample text file:
abc1-10
abc1-11
abc1-12
xyz2-01
xyz2-02
xyz3-01
xyz3-02
in this case, it has to split into 3 files, first one consists the lines abc1-xx, second one consists xyz2-xx and xyz3-xx go to the last one
You could use a batch file, but why not just use FINDSTR command?
findstr /R "^abc1-" sample.txt > file1.txt
findstr /R "^xyz2-" sample.txt > file2.txt
findstr /R "^xyz3-" sample.txt > file3.txt
Use the cgwin command SPLIT.
Samples:
-split a file every 500 lines counts:
split -l 500 [filename.ext]
For more: split --help
This may help - it will split the text into separate files of
abc1.txt
xyz2.txt
xyz3.txt
#echo off
for /f "tokens=1,* delims=-" %%a in ('type "file.txt"') do (
>>"%%a.txt" echo(%%a-%%b
)
pause

Batch file to remove first 10 chars from txt file

I'm trying to remove the first 10 characters from multiple lines inside a text file using a batch script, then output the results to a new file. I ran across this and it got me pointed in the right direction but the final output isn't working.
Here's what I've got so far:
setLocal EnableDelayedExpansion
CSCRIPT /nologo %windir%\System32\prnport.vbs -l > c:\IPPorts.txt
type c:\IPPorts.txt | findstr IP_ > c:\IPPorts2.txt
for /f "tokens=*" %%a in (c:\IPPorts2.txt) do (set line=%%a set chars=!line:~10! > c:\IPPorts3.txt)
for /f "delims=" %%x in (c:\IPPorts3.txt) do CSCRIPT /nologo %windir%\System32\prnport.vbs -d -r %%x
The 2nd line exports a list of printer ports to a file named IPPorts.txt. The 3rd finds the lines with "IP_" in them and exports to IPPorts2.txt. The 4th line is supposed to remove unneeded text (which it isn't doing) and export to IPPorts3.txt. And the last line will take the results from IPPorts3.txt and then delete those ports.
IPPorts.txt is as follows:
Server name
Port name IP_172.20.51.11
Host address 172.20.51.11
Protocol RAW
Port number 9100
SNMP Disabled
These lines are repeated for every port, of which there are several. Since I only need the line containing the port name, IPPorts2.txt looks like this:
Port name IP_172.20.51.11
Port name IP_172.20.52.58
Port name IP_172.20.53.16
Port name IP_172.20.54.19
Port name IP_172.20.55.15-1
Port name IP_172.20.55.15
Port name IP_172.20.55.11
Where I'm having trouble is removing the "Port name " portion of the lines (the first 10 characters). I want the output to read on each line as "IP_X.X.X.X". The problem is the 3rd file is always empty.
Where am I going wrong? Any help is greatly appreciated.
EDIT:
This is further down under Endoro's answer, but I thought it might be nice to post the answer here. Here's what I changed the 4th line to:
for /f "tokens=* delims=" %%c in ('type c:\IPPorts2.txt') do (
set LINE=%%c
>> c:\IPPorts3.txt echo !LINE:~10!
)
This has corrected my problems. Thanks everyone!
try this:
(for /f "tokens=3" %%i in (IPPorts2.txt) do #echo %%i)>IPPorts3.txt
Script to get directory name out of DIR command output :
...
20/09/2014 01:23 [DIR] some1
21/09/2014 02:34 [DIR] some2
22/09/2014 03:45 [DIR] some3
23/09/2014 11:22 [DIR] some4
...
We want it to be:
some1
some2
some3
some4
...
Code :
#FOR /f "tokens=4" %%D IN (i:\test.txt) DO #( echo %%D ) >> result.txt
In your case tokens=3, not perfect but does the job with few lines manually edited in the result.
(For /f "tokens=3delims= " %%i in (ipports2.txt) do echo %%i) >ipports3.txt
should do it for you.
The paretheses are important - ensure that the file is created anew. If omitted, will only generate the last line.
Simply uses the delimiter [space] to tokenise the string on each line into token1=Port, token2=Name and sets %%i to each token3 in turn.
The following isn't really a different solution but merely a suggestion to simplify your script by reducing the number of output files.
In fact, it is possible to exclude all of them from the script, unless you need to keep them for history.
Basically, the idea is first to apply FINDSTR directly to the output of prnport.vbs:
CSCRIPT /nologo %windir%\System32\prnport.vbs -l | FINDSTR "IP_"
then apply a loop directly to the output of FINDSTR (note the single quotation marks around the piped command line, as well as the escaped |):
FOR /F "tokens=3" %%A IN (
'CSCRIPT /nologo %windir%\System32\prnport.vbs -l ^| FINDSTR "IP_"'
) DO …
and call prnport.vbs with another set of arguments in that same loop:
FOR /F "tokens=3" %%A IN (
'CSCRIPT /nologo %windir%\System32\prnport.vbs -l ^| FINDSTR "IP_"'
) DO (
CSCRIPT /nologo %windir%\System32\prnport.vbs -d -r %%A
)
The tokens option of a FOR /F loop specifies which token (or field) to take based on a specific delimiter or set of delimiters. The default set of delimiters is a space, a comma, a tab. Your Port name IP_whatever lines conveniently consist of exactly three tokens and the third one is what you are after, hence "tokens=3" in the options.
So, as you can see, no output files, the necessary value is extracted and passed to the target command in the same iteration.

Resources