I use a code to remove all files in a folder that contain some words saved in a CSV file.
This code reads every line of my CSV file, and after searches every word read in CSV and delete the files that in our source have these word.
Now I need to adapt this code to use like value the second column of my CSV.
My list.csv
file:
1,DRWORDAAA
2,ERWORDBBB
3,BCWORDCCC
99,ASWORDZZZ
My batch file:
for /F "delims=" %%a in ('findstr /M /G:"D:\Program Files\list.csv" "D:\Program Files\myfolder\*.log"') do del "%%a"
I need to set like values to search the 2 columns of my CSV (in my example from DRWORDAAA
to ASWORDZZZ
).
My CSV has hundreds of lines and the words to search can be different, but the structure is always the same.
Actually, the batch file code reads only the first column of CSV and do not recognize the seconds column.
Whilst I've tried to address your question, I've also utilised information from my last answer to you, as it appears to be effectively the same task.
The file of search strings is not supposed to be anything other than a list, i.e. a single column of search strings, where each complete line makes up the string to be matched. The simplest fix therefore is that you rewrite the csv file to include only a single 'column'.
@Echo Off
If Not Exist "D:\Program Files\MyFolder\*.log" Exit /B
Set "ds="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=list_%%C-%%B-%%A.csv"
If Not Exist "%ds%" Exit /B
( For /F "EOL=, Tokens=2 Delims=," %%A In ('Type "%ds%"'
) Do Echo(%%A)>"%Temp%\list.txt"
For /F "Tokens=*" %%A In ('FindStr /IMLG:"%Temp%\list.txt"^
"D:\Program Files\MyFolder\*.log"') Do Del /A /F "%%A">Nul 2>&1
Del "%Temp%\list.txt"
This uses the same method but without creating, reading and deleting a holding file. I would not recommend this method for very large csv files.
@Echo Off
If Not Exist "D:\Program Files\MyFolder\*.log" Exit /B
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=list_%%C-%%B-%%A.csv"
If Not Exist "%ds%" Exit /B
For /F "EOL=, Skip=1 Tokens=2 Delims=," %%A In ('Type "%ds%"'
) Do For /F "Tokens=*" %%B In ('FindStr /IMLC:"%%A" "*.log"'
) Do Del /A /F "%%B">Nul 2>&1
Notes:
Skip
's it. (Just remove, or add, the Skip=1
to whichever method you choose, as necessary.)Type
, which may help should your source csv be UTF.