Topic Options
#57920 - 03/10/21 09:08 AM help with a script to move a line
sygui Offline
OL Expert

Registered: 11/02/10
Posts: 170
I have this script that remove a line containing the word terms. Now, instead of removing this line, I want to move it to line one position 86. how can i do ?

Dim FSO
set FSO=CreateObject("Scripting.FileSystemObject")
Dim fileInput, fileOutput
Dim sTempPath, sTempName
Dim sLine
Dim i

Set fileInput=FSO.OpenTextFile(Watch.GetJobFilename, 1)
sTempPath=FSO.GetFile(Watch.GetJobFilename).ParentFolder.Path
sTempName=FSO.GetTempName
Set fileOutput=FSO.GetFolder(sTempPath).CreateTextFile(sTempName)

i=0
Do While not(fileInput.AtEndOfStream)
i=i+1
sLine=fileInput.ReadLine
if not (mid(sLine,1,5) = "terms") then
fileOutput.WriteLine(sLine)
end if
Loop

fileInput.Close
fileOutput.Close

FSO.DeleteFile Watch.GetJobFilename, true
FSO.MoveFile sTempPath+"\"+sTempName, Watch.GetJobFileName

Top
#57921 - 03/10/21 08:27 PM Re: help with a script to move a line [Re: sygui]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 650
Loc: Québec, Canada
If you file isn't gigabits in size, why don't you read the whole file into an array.

Then it is easy to remove one line and move it elsewhere.

After that simply rewrite the new file.

VBScript isn't the best language to handle arrays, might I suggest to do it in Javascript instead?

Here something to get you started:
Code:
/*********************************************************
 * Function move
 * 
 * Move an item from an array from one position to another
 * 
 * Parameters:
 *   - arr = the array to be modified
 *   - old_index = current line to be moved
 *   - new_index = where to move it 
 ********************************************************/
function move(arr, old_index, new_index) {
    while (old_index < 0) {
        old_index += arr.length;
    }
    while (new_index < 0) {
        new_index += arr.length;
    }
    if (new_index >= arr.length) {
        var k = new_index - arr.length;
        while ((k--) + 1) {
            arr.push(undefined);
        }
    }
     arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
   return arr;
}

var myFile = Watch.GetJobInfo(1).split("\n");
var theLine = -1;
for(var i = 0; i < myFile.length; i++){
      if(myFile[i].search("what you are looking for")>-1){
              theLine = i;
              break;
      }
}
move(myFile,theLine,1);
Watch.SetJobInfo(2,myFile.join("\n"));


Now how to use this in PPWatch? Simple

  • ...your process up to this point
  • Set Job Infos and Variables plugin where %1 is set to %c
  • the script
  • Create File plugin with %2 in it.
  • rest of your process...


P.S. To not steal glory from someone else, the function's code move can be found here
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57922 - 03/12/21 08:07 AM Re: help with a script to move a line [Re: sygui]
sygui Offline
OL Expert

Registered: 11/02/10
Posts: 170
wow it works. thks a lot

Top