lqdo2000
lqdo2000

Reputation: 391

DOS batch file only process the first file

Being new to DOS, I am trying to write a batch file that replaces all the "|" by "," in files called test1.txt, test2.txt and output to files called test1.csv, test2.csv. It works fine for the first file but the second .csv file keeps the "|".

Here is my code:

@echo off
setlocal enabledelayedexpansion

for %%a in (test*.txt) do (
set line=%%a
type "!line:|=","!" > %%~na.csv
)

I read a thread mentioning that the "line" variable may be changed after parsing the block once, but I don't see how to fix the problem (I tried several modifications like "call" before "type" but still the same).

Any help would be great. Thanks !

Upvotes: 1

Views: 330

Answers (1)

foxidrive
foxidrive

Reputation: 41244

Edited: These work here.

@echo off
setlocal enabledelayedexpansion
for %%a in (test*.txt) do (
  for /f "delims=" %%b in ('type "%%a" ') do (
   set "line=%%b"
   set "line=!line:|=,!"
   >> "%%~na.csv" echo !line!
  )
)

Using a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855 this will preserve blank lines and be more reliable.

@echo off
for %%a in (test*.txt) do type "%%a" | repl "\|" "," m > "%%~na.csv"

Upvotes: 1

Related Questions