1

I'm really new to batch scripting, so please be patient! In windows xp I have an executable which has input.in and output.out files. I would like to create a batch which reads the input file, modifies one or more lines, runs the .exe and stores the output in a folder whose name is dependent on the change made to the input file.

Say I have an input file with 4 lines like this:

32.0d0 ! first parameter
54.0d5 ! second parameter
1.5d-1 ! third parameter
11.0d0 ! fourth parameter

and I want to change the second parameter in the range 0.0->1.0 with step 0.1:

@echo off
setlocal EnableDelayedExpansion
set ReplaceLine1=2
set inpfile=input.in
set tempfile=input.temp
set increment=0.1
if exist "%tempfile%" del "%TempFile%"
for /L %%i in (1,1,11) do (
set /A param_value=(%%i-1)*increment
set /A Cnt2=0
for /f %%a in (%inpfile%) do (
set /A Cnt2+=1
if !Cnt2! geq %ReplaceLine1% echo %param_value% >> "%tempfile%" else echo %%a >> "%tempfile%"
)
copy /y "%tempfile%" "%inpfile%"
MD .\"param_value"
executable.exe < "%tempfile%" >> .\"param_value" 
)

does the above have any chance to get me somwhere close to where I would linke to go?

1 Answer 1

1

You'll face a fundamental problem that batch-maths deals ONLY with integers...

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET line=%1
SET start=%2
SET inc=%3
SET end=%4
IF NOT DEFINED end ECHO require line#, start, increment, end&GOTO :EOF 
SET inpfile=input.IN
SET tempfile=input.temp
FOR /L %%i IN (%start%,%inc%,%end%) DO (
 IF EXIST "%tempfile%" DEL "%tempfile%"
 FOR /f "tokens=1*delims=[]" %%a IN ('find /n /v "" ^<%inpfile%') DO (
  IF %%a==%line% (
  SET value=%%i
  SET value=!value:~0,-1!.!value:~-1!
  IF .%%i==!value! SET value=0!value!
  >>%tempfile% ECHO !value!
  ) ELSE (>>%tempfile% ECHO %%b)
 )
MD .\!value!
COPY %tempfile% .\!value!\inputdata.
)

I'm assuming that your input file does NOT contain the !comment and that no line commences "]" or "["

I'm simply copying the tempfile constructed to the file inputdata. in the new directory created. No doubt in your application you would replace this with executable.exe < "%tempfile%" >> .\"!value!\resultfilename`" (noting that your output will be to a FILE not a DIRECTORY...)

Sign up to request clarification or add additional context in comments.

5 Comments

If POWERSHELL is an option, it supports floating point data types. And I believe it comes with newer Win versions and is obtainable free for XP. <technet.microsoft.com/en-us/scriptcenter/powershell.aspx>
@Peter WrightThanks for quick answer!! i'm studying the code: i guess the instruction SET value=!value:~0,-1!.!value:~-1! is there to deal with the real numbering....right? can you give details on what it's doing please?
Batch ALWAYS works on strings. The mathematical functions work on strings-of-numerics converted to numeric representations then converted back to strings again. %var% represents the value of the variable var When a FOR command is encountered, the entire command is parsed, from the FOR to the closing parenthesis terminating the DO, which may be many lines away. As part of that procedure, %var% is replaced by the then-current value of var - that is, the PARSE-TIME value.
When DELAYEDEXPANSION mode is invoked by a SETLOCAL ENABLEDELAYEDEXPANSION statement, then !var! is evaluated as the RUN-TIME value of var - that is, as it changes during the FOR loop. The syntax !var:~m,n!is evaluated as the substring length n characters starting at character m of var. If m or n is negative, it refers to the character-count from the END of the string var. Hence value=!value:~0,-1!.!value:~-1! sets value to (its current value from the start (position "0") to 1 character from the end) + . + (the current value from 1 short of the end to the end)
For more info on substringing in batch see SET /? from the prompt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.