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
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.
Related
This is my first time working with batch files. I am trying to extract certain columns from original csv and pipe output to new csv. The following code is what I wrote based on this link:
https://stackoverflow.com/a/17557532/16034206
#echo off
setlocal EnableDelayedExpansion
Rem for /f "skip=1 usebackq tokens=1,2,10,11 delims=," %%i in (sample.csv) do #echo %%i,%%j,%%k,%%l >>output.csv
echo "Your script is starting..."
FOR /F "skip=1 usebackq delims=" %%L in (sample.csv) DO (
set "line=%%L,,,,,,,,"
set "line=#!line:,=,#!"
FOR /F "tokens=1,2,10,11 delims=," %%a in ("!line!") DO (
set "param1=%%a"
set "param2=%%b"
set "param10=%%c"
set "param11=%%d"
set "param1=!param1:~1!"
set "param2=!param2:~1!"
set "param10=!param10:~1!"
set "param11=!param11:~1!"
if "%%~A"=="RH" echo !param1!, !param2!, !param10!, !param11! >> output.csv
)
)
echo "Your script has completed"
I am looking to apply logic to check param1 contains a substring "#gmail.com" AND that param10 starts with a specific string "100" before outputting that specific row of 4 columns into the csv.
I checked how to use if-statement from this link: https://stackoverflow.com/a/17474377/10671013
but I have not found any links on SO discussing "containing substring" or checking for "starting with a string". Please advise.
Remove the substring you look for from the first column and compare it with the original string, if not equal (string contains substring), check the first three characters of the other column. (This substring substitution is case insensitive):
if not "!param1:#gmail.com=!" == "!param1!" if "!param10:~0,3!" == "100" echo ...
I have a commma seperated csv-file like this:
ID,USER_ID, COL3_STR, COL4_INT
id1,username1,exampleA, 5
id2,username1,exampleB,0
id3,username2,NULL,-1
id4,username3,,3,false,20
Each value from the 2nd column USER_ID must be replaced with testusername (except the header "USER_ID"). The values are different, so I can't search a defined string.
My idea was to use a for-loop and get the second token from each line to get the username. For example:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET currentDir=%~dp0
SET "srcfile=%currentDir%\inputfile.csv"
SET "outfile=%currentDir%\result.csv"
for /f "tokens=2 delims=," %%A IN (%srcfile%) DO (
ECHO %%A)
ECHO done
PAUSE
Output:
USER_ID
username1
username1
username2
username3
So the 2nd column of the (new) csv file must look like:
USER_ID
testusername
testusername
testusername
testusername
I saw another question with an helpful answer.
Example: When each username is "admin":
(
for /f "delims=" %%A in (%srcfile%) do (
set "line=%%A"
for /f "tokens=2 delims=," %%B in ("admin") do set "line=!line:%%B=testuser!"
echo !line!
)
)>%outfile%
But this works only for a defined string. It's my first batch-script and I don't know how to "combine" this for my situation. I hope sombody can help me.
Must work for Windows 7 and 10.
You need all the tokens (for writing the modified file), not just the second one:
for /f "tokens=1,2,* delims=," %%A in (%srcfile%) do echo %%A,testuser,%%C
(where * is "the rest of the line, undelimited"). %%B would be the username, so just write the replacement string instead.
You could use an if statement to process the first line differently, or you process it separately:
<"%srcfile%" set /p header=
(
echo %header%
for /f "skip=1 tokens=1,2,* delims=," %%A in (%srcfile%) do echo %%A,testuser,%%C
) > "%outfile%"
The following script (let us call it repl_2nd.bat) replaces the values in the second column of a CSV file and correctly handles empty fields (where separators occur next to each other like ,,):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (path to input file; `%~1` is first argument)
set "_NVAL=testusername" & rem // (new string for second column)
set "_SEPC=," & rem // (separator character usually `,`)
rem // Especially handle first line:
< "%_FILE%" (
set "HEAD=" & set /P HEAD=""
setlocal EnableDelayedExpansion
echo(!HEAD!
endlocal
)
rem // Read input file line by line:
for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in ("%_FILE%") do (
rem // Store current line string:
set "LINE=%%L"
rem // Toggle delayed expansion to avoid loss of `!`:
setlocal EnableDelayedExpansion
rem /* Replace each separator `,` by `","` and enclose whole line string in `""`,
rem resulting in all items to become quoted, ven empty ones, hence avoiding
rem adjacent separators, which would become collapsed to one by `for /F`;
rem then split the edited line string at the first and second separators: */
for /F "tokens=1,2,* delims=%_SEPC% eol=%_SEPC%" %%A in (^""!LINE:%_SEPC%="^%_SEPC%"!"^") do (
rem /* Unquote the first item, then join a separator and the replacement string;
rem then remove the outer pair of quotes from the remaining line string: */
endlocal & set "REPL=%%~A%_SEPC%%_NVAL%" & set "REST=%%~C"
rem // Append the remaining line string with `","` replaced by separators `,`:
setlocal EnableDelayedExpansion & echo(!REPL!%_SEPC%!REST:"%_SEPC%"=%_SEPC%!
)
endlocal
)
endlocal
exit /B
To use the script o a file in the current working directory, use this command line:
repl_2nd.bat "inputfile.csv"
To store the output to another file, use the following command line:
repl_2nd.bat "inputfile.csv" > "outputfile.csv"
I have a csv file with 18 fields. I need to copy the file to a txt file, delete the first four lines, replace the data in field #8, and save the file with a new name.
The data in field #8 is an integer (for example, 1, 2, 3, etc). Each integer needs to be replaced with a separate value (for example, I need to replace 1 with 1005 and 3 with 1008). I am trying to modify/fix the following batch file:
#echo off
More +4 datatest.csv > datacopy.txt
( FOR /f "tokens=8 delims=," %%h in (datacopy.txt) do (
if "%%h"=="3" (echo 1008) else (
echo %%a %%b %%c` echo %%a %%b %%c
)
)
)>paygoinvoice.txt
#echo on
With only one token selected, you'll get only one column (%%h)
parsing the more command directly, there is no need for a temporary file.
depending on how many integers to replace, you may use a pseudo array with the int as an index/pointer.
you may either get all columns separately (tokens=1-18,%%A..%%R) or gather the rest * in one for variable.
#echo off & Setlocal EnableDelayedExpansion
( FOR /f "tokens=1-8* delims=," %%A in ('More +4 datatest.csv') do (
Set "H=%%H"
if "%%H"=="1" Set "H=1005"
if "%%H"=="3" Set "H=1008"
echo %%A,%%B,%%C,...,!H!,%%I
)
)>paygoinvoice.txt
#echo on
I have a simple batch where it echos each cell from a CSV file.
I would like to have an 'IF' statement to check if current cell value is one I need. So far I have:
#echo off
for /f "usebackq tokens=1-4 delims=," %%a in ("C:\Users\Name\New folder\Test.csv") do (
echo "%%a"
if %%a="computer123" echo "We need you"
pause
As you haven't asked a question, I can only assume that there is a problem with what you have so far.
Here's what I have:
#Echo Off
For /F "UseBackQ Tokens=1-4 Delims=," %%A In (
"C:\Users\Name\New folder\Test.csv"
) Do (
Echo="%%~A"
If /I "%%~A"=="computer123" Echo="We need you"
)
Timeout -1
This of course assumes that you're looking for the content in the first field and you still wish to use fields 2, 3 & 4; (Tokens 2[%%B], 3[%%C] & 4[%%D]).
I'm new to batch files and this is a tricky question. In stores.csv there is a column called 'Image' which stores vertical-line-delimited image URLs as values. There are also additional columns called 'AltImage2', 'AltImage3', etc. How can I split the vertical-line-delimited string into columns that start with 'AltImage' for each row in the CSV? 'AltImage' columns only go to AltImage5, and there may not be five image URLs in a given row. I would also like to keep the first image URL in the 'Image' column if possible.
Example of headers and single row of data:
Company,Title,Image,AltImage2,AltImage3,AltImage4,AltImage5
Testco,U2X40,image1.png|image2.png|image3.png
Desired result after running batch:
Company,Title,Image,AltImage2,AltImage3,AltImage4,AltImage5
Testco,U2X40,image1.png,image2.png,image3.png
So far I've tried this:
for /f "tokens=3 delims=, " %%a in ("stores.csv") do (
echo run command here "%%a"
)
But cannot even echo the values in the Image column.
Here is a solution using Bash script (unfortunately I need batch): How do I split a string on a delimiter in Bash?
#echo off
setlocal
< stores.csv (
rem Read and write the header
set /P "header="
call echo %%header%%
rem Process the rest of lines
for /F "tokens=1-3 delims=|" %%a in ('findstr "^"') do echo %%a,%%b,%%c
)
I think this handles your parsing problem. Pay attention to quotes and the usebackq option.
for /f "skip=1 tokens=3 delims=," %%a in (stores.csv) do for /f "tokens=1-5 delims=|" %%b in ("%%a") do echo %%b %%c %%d %%e %%f
Here's a fuller solution to play with. There may be a more elegant way to handle optional commas. And you'll have to handle directing the output to whichever place is appropriate.
#echo off
setlocal enabledelayedexpansion
echo Company,Title,Image,AltImage2,AltImage3,AltImage4,AltImage5
for /f "skip=1 tokens=3 delims=," %%a in (stores.csv) do (
for /f "tokens=1-5 delims=|" %%b in ("%%a") do (
set line=%%b
if not "%%c"=="" set line=!line!,
set line=!line!%%c
if not "%%d"=="" set line=!line!,
set line=!line!%%d
if not "%%e"=="" set line=!line!,
set line=!line!%%e
if not "%%f"=="" set line=!line!,
set line=!line!%%f
echo !line!
)
)
read the file line by line and replace | with , (you have to escape the | and use delayed expansion:
#echo off
setlocal enabledelayedexpansion
(
for /f "delims=" %%a in (old.csv) do (
set line=%%a
echo !line:^|=,!
)
)>new.csv