Search code examples
csvfor-loopbatch-filecmd

How to check column contains substring and another column starts with substring


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.


Solution

  • 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 ...