1.

Solve : Deleting the first line or a string in batch.?

Answer»

Okay I need to find; Code: [Select]X983493853CC£$£FGG$In a text file, and delete it.
Or
Delete the first line of a text file.

I am not sure how to use vbs in batch, but if you are willing to tell then what would be great.

Thank you.I'm not sure replacing a string with nulls actually deletes the line. There MIGHT be that pesky carriage return/line feed to deal with.

Quote

strPath = "file.txt"
strFind = "find text" & VbCrLf Set objFso = CreateObject("Scripting.FileSystemObject")
Set iFile = objFso.OpenTextFile(strPath, 1, vbFalse)
strData = iFile.ReadAll iFile.Close strData = Replace(strData, strFind, "")
Set oFile = objFso.OpenTextFile(strPath, 2, vbFalse) oFile.Write(strData)

oFile.Close

Does this code even get interpreted correctly? Don't you need colons between the instructions if they occupy the same line? Readability still counts:

Code: [Select]Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set iFile = fso.OpenTextFile("file.txt", ForReading)
Set oFile = fso.OpenTextFile("fileout.txt", ForWriting, True)

Do While iFile.AtEndOfStream <&GT; True
str = iFile.ReadLine
If InStr(1, str, "X983493853CC£$£FGG$") = 0 Then 'check if string not EXIST
ofile.WriteLine str
End If
Loop

ifile.Close
ofile.Close

I took some liberties with the file names, but I'm sure you can make any changes necessary.

Good luck.

Note: the compare is case sensitive; Research the instr function on how to do a case-insensitive compare.Thanks for your time. I saved that code as de.vbs and APPLIED the appropriate changes.
I then made sure all files existed and then double CLICKED de.vbs.
This resulted in the following error:

Do I have to run the .vbs script through command prompt, and if so how do i do that?I cannot reproduce the error. My editor shows line 11 as ofile.WriteLine str
Try replacing with ofile.WriteLine(str). Otherwise, some characters may have been misplaced during the copy/paste operation.

Quote
Do I have to run the .vbs script through command prompt, and if so how do i do that?

No. The default of most machines is to use the wscript program as the interpreter. If you have a need or find it more convenient to run from the command line, the default can be changed to cscript by running WScript //H:CScript

All scripts can be started from the console or the run box. The default engine is used unless specified on the command line.

Note: there are some subtle differences between the two interpreters, especially with std i/o device operations (wscript has none) and how echo is handled (window vs. console).

Happy Scripting

Okay, I'm not sure why it is doing this.
This is useless now to me anyway, but thanks very much for your help.


Discussion

No Comment Found