2017-06-29 32 views
1

OpenTextFileに問題があります 私は長い時間前にスクリプトを作成しましたが、その後昨日問題が発生し始めました。なぜこのようにしていたのか、混乱していました。私はLOL.txt Hi.txtの引数を指定して、コマンドを実行すると、ファイルと私に与えてエラーVbscript - OpenTextFileが動作しない

Set objArgs = WScript.Arguments 
myFile = objArgs(0) 
numberofTXT = objArgs(1) 
line = objArgs(2) 

Set f = CreateObject("Scripting.FileSystemObject").OpenTextFile(myFile, line) 
d = f.ReadLine 

Set objFSO=CreateObject("Scripting.FileSystemObject") 
outFile=numberofTXT 
Set objFile = objFSO.CreateTextFile(outFile,True) 
objFile.Write d & vbCrLf 
objFile.Close 

私はそれはCMDでのように、持っている1、それは1を取り、

Set f = CreateObject("Scripting.FileSystemObject").OpenTextFile(myFile, line) 

でそれを置きますThe Valuable
次に私は、このフォームに新しいですし、私は例が

を喜ばたい:私はそれが私はそれがファイル「Hi.txt」

Set objFSO=CreateObject("Scripting.FileSystemObject") 
outFile=numberofTXT 
Set objFile = objFSO.CreateTextFile(outFile,True) 
objFile.Write d & vbCrLf 
objFile.Close 

NOTEに記述する必要がTextFileのその後

d = f.ReadLine 

を読まなければなりません

EDIT:https://pastebin.com/Cd5z8BHH

:それはライン1とテキストファイルがで使用しようとするともっとそして1行

FILEを持って、より多くのでは動作しません。

+0

をので、あなたはLOL.txtファイルからすべての行を読み込み、コマンドラインでます。CscriptでHi.txtするためにそれらを書きたい:次に、次はコードスニペットは助けることができるコメント? – Hackoo

+0

私はLol.txtから1行を読み込み、Hi.txtに書きます.Lol.txtからHi.txtに2行書きたいと思います。 – DatChicken040

答えて

0

読むとOpenTextFile Method参照適用:

は、指定されたファイルを開くと、から読み出しへの書き込み、またはファイルに追加する を使用することができるテキストストリームオブジェクトを返します。

構文

object.OpenTextFile(filename[, iomode[, create[, format]]]) 

引数必要

  • object  。オブジェクトは常にFileSystemObjectの名前です。
  • filename必須。開くファイルを識別する文字列式。
  • iomode  任意。 3つの定数:ForReading,ForWritingまたはForAppendingのいずれかになります。
  • create  任意。指定されたファイル名が存在しない場合に新しいファイルを作成できるかどうかを示すブール値。値 は、新しいファイルが作成された場合はTrue、作成されていない場合はFalseです。 を省略すると、新しいファイルは作成されません。
  • format  任意。開いているファイルの形式を示すために使用される3つのトライステート値の1つ(ユニコード、TristateFalse = 0、ASCIIとしてファイルを開くために TristateUseDefault = -2としてファイルを開くためにTristateTrue = -1)が使用されます。 を省略すると、ファイルはASCIIとして開かれます。

設定が

iomode引数は、次のいずれかの設定を持つことができます。

Constant  Value Description 
ForReading 1  Open a file for reading only. You can't write to this file. 
ForWriting 2  Open a file for writing. 
ForAppending 8  Open a file and write to the end of the file. 

読むCreateTextFile Method参照を同様。

Const ForReading = 1 
Set objArgs  = WScript.Arguments 
myFile   = objArgs(0)   ' file to read 
numberofTXT  = objArgs(1)   ' file to write 
line    = objArgs(2)   ' line serial number to write into output file 
             ' (or number of lines?) 
Set objFSO  = CreateObject("Scripting.FileSystemObject") 

outFile=numberofTXT 
Set objFile = objFSO.CreateTextFile(outFile,True) 

Set f = objFSO.OpenTextFile(myFile, ForReading) 

lineindex = 1 
Do until f.AtEndOfStream 
    d = f.ReadLine 
    if lineindex = line Then  ' only take the line-th line 
     objFile.Write d & vbCrLf ' or objFile.WriteLine d 
     Exit Do ' transfers control to the statement immediately following Loop statement 
    End If 
    lineindex = lineindex + 1 
Loop 

objFile.Close 
f.Close 
関連する問題