Copying File that contains Pipe characters

June 7, 2010 at 14:48:08
Specs: Windows XP
Hello, I am attempting to replace a couple lines in a file using a batch file. Everything is working fine until it gets to a line with pipes on it: "Codes:=16|17|24|25|26|28"
When it tries to set a variable equal to that line it exits and stops copying.

@echo off
SET Filepath=C:\test\myfile.DAT
SET TempPath=%Filepath%.tmp
if exist %Filepath%.tmp del %Filepath%.tmp
for /f "tokens=* delims= " %%a in (%Filepath%) do call :Change "%%a"
move %TempPath% %Filepath%
ENDLOCAL
Pause
exit /b

:Change
set Line=%~1
for /f "tokens=1 delims==" %%a in ("%Line%") do set Text=%%a
if "%Text%"=="LogicalLinkID" (
(echo LogicalLinkID=-1)>> %TempPath%
) else (
if "%Text%"=="LogicalLinkID_0" (
echo LogicalLinkID_0=-1)>> %TempPath%
) else (
(echo %Line%)>> %TempPath%
)
)
exit /b


See More: Copying File that contains Pipe characters

Report •


#1
June 7, 2010 at 15:10:28
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set S=%%a
set S=!S:apple=berry!
>> newfile echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
June 7, 2010 at 15:18:56
My issue isn't with the string replacement, that is handled by the if statements and works fine. The file bugs afterwards on copying the line with pipes in it. Your example shows using delayed expansion and replacing "apples" with "berry" but I need the line to stay exactly as is. It has pipes in it and I need to copy those pipes to the new file.

I am able to get past this if leaving the "" on it (SET Line=%1 instead of SET Line=%~1) however this would not work as the new file must be exactly like the old one with the exception of the changes made in the if statements.

Also note that the file is exiting on set, not on the echo.

"SET Line=%~1" causes the bug when %~1 is the line with pipes in it.


Report •

#3
June 7, 2010 at 15:47:30
Pipes are special characters to CMD. I suggest you use a language that separates data and commands.

Report •

Related Solutions

#4
June 7, 2010 at 16:55:41
LOL I guess you didn't bother to try it.

Here's the before & after:

a2
apple
a3
file. Everything is workin
Codes:=16|17|24|25|26|28
222222
line six

a2
berry
a3
file. Everything is workin
Codes:=16|17|24|25|26|28
222222
line six


=====================================
Helping others achieve escape felicity

M2


Report •

#5
June 8, 2010 at 06:33:39
Your example is a bit different from what I'm doing and doesn't explain why it breaks in my code.

Why is it breaking on: Set Line=%~1 ?
Using delayed expansion I'm still having the same error...

Is it because I'm using a call and you are not?



Report •

#6
June 8, 2010 at 06:49:53
If I move my call into the for loop the file is filled with "ECHO is off", leaving it where it is I only get the file up to the line with pipes in it where it stops.

Report •

#7
June 8, 2010 at 07:07:33
Thank you for your help M2. I was able to get it to work with the below:


SET Filepath=C:\test\myfile.txt
SET TempPath=%Filepath%.tmp
if exist %Filepath%.tmp del %Filepath%.tmp

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=* delims= " %%a in (%Filepath%) do (
set Line=%%a

for /f "tokens=1 delims==" %%a in ("!Line!") do set Text=%%a
if "!Text!"=="LogicalLinkID" (
(echo LogicalLinkID=-1)>> %TempPath%
) else (
if "!Text!"=="LogicalLinkID_0" (
(echo LogicalLinkID_0=-1)>> %TempPath%
) else (
>> %TempPath% echo.!Line!
)
)

)
move %TempPath% %Filepath%
ENDLOCAL
exit /b


Report •

Ask Question