Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I am new to DOS programming.
I have the following text file:apple
boyPT(1,004)= 75.0D0, -2.70D0,
catdog
What i want to do is edit the portion of the text -2.70D0, to other values like -2.75D0, -2.80D0 and so on. (There are 8 spaces in front of the PT(1,004) and they have to be kept intact too).From the previous post
http://www.computing.net/answers/pr...I tried out the code:
@echo off > newfile
setLocal EnableDelayedExpansionfor /f "tokens=* delims=@" %%a in (input.txt) do (
if "%%a"==" PT(1,004)= 75.0D0, -2.70D0," (
echo: PT(1,004)= 75.0D0, -2.75D0,>> newfile
) else (
echo %%a >> newfile
)
)I used the delimiter @ as it is non-existent in the text file. When I execute the code, there are 2 problems:
1. Error message saying "Incorrect usage of 75.0D0".
2. Blank lines are removed in the newfile.
Please advise on the error message and how can I keep the blank lines when output to newfile. (Many posts in this forum are about removing blank lines).
I am using windows XP Japanese version.
As a side question, what is the representation of the CR or LF in DOS program?Thanks in advance for your advice and help.

@echo off>newfile for /f "skip=2 tokens=1* delims=]" %%a in ('find /v /n "" input.txt') do ( if "%%b"=="PT(1,004)= 75.0D0, -2.70D0," (echo PT^(1,004^)= 75.0D0, -2.75D0,) else ( if "%%b"=="" (echo.) else (echo %%b) ) )>>newfile type newfileto escape special character when echo-ing, use the caret ^ sign

@echo off & setLocal EnableDelayedExpansion
@echo off > newfile
setLocal EnableDelayedExpansionfor /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^<input.txt') do (
set str=%%b
if not "%%b"=="" set str=!str:70D0=75D0!
echo.!str!>> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

Hi reno,
I guess I need to think faster.
:(
=====================================
If at first you don't succeed, you're about average.M2

hehe, your code is simpler.
echo.!str!>>newfilei just know that trick in batch script. saving on if statement, thus reducing cpu cycle. =)

There is one thing to consider when using find this way - lines that begin with "]" will have any and all closing square brackets until another character filtered out.
Not very common but worth a mention.

Wow, thanks Judago, I hadn't thought of that. I will add this caveat to the HowTo that describes this technique.

Hi guys,
Thanks for all the help. You guys are awesome. Sorry for late reply as I was testing out the code proposed by Reno and Mech. Mech's code produces the desired output. For Reno's code, it added an extra blank line at the start of the output file, and also at 4 other different places in the output file, added the text "echo is off". The input file contains quite a lot of text so I am unable to post it. As I am new to DOS programming, it would really be great if some advisory comments to the code be added. For instance in Mech's codefor /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^<input.txt') do (
set str=%%b
if not "%%b"=="" set str=!str:70D0=75D0!
echo.!str!>> newfile
)
I only know the ('find /v /n "" ^<input.txt') part will prints out the input file and add row numbers contained in the square brackets []. But I can't figure out why the tokens is 1*, and the rest of the code. But once again, thanks for all your help.

suitto,
This is how you can learn batch commands:
Goto start menu, select Run and type "cmd.exe" and hit Enter.
Now a command window (or console) pops up with text display.Now, to learn about the 'for' command, you would type "for /?" and press enter in this command prompt.
Similarly, type "set /?", "if /?" etc on this command prompt.Try to understand what it displays and relate it to the commands in the above batch file.
After this, if you can not figure out something specific, ask here and people here would be glad to help you.--
Holla.

Don't feel left out if the syntax is a bit mystifying. It took me years before the fog began to lift.
the for /f, in this case, breaks the line into tokens. Call them chunks. The default delimiter, that defines the chunks, is white space. For our purpose here we want to chunk it up by square brackets.
tokens=1* assigns the number in square bracket to %%a and the rest of the line to %%b.
Notice that we never cared about the number; it's simply a way to avoid losing blank lines.
=====================================
If at first you don't succeed, you're about average.M2

Thanks Holla and Mech for putting in advisory and encouraging words. Mech, with your hints, the code is clearer now. But one part still remains puzzling. This statement
if not "%%b"=="" set str=!str:70D0=75D0!
replaces the text "70D0" to "75D0", excluding those blank lines (The exclamation marks allowing delayed environment variable expansion).
What I don't understand is the use of 'str:' with 'set str='.
I read the help on the command set but it does not describe anything about the use of colon. What does colon means in DOS command?
Once again, thanks for your help.

I don't know where the SET use of colon is documented. Maybe IVO does.
=====================================
If at first you don't succeed, you're about average.M2

Hi again,
The reason I asked is because if an equal sign exists in the textstring to be replaced, the command doesn't worked. Even adding a carat before the equal sign does not help. For instance, if I want to change value of k from 1 to 2if not "%%b"=="" set str=!str:k^=1=k^=2!
replaces k=1 to produce the output1=k=2=1
Is it that the EnableDelayedExpansion is not working?
Thanks

I made a test file with this content:
a=b
c=d
And thie bat seems OK; ned more testing.======================================
@echo off > newfile & setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (myfile2) do (
set str=%%a
set str=!str:a=x!
echo !str!
)
=====================================
If at first you don't succeed, you're about average.M2

M2, try replacing the substring "a=b" with "c=d". What do you do?
set str=!str:a=b=c=d! ????
P.S. There's probably an answer here, I just haven't tried it.

suito,
Somewhere down in the help for set command,
it says (set/?):Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1. May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. [.........]--
Holla.

![]() |
java printStackTrace()
|
How to find the Run Dialo...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |