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
)
Writing data to a file and failing
Moderator: DosItHelp
Re: Writing data to a file and failing
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.
Re: Writing data to a file and failing
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:
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
Dave Benham
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
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Writing data to a file and failing
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
Fixed by replacing : with <space> after echo:
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
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
I found an alternative fix that preserves the colon
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
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
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Writing data to a file and failing
Thank you again for all your help. This newest change works perfectly.