1

I am working with iMacro JavaScript .js files which are simple unencrypted lines of code-like text. The file contains the following code:

var i;
for (i = 1; i <= 10; i++)
{
iimSet("loop", i);
iimPlay("ABC.iim");
}

This loop repeats 10 times. I want to take input from user in a batch file like say:

set /p ID="Enter Number: " %=%

and replace this 10 with the number entered by user i.e %ID%

I have found this question: How can you find and replace text in a file using the Windows command-line environment?

But, for some reason, I have to use built-in functions available in the Windows command prompt (nothing external like PowerShell, etc.) Have a look.

Something I tried (from above answers; didn't work)

cscript replace.vbs "C:\XYZ.js" "i <= 10" "i <= %ID%"
  • Please avoid unnecessary formatting: it just makes your question harder to read. Could you explain what "text containing non-txt file" means? Also, I guess by "DOS" you actually mean the command prompt so I'm tagging this as [batch] instead. – and31415 Jul 23 '14 at 14:33
  • @and31415 and since he's looking at a vbscript solution in his question, it's not even batch so much.. more like cmd.exe / command line. and vbscript. – barlop Jul 23 '14 at 14:34
  • It doesn't say they're looking specifically for vbscript... it looks like they've tried the vbscript through the answer they've read. The question body also suggests they could use any solution that isn't PowerShell etc. – Kinnectus Jul 23 '14 at 14:37
  • I tried to make it more palatable and easily understandable by formatting. "text containing non-txt file" means a file which has text/basic unecrypted code but doesn't have a .TXT extension. Anyway. Yes its BATCH. No i am not looking for Vb solution only. – SuperUserMan Jul 23 '14 at 14:54
1

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>
  • Thanks Barlop for answer. I just checked again and it worked. I also gotto work another solution from that link named "Script/batch utility called REPL.BAT ". But i have an associated query. Can you kindly read my latest comment on other answer of this question. Thanks Plz – SuperUserMan Jul 23 '14 at 16:35
  • I know i have asked many questions already, but if you can help with something similar. This is for Search and Add . Say i want to add another line of code iimPlay("DEF.iim"); below line of iimPlay("ABC.iim"); in this .jsfile, What batch code do i need? I can open another question if u say :). Sorry for many questions :) @barlop – SuperUserMan Jul 23 '14 at 20:39
  • thanks for ur time and persistence with such long replies. I am still reading and comprehending..silly me :P. Can you have a look here > superuser.com/questions/787262/… i know now i am trying to hire you for free... but if possible have a look.... Thanks – SuperUserMan Jul 24 '14 at 12:28
1

Unfortunately this answer doesn't do any "searching", but your file could be easily re-written in a small number of lines, so it may be easier to re-write the file contents...?

set /p ID="Enter Number: "
@echo off
@echo var i;> javascript.js
@echo for (i = 1; i ^<^= %ID%; i++)>> javascript.js
@echo {>> javascript.js
@echo iimSet("loop", i);>> javascript.js
@echo iimPlay("ABC.iim");>> javascript.js
@echo }>> javascript.js

This is a bat file so, for example, js.bat

  • I understand what you've done there, but care to explain your answer? – barlop Jul 23 '14 at 14:00
  • The OP's file only has a small number of lines of code (as written). My code takes the number of loops required (set /p ID="Enter Number: ") and it simply re-writes the OP's javascript file but inserts the number of loops from the batch variable into the loop definition and echos out the rest of the file to a file named javascript.js - not sure how else to explain it... :S – Kinnectus Jul 23 '14 at 14:03
  • Thanks. I haven;t tried it as i got another solution. But i plan on adding more content to that batch file. Would it be still easier in that case? – SuperUserMan Jul 23 '14 at 14:22
  • It's totally up to you how you do it... perhaps a "find & replace" function may be easier - as you now say you'll be extending the javascript.js file with additional content... the solution I have submitted could become quite cumbersome if your JS file becomes quite large... a "search -> replace" solution may be better. I was just going on what you had put in your question :) – Kinnectus Jul 23 '14 at 14:28
  • Thanks 4 reply. Chris can i ask you something associated? After this batch file ends, value gets replaced from 10 to %ID% (user input) and gets stored in a file. I need to re-run the same batch for another user. This time i cant use search for "i <= 10" as its value in file has already changed to "i <= %ID%". So how should i go about finding value %ID% just after "i <=". Some findstring command of sorts? TIA – SuperUserMan Jul 23 '14 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.