Page 1 of 1

Writing data to a file and failing

Posted: 26 May 2011 08:06
by mid_life_crisis
I'm trying to read each line from one file, strip out one character, which effectively shifts the remaining characters one down, then write the trimmed line to a second file. From what little I know of batch files, this should work. The first part, reading the lines, works. I just can't get the syntax right for trimming the line and writing it to the second file.

Can someone straighten me out, please?


for /F "tokens=*" %%H in (infile.txt) do
(
Set A=%%H%
Set A:~52=%%H:~53%
echo %A% >> outfile.txt
)

Re: Writing data to a file and failing

Posted: 26 May 2011 08:41
by !k

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%H in (infile.txt) do (
Set "A=%%H"
rem Set "A=!A:~52,1!"
rem echo,!A!>> outfile.txt
echo,!A:~52,1!>> outfile.txt
)

Re: Writing data to a file and failing

Posted: 26 May 2011 08:44
by dbenham
I don't think the !k solution gives what mid_life_crisis is looking for.

I believe this code fixes the problems inside the loop:

Code: Select all

for /F "tokens=*" %%H in (infile.txt) do (
  set "A=%%H"
  setlocal enableDelayedExpansion
  echo:!A:~0,52!!A:~53!>>outfile.txt
  endlocal
)

The SETLOCAL ENABLEDELAYED EXPANSION / ENDLOCAL is required inside the loop in case the input file contains the ! character.

You may still have problems with the loop definition depending on your requirements.

As written, the loop will
  • Skip empty lines (standard behavior of FOR /F)
  • Skip lines that start with a semicolon (default FOR /F "eol" option)
  • Strip leading whitespace from each line (default FOR /F "delims" option)

Dave Benham

Re: Writing data to a file and failing

Posted: 26 May 2011 09:08
by mid_life_crisis
dbenham wrote:I don't think the !k solution gives what mid_life_crisis is looking for.

I believe this code fixes the problems inside the loop:

Code: Select all

for /F "tokens=*" %%H in (infile.txt) do (
  set "A=%%H"
  setlocal enableDelayedExpansion
  echo:!A:~0,52!!A:~53!>>outfile.txt
  endlocal
)

The SETLOCAL ENABLEDELAYED EXPANSION / ENDLOCAL is required inside the loop in case the input file contains the ! character.

You may still have problems with the loop definition depending on your requirements.

As written, the loop will
  • Skip empty lines (standard behavior of FOR /F)
  • Skip lines that start with a semicolon (default FOR /F "eol" option)
  • Strip leading whitespace from each line (default FOR /F "delims" option)

Dave Benham



Thank you, this is much closer, except that the first part of each line is literally A:~0,52 in the output file

Re: Writing data to a file and failing

Posted: 26 May 2011 09:35
by dbenham
Fixed by replacing : with <space> after echo:

Code: Select all

  echo !A:~0,52!!A:~53!>>outfile.txt

I don't know why : or . doesn't work in this case after echo. It is frequently used after echo to enable printing of a blank line. But for some reason it is disabling the delayed expansion.

Since the for loop is stripping blank lines, the : is not needed anyway.

Dave Benham

Re: Writing data to a file and failing

Posted: 26 May 2011 09:54
by dbenham
I found an alternative fix that preserves the colon

Code: Select all

  call echo:!A:~0,52!!A:~53!>>outfile.txt

I am completely mystified why either fix is needed.

Experiments show that the fix is only needed if the delayed expansion expression immediately following ECHO: includes the substring expression with both the start and the length.

I started a new topic on this issue: Problem with delayed expansion after ECHO. or ECHO:

Dave Benham

Re: Writing data to a file and failing

Posted: 26 May 2011 11:29
by mid_life_crisis
Thank you again for all your help. This newest change works perfectly.