読者です 読者をやめる 読者になる 読者になる

MEMO blog

主に自分用のメモです

csvを読み込んで文字列を置換するbat

BAT

csvを読み込んで文字列を置換するbat

@echo off
setlocal enabledelayedexpansion

rem pushd current directory
pushd %~dp0

rem Specify file name
set source_file=source.txt
set csv_file=replace.csv

rem Read csv_file("new_file_name, target_word, replace_word")
rem %%i %%j %%k
for /f "usebackq tokens=1-3 delims=," %%i in (%csv_file%) do (
    rem Make output file
    type nul >%%i
     
    rem Edit file contents  
    for /f "delims=" %%A in (%source_file%) do (
        set line=%%A
        echo !line:%%j=%%k!>>%%i
    )
) 


echo Done...
pause

exit /b