Solved Delete Text Out Of A File

March 8, 2012 at 00:33:54
Specs: Win 7 Pro, 4.17Ghz 8GB ram
Either vb script or batch?


I have a file that I would like to delete some text out of.

Input.txt:
id=1234
x=2
y=4
z=3
c=9
id=1235
x=6
y=2
z=8
c=8

i want to be able to delete just id 1234

NOTE this file could have hundreds of these

is there a way that what id it calls upon can be the input of command line switch?

thank you very much


See More: Delete Text Out Of A File

Report •


✔ Best Answer
March 19, 2012 at 14:56:41
You're taking the second argument as the file to process, and the last argument as the id.
Dim inName, id
If Not parseArgs(inName, id, 0) Then
  WScript.Echo "Usage: " & WScript.ScriptName & " [ID] [Input file]"
  WScript.Quit 1
End If
WScript.Quit Main(inName, "ID=" & id)

Function parseArgs(ByRef inName, ByRef id, offset) 'As Bool
  If Not WScript.Arguments.Count > offset + 1 Then _
    Exit Function  'Not enough args; fail.

  If LCase(WScript.Arguments(offset)) = "id" Then
    parseArgs = parseArgs(inName, id, offset + 1)
    Exit Function  '"id" detected; offset + 1, try again.
  End If

  'File name in arg list now known.
  inName = WScript.Arguments(offset + 1)
  'Determine if first arg is prefixed with "id="
  If Len(WScript.Arguments(offset)) < 4 Then
    id = WScript.Arguments(offset) 'arg too short to include "id="
  ElseIf LCase(Left(WScript.Arguments(offset), 3)) = "id=" Then
    id = Mid(WScript.Arguments(offset), 4) '"id=" found; remove
  Else
    id = WScript.Arguments(offset) 'assume entire arg is desired.
  End If
  parseArgs = True 
End Function

Function Main(inName, watchFor) 'As Integer
  writeLine = True
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set txtFile = fso.OpenTextFile(inName)
  Set outFile = fso.OpenTextFile("someOther.txt", 2, True)
  Do Until txtFile.AtEndOfStream
    line = txtFile.ReadLine
    If Left(line, 3) = "ID=" Then _
    writeLine = line <> watchFor
    If writeLine Then _
    outFile.WriteLine line
  Loop
  Main = 0
End Function

20 lines to parse arguments, and 13 to do the work? There's probably a better way. Oh well.

How To Ask Questions The Smart Way



#1
March 8, 2012 at 01:35:54
If this is your command line:
>MyBatch /delid=1234 /showcmds

Put this in your batch file to process any number of command line switches (switches may be listed in any order):

:CHECKFORSWITCH
  IF "%1"=="/?"  GOTO DISPHELP
  IF "%1"=="/h"  GOTO DISPHELP
  IF "%1"=="/H"  GOTO DISPHELP
  IF "%1"=="/delid"  GOTO GETDELETEID
  IF "%1"=="/DELID"  GOTO GETDELETEID
  IF "%1"=="/showcmds"  GOTO SHOWCMDSONLY
  IF "%1"=="/SHOWCMDS"  GOTO SHOWCMDSONLY
  IF "%1"==
  .
  .
GOTO READYTOGO

:GETDELETEID
  IF "%2"==""  GOTO BADSYNTAX
  SET Delete_ID=%2
  SHIFT
  SHIFT
GOTO CHECKFORSWITCH

:SHOWCMDSONLY
  SET Show_Commands=TRUE
  SHIFT
GOTO CHECKFORSWITCH

NOTE: First 8 characters of labels need to be unique (eg. ":GETDELET")

Report •

#2
March 8, 2012 at 14:00:17
I kinda knew how to do it in batch but i didn't in vb. Just in case some one knew how to do what i want to do in vb. I dont think it is possible in batch


Report •

#3
March 13, 2012 at 13:38:21
Quick and dirty VBScript:

writeLine = True
watchfor = "id=" & WScript.Arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("some.txt")
Set outFile = fso.OpenTextFile("someOther.txt", 2, True)
Do Until txtFile.AtEndOfStream
  line = txtFile.ReadLine
  If Left(line, 3) = "id=" Then _
    writeLine = line <> watchfor
  If writeLine Then _
    outFile.WriteLine line
Loop

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
March 17, 2012 at 17:05:12
I tried that it says subscript out of range

Script im using:

writeLine = True
watchfor = "ID=57030301-15" & WScript.Arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("input.txt")
Set outFile = fso.OpenTextFile("output.txt", 2, True)
Do Until txtFile.AtEndOfStream
  line = txtFile.ReadLine
  If Left(line, 3) = "ID=57030301-15" Then _
    writeLine = line <> watchfor
  If writeLine Then _
    outFile.WriteLine line
