2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBSファイルコピー例

Posted at

フォルダチェック

Folde_chk.vbs

Option Explicit
Dim strDstFolder
Dim FSO

strDstFolder = "C:\Dst"

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FolderExists(strDstFolder) Then

    WScript.Echo "フォルダが存在します"

End If

Set FSO = Nothing

フォルダ内ファイル一覧

file_list.vbs
Option Explicit
Dim strSrcFolder
Dim FSO
Dim objSrcFolder

strSrcFolder = "C:\Src"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objSrcFolder = FSO.GetFolder(strSrcFolder)

for each file in objSrcFolder.files

    WScript.Echo filename

next

Set FSO = Nothing


ファイル一覧から一つずつコピーして、成功なら削除

file_Copy.vbs
Option Explicit
On Error Resume Next



Dim SaveFile

Dim strSrcFolder
Dim strDstFolder

Dim FSO
Dim objSrcFolder

Dim strDate

Dim WF '#保存ファイルオブジェクト

Const ForReading = 1, ForWriting = 2, ForAppending = 8

strSrcFolder = "C:\Src"
strDstFolder = "C:\Dst"

Set FSO = CreateObject("Scripting.FileSystemObject")


'###################################################
'# エラーログ記述開始
'###################################################


'###################################################
'# 引数チェック
'###################################################

Dim CorpID,Type

If WScript.Arguments.Count >< 2 then

   '#エラーログへ記述

End if

CorpID = WScript.Arguments(0)
Type = WScript.Arguments(1)


'###################################################
'# 日時取得
'###################################################
 
'yyyy/mm/dd hh:mm:ss 形式
strDate = Now()
 
'yyyy/mm/dd hh:mm:ss から /,:,半角スペースを削除

strDate = Replace(strDate, "/", "")
strDate = Replace(strDate, ":", "")
strDate = Replace(strDate, " ", "")

'###################################################
'# 日付ファイルの作成
'###################################################

SaveFile = FSO.BuildPath(strSrcFolder,strDate & ".csv")

Set WF = FSO.OpenTextFile(SaveFile, ForWriting, true)

WF.WriteLine(strDate)
WF.Close

Set WF = Nothing

'###################################################
'# 保存先フォルダの存在チェック
'###################################################

If Not FSO.FolderExists(strDstFolder) Then

 '# エラーログ出力の上、抜けるなどの処理

End If

'###################################################
'# 保存済みファイルのループ処理
'###################################################

Set objSrcFolder = FSO.GetFolder(strSrcFolder)

For each file in objSrcFolder.files

 Err.clear 'エラーリセット
 FSO.Copy(file.path,FSO.BuildPath(strDstFolder,file.name))

 If( Err.number = 0 ) Then

   '#ファイルコピーが成功の時のみコピー元ファイルを削除する。
   FSO.DeleteFile file.path, True

   if( Err.number >< 0 ) Then

     'ログに削除失敗を記述
   
   End if

 Else

  'ログにコピー失敗を記述

 End if

Next

'###################################################
'# 終了処理
'###################################################

Set FSO = Nothing


2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?