0

As a part of an assignment, I need to run two for loops in parallel in batch script. How can I run two for loops in parallel in batch script. In brief, I am using the following code to replace the contents present in file 1 with the contents present in file 2 but in a parallel manner.

Code:

FOR /f ('use backqdelims=' %%G in `type D:\Users\krkarthi\Desktop\input1.txt`) && ('use backqdelims=' %%H in `type D:\Users\krkarthi\Desktop\input2.txt`) DO cscript D:\Users\krkarthi\Desktop\replace.vbs "D:\Users\krkarthi\Desktop\test.txt" "%%~G" "%%~H")

In the above script, input1.txt and input2.txt contains the data which are defined with G and H variables. I need the ouput to be generated in a test.txt file.

The code should be executed in such a way that G and H variables should take the inputs in parallel from input1.txt and input2.txt

Please let me me know if you need more clarification on this

abelenky
  • 63,815
  • 23
  • 109
  • 159
karthik
  • 417
  • 1
  • 7
  • 15
  • 1
    I'm very interested to see what people come up with, as to my knowledge, this isn't possible with traditional `for` loops running in a single script. – SomethingDark May 15 '18 at 13:11
  • I've seen something which may work... – LS_ᴅᴇᴠ May 15 '18 at 13:13
  • 1
    Your code is syntax wise and by (your) formatting a mess. You can't have for loops run in parallel - you can read several files in parallel with different input streams. But if you want to replace all lines in file 1 why however don't simply copy the file over? –  May 15 '18 at 13:18
  • Closely related: [How can two text files be read in parallel by a batch file?](https://stackoverflow.com/q/38214874) – aschipfl May 15 '18 at 13:57

2 Answers2

1

You may use FOR for input1 and get line after line from input2 with SET /P:

SETLOCAL ENABLEDELAYEDEXPANSION
(FOR /F %%G in (D:\Users\krkarthi\Desktop\input1.txt) DO (
    SET /P H=
    cscript D:\Users\krkarthi\Desktop\replace.vbs "D:\Users\krkarthi\Desktop\test.txt" "%%~G" !H!
))<D:\Users\krkarthi\Desktop\input2.txt

You will lose, however, argument handling for input2 (note !H!, not "%%~H").

Also, you must ensure same number of lines in both inputs.

If I were you, I would switch to vbscript.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
0

Here is my replace.vbs script

Const Readpurpose = 1
Const Writepurpose = 2

strServiceMonFile = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

set objFSO = createObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strServiceMonFile,Readpurpose)
strText = objFile.ReadAll
objFile.close

set objFile = objFSO.OpenTextFile(strServiceMonFile,Writepurpose)
strNewText = Replace(strText, strOldText, strNewText)
objFile.Write strNewText
objFile.close
karthik
  • 417
  • 1
  • 7
  • 15
  • This is answer or question ? – 4b0 Jun 15 '18 at 06:26
  • I am trying to use a **replace.vbs** script which is actually used to replace the contents in an existing file with new content. As one can see in the **replace.vbs** code, strText used to identify the existing file and read all the content in that file. strOldText is a content (a line) of the existing file which is to be replaced and strNewText is a new content (a line) which to be replaced with. I am using the vbscript to perform the replace operation and I am trying to use the batch file by using some loops to read the contents in parallel while executing vbscript – karthik Jun 15 '18 at 10:25