WSH 同名のファイルを更新
WSHを利用したサンプルです。指定フォルダ(元)のファイルで指定フォルダ(先)のファイルを上書きます。上書きされるファイルはバックアップします。実際に編集したい処理に変更して下さい。
Dim map,fromDir,targetDir
fromDir = "C:\Users\xxx\Desktop\from"
targetDir = "C:\src"
Dim yyyymmddhhmmss
yyyymmddhhmmss = getDateTime(Now())
Dim objFSO
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
'連想配列の生成
Set map = CreateObject("Scripting.Dictionary")
'対象ファイル取得
getFromFile
'メイン処理実施
exeDir targetDir
'移動元ファイルの取得
Function getFromFile()
Dim objBaseDir
Dim objFile
'対象フォルダを処理
Set objBaseDir = objFSO.getFolder(fromDir)
'ファイル一覧
For Each objFile In objBaseDir.files
'項目の追加(キー,バリュー)
map.Add LCase(objFile.name), objFile.path
Next
End Function
'メイン処理
Sub exeDir(path)
Dim workDir
Dim workFile
Dim objDir
'サブフォルダの位置を取得
If path = fromDir Then
workDir = toDir + "\"
Else
workDir = toDir + "\" + Mid(path, Len(fromDir) + 2) + "\"
End If
'対象フォルダを処理
Set objBaseDir = objFSO.getFolder(path)
'ファイル一覧
For Each objFile in objBaseDir.files
workFile = LCase(objFile.name)
If map.Exists(workFile) Then
moveFile map.Item(workFile), path + "\", objFile.path, fromDir + "\" + yyyymmddhhmmss + "\"
'キーを指定して項目削除
map.Remove(workFile)
End If
Next
'フォルダ一覧
For Each objDir In objBaseDir.subFolders
'サブフォルダを処理
exeDir objDir.path
Next
End Sub
'ファイル移動
Sub moveFile(fromPath, toDir, bakupFile, backupDir)
'フォルダ存在チェック
If Not objFSO.FolderExists(backupDir) Then
'フォルダ作成
objFSO.CreateFolder backupDir
End If
'ファイル移動(フォルダ指定)
objFSO.MoveFile bakupFile, backupDir
wscript.echo fromPath
wscript.echo toDir
'ファイル移動(フォルダ指定)
objFSO.MoveFile fromPath, toDir
End Sub
'日時をyyyymmddhhmmss形式に変換
Function getDateTime(date)
getDateTime= Year(date)
getDateTime= getDateTime & Right("0" & Month(date) , 2)
getDateTime= getDateTime & Right("0" & Day(date) , 2)
getDateTime= getDateTime & Right("0" & Hour(date) , 2)
getDateTime= getDateTime & Right("0" & Minute(date) , 2)
getDateTime= getDateTime & Right("0" & Second(date) , 2)
End Function
Set objFSO = Nothing
ページのトップへ戻る