-2

I havent find anything on internet so i need your help.

I have 2 CSV Files that i would like to compare:

the first one is like :

"Name","PrimarySmtpAddress","EmailAddresses"

the second one is like :

"Name","$_.TotalItemSize.Value.ToMB()"

the output file must show which name is both in first and second files

And i want to have, as output, a file with all the data in the first files but with the "$_.TotalItemSize.Value.ToMB()" added a the end of each lines.

for exemple it would do something like :

"Name","PrimarySmtpAddress","EmailAddresses","$_.TotalItemSize.Value.ToMB()",

I must be not very clear because me english is not perfect. Can you guys please help me ? im not very good at scripting. thank you very much.

edit :

REM @echo off
setlocal enabledelayedexpansion
set var1
set var2

for /f "tokens=1 delims=," %%A in (file2.txt) do (
   set var1=%%A
   echo %var1%

   for /f "tokens=1 delims=," %%B in (file1.txt) do (
      set var2=%%B
      echo %var2%
      if ("%var1%"=="%var2%")
      (
         echo equal var
      )
      else
      (
         echo not equal var
      )
   pause
   )
)
pause

It looks like the IF is not working

  • Not being good at scripting does not exempt you from doing some research and showing some efforts! Regard that StackOverflow is not a free code/script writing service! Please read the [tour] and learn [ask] here! – aschipfl Oct 20 '17 at 08:35
  • i didnt ask someone to do it for me, sorry if its look like i did. i have find multiple topic where people only wanted to compare if the line were equal or not, in my case they are never alike. only the first field match – user7394492 Oct 20 '17 at 08:55
  • [you need a way to read two files parallel](https://stackoverflow.com/a/43005887/2152082). – Stephan Oct 23 '17 at 06:19
  • could be usefull but i still have some trouble with the if – user7394492 Oct 23 '17 at 09:26

1 Answers1

0

for each line in 1.csv, look for the name in 2.csv and print combined line.
The REGEX may look a bit strange to you, it's:

/rc:: r=use Regex, c:=use string (necessary, as there could be spaces)
^: "Start of string"
\": a literal "
%%~a: the name without quotes
/": another literal "
,: a literal , (optional)

"tokens=1,* delims=," means "put the first token into %%m and all the rest into %%n"

Note: there is no IF. It's replaced by findstr, which extracts just the line, you need.

Note: this may be slow with big files (2.csv is read multiple times (as much as there are lines in 1.txt))

@echo off 
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=," %%a in (1.csv) do (
 for /f "tokens=1,* delims=," %%m in ('findstr /rc:"^\"%%~a\"," 2.csv') do (
  echo %%a,%%b,%%n
 )
)

Names, that aren't in both files, will be skipped.

Stephan
  • 53,940
  • 10
  • 58
  • 91