1. ホーム
  2. スクリプト・コラム
  3. vbs

txtテキストファイルを操作するvbsの共通メソッドと関数コード

2022-02-07 22:55:31

'西飛燕(小西先生)作

'テキストファイルの操作、fsoオブジェクトの操作(ファイルオブジェクトの操作)

関数コード

ファイルを作成する

dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) 'The second parameter indicates whether to overwrite the target file if it exists
f.Write("Write content")
f.WriteLine("Write content and line feed")
f.WriteBlankLines(3) 'Write three blank lines (equivalent to pressing enter three times in the text editor)
f.Close()
set f = nothing
set fso = nothing

ファイルを開く、読み込む

dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) 'The second parameter 1 means read-only open, the third parameter means whether to create the target file if it does not exist
f.Skip(3) 'move the current position back three characters
f.SkipLine() 'move the current position to the first character of the next line, note: no parameters
response.Write f.Read(3) 'read three characters backwards from the current position and move the current position backwards by three characters
ReadLine() 'Read backwards from the current position until a line break is encountered (no line break is read) and move the current position to the first character of the next line, note: no arguments
ReadAll() 'Read backwards from the current position until the end of the file, and move the current position to the end of the file
if f.atEndOfLine then
  response.Write("the end of a line! ")
end if
if f.atEndOfStream then
  response.Write("The end of the file! ")
end if
f.Close()
set f = nothing
set fso = nothing

ファイルを開く、書き込む

dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) 'The second parameter 2 means rewrite, if it is 8 means append
f.Write("Write content")
f.WriteLine("Write content and line feed")
f.WriteBlankLines(3) 'Write three blank lines (equivalent to pressing enter three times in the text editor)
f.Close()
set f = nothing
set fso = nothing

ファイルが存在するかどうかを判断する

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
  response.Write("Target file exists")
else
  response.Write("The target file does not exist")
end if
set fso = nothing

ファイルの移動

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") 'The filename part of the two parameters can be different
set fso = nothing

ファイルをコピーする

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") 'The filename part of the two parameters can be different
set fso = nothing

ファイルの削除

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing

フォルダの作成

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
CreateFolder("C:\test") 'The parent folder of the target folder must exist
set fso = nothing

フォルダが存在するかどうかを判断する

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
  response.Write("Target folder exists")
else
  response.Write("Target folder does not exist")
end if
set fso = nothing

フォルダの削除

dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") 'The folder doesn't have to be empty
set fso = nothing


今回の記事は以上です。詳しくは、スクリプトハウスが公開しているvbsのtxt操作に関する過去の記事をご覧ください。