Writing data to a file and failing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Writing data to a file and failing

#1 Post by mid_life_crisis » 26 May 2011 08:06

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
)

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Writing data to a file and failing

#2 Post by !k » 26 May 2011 08:41

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
)
Last edited by !k on 26 May 2011 08:56, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Writing data to a file and failing

#3 Post by dbenham » 26 May 2011 08:44

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

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Re: Writing data to a file and failing

#4 Post by mid_life_crisis » 26 May 2011 09:08

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Writing data to a file and failing

#5 Post by dbenham » 26 May 2011 09:35

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Writing data to a file and failing

#6 Post by dbenham » 26 May 2011 09:54

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

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Re: Writing data to a file and failing

#7 Post by mid_life_crisis » 26 May 2011 11:29

Thank you again for all your help. This newest change works perfectly.

Post Reply