2

I'm using the following VBS script to delete the first n number for line from a file:

strInputFile = "*Filename.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
If Not objInputFile.AtEndOfStream Then
      objInputFile.SkipLine
Else
      WScript.Quit
End If
strContents = ""
While Not objInputFile.AtEndOfStream
      If strContents = "" Then
            strContents = objInputFile.ReadLine
      Else
            strContents = strContents & VbCrLf & objInputFile.ReadLine
      End If
Wend
objInputFile.Close
Set objInputFile = Nothing

Set objOutputFile = objFSO.CreateTextFile(strInputFile, True)
objOutputFile.Write strContents
objOutputFile.Close
Set objOutputFile = Nothing
Set objFSO = Nothing

How do I change the code so that instead of a constant input file it will be an argument when I start the program via CMD?

James Dunn
  • 8,064
  • 13
  • 53
  • 87
Druz
  • 85
  • 1
  • 11

2 Answers2

1

Have a look at wscript.arguments which provides access to the command line used to start the script.

if (wscript.arguments.count <> 1) then
    wscript.echo "Usage: dl2unc <drive-letter-path>"
    wscript.quit 1
end if

s = wscript.arguments.Item(0)
lit
  • 14,456
  • 10
  • 65
  • 119
0

change

strInputFile = "*filename.txt" 

to

strInputFile = WScript.Arguments(0)

and the first arg would be the file name.

Sorceri
  • 7,870
  • 1
  • 29
  • 38