I want to replace specific text in CSV file in specific column. I'm able to do that using old post. But that code is not working when my CSV data contains space. I am not batch script expert so I am not able to fix it. I tried many things.
@echo off
setlocal enabledelayedexpansion
set inputCSV=TestCSV.csv
set outputCSV=TestCSV2.csv
(for /f "tokens=*" %%a IN (%inputCSV%) DO (
set column=0
set "line="
for %%i in ( %%a ) do (
set /a column+=1
set value=%%~i
if !column!==4 (
if "!value!"=="TEST" set "value=abc"
)
set "line=!line!,"!value!""
)
echo !line:~1!
))>%outputCSV%
ID,Name,Date,Field1,Test Data Space Check
1010101,Test 1,01/27/2001 10:00:00 PM,TEST,Test Data Space Check
2020202,Test 2,01/27/2001 10:00:00 PM,TEST,Test Data Space Check
3030303,Test 3,01/27/2001 10:00:00 PM,TEST,Test Data Space Check
4040404,Test 4,01/27/2001 10:00:00 PM,TEST,Test Data Space Check
5050505,Test 5,01/27/2001 10:00:00 PM,TEST,Test Data Space Check
I ALso tried this but it didn't work.
@echo off
setlocal enabledelayedexpansion
set inputCSV=TestCSV.csv
set outputCSV=TestCSV2.csv
:: tokens=*
(for /f "tokens=*" %%a IN (%inputCSV%) DO (
set column=0
set "line="
for /f "delims=," %%i in ( %%a ) do (
set /a column+=1
set value=%%~i
if !column!==4 (
if "!value!"=="TEST" set "value=ABC"
)
set "line=!line!,"!value!""
)
echo !line:%%a!
))>%outputCSV%
This is how the final output looks

Old post: Use batch scripting to replace null values with 0 in a specific column in a CSV
4with0? or is the code not related to the task you're now wanting it for? Also you need to expand on where the issue lies with csv data containing a space, because we cannot see it, and you've not attempted to account for that.if "!value!"=="Test" set "value=ABC". I have updated the question with test csv data.