Loop

Input File:

ID=57030301-15
[Offsets]
X=-0.0002
Y=-0.0001
Z=-0.0006
Avg=-0.0032
C=0.0135
ID=57030301-16
[Offsets]
X=-0.0003
Y=0.0000
Z=-0.0005
Avg=-0.0033
C=0.0128
ID=57030301-17
[Offsets]
X=-0.0003
Y=0.0000
Z=-0.0005
Avg=-0.0032
C=0.0144
ID=57030303-6
[Offsets]
X=-0.0002
Y=0.0000
Z=-0.0005
Avg=-0.0028
C=0.0069
ID=57030303-7
[Offsets]
X=-0.0002
Y=0.0000
Z=-0.0006
Avg=-0.0027
C=0.0078


Report •

#5
March 18, 2012 at 05:05:33
That's because the script is looking for a command line argument. If you're not going to provide an argument, remove the "WScript.Arguments(0)" bit.

How To Ask Questions The Smart Way


Report •

#6
March 18, 2012 at 11:42:16
oh I'm sorry it didnt seem like you put the argument in it. But I dont know vbs so I should have tried it.

I did get it to run without an error but it just copies the file. both of them are exactly the same

Bat file that tells vbs to run:

@echo off
start delline.vbs id=57030301-15
pause

I am using the original vbs that you sent me.

Thank you very much for the help


Report •

#7
March 18, 2012 at 13:19:35
Two things of note:
1) The search is case sensitive. "id" ≠ "ID". I'm mentioning this because I've seen both used in this thread.
2) The "id=" bit is assumed, so you shouldn't include it on the command line. (Otherwise, you'd be searching for "id=id=57030301-15".)

Really, I should just add better argument logic, but it's 73°F (22°C) and sunny outside. That's far too nice to be inside.

EDIT: I forgot the equals sign counts as a token separator.

How To Ask Questions The Smart Way


Report •

#8
March 19, 2012 at 07:50:42
More hail outside means more time inside.
A little less quick, and a bit more dirty:
writeLine = True
If Not WScript.Arguments.Count > 0 Then
  WScript.Echo "Usage: " & WScript.ScriptName & " [ID]"
  WScript.Quit 1
Else
  Dim arg : arg = Split(WScript.Arguments(WScript.Arguments.Count - 1), "=")
  watchfor = "ID=" & arg(UBound(arg))
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("some.txt")
Set outFile = fso.OpenTextFile("someOther.txt", 2, True)
Do Until txtFile.AtEndOfStream
  line = txtFile.ReadLine
  If Left(line, 3) = "ID=" Then _
    writeLine = line <> watchfor
  If writeLine Then _
    outFile.WriteLine line
Loop

How To Ask Questions The Smart Way


Report •

#9
March 19, 2012 at 09:27:18
Yeah I was also outside it was almost 80 yesterday. Im loving these longer days.

But I did get the previous script to work this morning. What does this one do that the other doesn't? Just check if there was an argument or not?

I will try it when i get to work


Report •

#10
March 19, 2012 at 10:09:22
1) It checks to see if there is an argument, if not, bomb in a more user friendly way.
2) Replaced all instances of "id=" with "ID="
3) Grabbed anything to the right of any equals sign of the last argument. This means the following are valid:
script.vbs 57030301-15
script.vbs id=57030301-15
script.vbs "id=57030301-15"

How To Ask Questions The Smart Way


Report •

#11
March 19, 2012 at 12:35:35
Oh nice I like it. That helps for client error issue and my uneducated brain to vbs.


is there a way to add a second argument that will tell it what file to do it to?

im going to take a stab at it.

writeLine = True
input = WScript.Arguments(1)
If Not WScript.Arguments.Count > 0 Then
  WScript.Echo "Usage: " & WScript.ScriptName & " [ID]"
  WScript.Quit 1
Else
  Dim arg : arg = Split(WScript.Arguments(WScript.Arguments.Count - 1), "=")
  watchfor = "ID=" & arg(UBound(arg))
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile(input)
Set outFile = fso.OpenTextFile("someOther.txt", 2, True)
Do Until txtFile.AtEndOfStream
  line = txtFile.ReadLine
  If Left(line, 3) = "ID=" Then _
    writeLine = line <> watchfor
  If writeLine Then _
    outFile.WriteLine line
Loop


But thank you again


Report •

#12
March 19, 2012 at 14:56:41
✔ Best Answer
You're taking the second argument as the file to process, and the last argument as the id.
Dim inName, id
If Not parseArgs(inName, id, 0) Then
  WScript.Echo "Usage: " & WScript.ScriptName & " [ID] [Input file]"
  WScript.Quit 1
