That vbscript file you give in your question works
You could say set /p yyy=input number: or %ID% as you have.
Below I change i <= 4, to i <= %yyy%
yyy=44 so it becomes i <= 44
C:\blah>type a.a
var i;
for (i = 1; i <= 4; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
}
C:\blah>set yyy=44
C:\blah>replace a.a "i <= 4" "i <= %yyy%"
C:\blah>type a.a
var i;
for (i = 1; i <= 44; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
}
C:\blah>
So just make a bat file with two lines.
One with set /p... and one with the replace line.
Or just one line
C:\blah>set /p ggg= & replace a.a var trt
(will replace the text var, with the text trt)
Here is replace.vbs which you linked to. I see it executes even without invoking it with cscript. (tested in Win 7)
C:\blah>type replace.vbs
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CR/LF
objFile.Close
C:\blah>
UPDATE-
here is a method that just rewrite the one for line
@ECHO OFF
REM run like repla 4 to make i <= 4 e.t.c.
REM C:\blah>repla 4<ENTER>
for /f "tokens=1,*" %%f in (a.a) do @(
IF NOT %%f == for (
echo %%f %%g
) ELSE (
ECHO for ^(int i=0; i ^<= %1; i++^)
)
)
UPDATE 2-
but as for finding the id
This does the FIND-
it looks at the for linem splits it up (Tokenises it) using the offensive looking cmd.exe for statement. In this case I just used space as delimeter(though that could be improved). It can then find the number, but it find the number followed by a semicolon. The last line replace the semicolon with nothing, i.e. removes the stray semicolon.
@ECHO OFF
for /f "tokens=1-12" %%f in (a.a) do @(
IF %%f == for set num=%%l
)
echo %num:;=%
where I write replace it's using replace.vbs
C:\blah>type a.a
var i;
for (i = 1; i <= 10; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
}
C:\crp>findit
10
C:\blah>replace a.a "i <= 10" "i <= 20"
C:\blah>type a.a
var i;
for (i = 1; i <= 20; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
}
C:\blah>findit
20
C:\blah>
UPDATE 3
to add another line after a line.. that had me for a moment until I found the ; needed to be escaped! batch files aren't pretty
The file that you want to work with is a.a
The bat file you'll run is a2.bat in this case. but you can name it what you want
as you can see, it reads a.a and amends it adding a line after the line you mentioned.
you can run a3.bat too to do the same as a3.bat a3.bat looks a bit neater 'cos it's where I realised I could still get it working without going full blast escaping lots of stuff, so a3.bat is a little but less ugly looking than a2.bat but they do the same thing.
C:\blah>type a.a
var i;
for (i = 1; i <= 20; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
}
C:\blah>type a2.bat
@echo off
for /f "tokens=*" %%f in (a.a) do (
echo %%f
if %%f == iimPlay^(^"ABC.iim^"^)^; echo iimPlay^(^"DEF.iim^"^)^;
)
C:\blah>a2
var i;
for (i = 1; i <= 20; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
iimPlay("DEF.iim");
}
C:\blah>
I should point out, that bat file that adds a line, works non-destructively. So a.a is still the same. If you wanted a.a to be changed, you could do C:\blah>a2 >b.b<ENTER> C:\blah>copy b.b a.a<ENTER> to output the results of the batch file to a new file then copy it onto a.a.
Here is another version of the batch file that also works.. this time though I do copy
a3.bat and a2.bat give the same result.
and for the copy, I used copy /y, so that it won't ask to overwrite the file.
so running a2 >b.b or a3>b.b means the results go to b.b Then copy /t b.b a.a and it overwrites the original a.a file
C:\crp>type a3.bat
@echo off
for /f "tokens=*" %%f in (a.a) do (
echo %%f
if "%%f" == "iimPlay("ABC.iim");" echo iimPlay("DEF.iim"^)^;
)
C:\crp>a3
var i;
for (i = 1; i <= 20; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
iimPlay("DEF.iim");
}
-
just to show that a2 works the same
C:\crp>a2
var i;
for (i = 1; i <= 20; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
iimPlay("DEF.iim");
}
-
this could have been done with a3 >b.b too. As they give the same output.
C:\blah>a2 >b.b
C:\blah>copy /y b.b a.a
1 file(s) copied.
C:\blah>copy b.b a.a <-- you wouldn't do this line. i'm just showing the point of /y
Overwrite a.a? (Yes/No/All):
0 file(s) copied.
C:\blah>copy /y b.b a.a
1 file(s) copied.
C:\blah>
-
now you see a.a has changed to include the additional line
C:\blah>type a.a
var i;
for (i = 1; i <= 20; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
iimPlay("DEF.iim");
}
C:\blah>