End If
WScript.Quit Main(inName, "ID=" & id)

Function parseArgs(ByRef inName, ByRef id, offset) 'As Bool
  If Not WScript.Arguments.Count > offset + 1 Then _
    Exit Function  'Not enough args; fail.

  If LCase(WScript.Arguments(offset)) = "id" Then
    parseArgs = parseArgs(inName, id, offset + 1)
    Exit Function  '"id" detected; offset + 1, try again.
  End If

  'File name in arg list now known.
  inName = WScript.Arguments(offset + 1)
  'Determine if first arg is prefixed with "id="
  If Len(WScript.Arguments(offset)) < 4 Then
    id = WScript.Arguments(offset) 'arg too short to include "id="
  ElseIf LCase(Left(WScript.Arguments(offset), 3)) = "id=" Then
    id = Mid(WScript.Arguments(offset), 4) '"id=" found; remove
  Else
    id = WScript.Arguments(offset) 'assume entire arg is desired.
  End If
  parseArgs = True 
End Function

Function Main(inName, watchFor) 'As Integer
  writeLine = True
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set txtFile = fso.OpenTextFile(inName)
  Set outFile = fso.OpenTextFile("someOther.txt", 2, True)
  Do Until txtFile.AtEndOfStream
    line = txtFile.ReadLine
    If Left(line, 3) = "ID=" Then _
    writeLine = line <> watchFor
    If writeLine Then _
    outFile.WriteLine line
  Loop
  Main = 0
End Function

20 lines to parse arguments, and 13 to do the work? There's probably a better way. Oh well.

How To Ask Questions The Smart Way


Report •

#13
March 20, 2012 at 11:11:27
Haha thats usually how my work is too. 2x the lines just in case of user error and only a little bit actually doing the work.

I tried that and i get an error

line: 33
char: 3
error: invalid procedure call or argument
code: 800A0005
Source: Microsoft VBScript runtime error

fixed the error
this:

Set txtFile = fso.OpenTextFile(input)

is supposed to be this:
Set txtFile = fso.OpenTextFile(inName)


the batch file is:

@echo off
testdel.vbs 57030301-15 .\input.txt
pause
exit

but it just copies the file now. does not actually remove the id

any suggestions?


Report •

#14
March 20, 2012 at 11:33:20
Yeah, I don't know how I missed that when I was editing my forums-friendly version of the code. Sorry.

As for how it's not working, all I can tell you is it works for me with the sample data you posted in #4. Is the input UTF-16? VBScript treats files as ASCII by default.

Tue 2012.03.20 14.28.22
H:\>testdel.vbs 57030301-15 some.txt

Tue 2012.03.20 14.28.38
H:\>fc some.txt someOther.txt
Comparing files some.txt and SOMEOTHER.TXT
***** some.txt
ID=57030301-15
[Offsets]
X=-0.0002
Y=-0.0001
Z=-0.0006
Avg=-0.0032
C=0.0135
ID=57030301-16
[Offsets]
***** SOMEOTHER.TXT
ID=57030301-16
[Offsets]
*****

How To Ask Questions The Smart Way


Report •

#15
March 20, 2012 at 11:49:06
ok there we go I got it.

It was me being dumb again and forgetting that it was case sensitive. But I got it working. Now to make it more user friendly with a nice batch file haha.


That's the main reason why I needed the command line switches built in.

Hey so if one were to want to learn VBS where would he go?


Report •

#16
March 20, 2012 at 12:41:52
The old answer was TechNet for techniques/basics/miscellaneous scriptable objects, and MSDN for reference, but MS phased that out to promote PowerShell. (The scripting environment that doesn't run scripts by default.) If you can push past that flaw, and you're supporting Vista+ environments, it is a nicer and more powerful scripting language than VBScript/JScript.

Your best bet would be to get a book about managing Windows though scripts, just make sure it isn't about PowerShell scripts. If you do get a pure VBScript book, make sure it's not about ASP.

How To Ask Questions The Smart Way


Report •

#17
March 20, 2012 at 13:12:02
OK Cool thank you. I will check those out

Im kinda wanting to more with Java/ JScript I know the 2 are completely different but both are multi-platform which I like.


Report •

#18
March 20, 2012 at 13:30:27
I suspect you're confusing three separate languages.

Java is a programming language, complies code into byte code, and said byte code requires the JVM to run.

JavaScript has no relation to Java, and its name was picked to ride on Java's coat tails. (It worked.) It requires modern-ish browser to work, and every browser handles JavaScript and DOM a bit differently. Use frameworks.

JScript is Microsoft's old version of JavaScript, with some added features to promote IE back in the day. It's sitting in the same boat as VBScript. (This boat is drifting out to sea.)

How To Ask Questions The Smart Way


Report •


Ask Question