0

これが私の基準に一致する私が見つけたコードです。以下のコードは、ファイルをソースパスからターゲットパスにコピーするために使用されます。

暗黙の条件:ファイルがターゲットパスに存在しない場合、またはファイルは存在するが古い場合にのみ、ソースパスとターゲットファイルが上書きされます。

このコード内でターゲットファイルを実行して、ファイルが上書きされているとき、またはターゲットファイルが新しくコピーされたときにのみターゲットファイルが実行されるようにするにはどうすればよいですか?

Option Explicit

Dim WshShell
Dim fso
Dim USERPROFILE
Dim srcPath
Dim tgtPath
On Error Resume Next

Set WshShell = WScript.CreateObject("Wscript.Shell")
Set fso = WScript.CreateObject("Scripting.FilesystemObject")
'USERPROFILE = WshShell.ExpandEnvironmentStrings("%USERPROFILE%")

srcPath = "C:\test.exe"
tgtPath = "D:\"

If Not fso.FileExists(tgtPath) Then
fso.CopyFile srcPath, tgtPath, True
ElseIf fso.FileExists(srcPath) Then
ReplaceIfNewer srcPath, tgtPath
End If

Sub ReplaceIfNewer(strSourceFile, strTargetFile)
Const OVERWRITE_EXISTING = True

Dim objFso
Dim objTargetFile
Dim dtmTargetDate
Dim objSourceFile
Dim dtmSourceDate

Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objTargetFile = objFso.GetFile(strTargetFile)
dtmTargetDate = objTargetFile.DateLastModified
Set objSourceFile = objFso.GetFile(strSourceFile)
dtmSourceDate = objSourceFile.DateLastModified
If (dtmTargetDate < dtmSourceDate) Then
objFso.CopyFile objSourceFile.Path, objTargetFile.Path,OVERWRITE_EXISTING
End If
Set objFso = Nothing
End Sub
4

1 に答える 1

0

ここにあなたのスクリプトの言い換えられて機能しているバージョンがあります

option explicit 

dim WshShell, fso, srcPath, tgtPath , file, objFileSrc, objFileTgt
const OVERWRITE = true

set WshShell = WScript.CreateObject("Wscript.Shell") 
set fso = WScript.CreateObject("Scripting.FilesystemObject")

file = "test.exe"
srcPath = "c:\" 
tgtPath = "e:\" 

if fso.FileExists(srcPath & file) then
  if not fso.FileExists(tgtPath & file) then
    'target doesn't exist, just copy
    fso.CopyFile srcPath & file, tgtPath
    wscript.echo srcPath & file & " copied to " & tgtPath & file
  else
    Set objFileSrc = fso.getFile(srcPath & file)
    Set objFileTgt = fso.getFile(tgtPath & file)
    'target exists, compare dates
    if objFileSrc.DateLastModified > objFileTgt.DateLastModified then
      fso.CopyFile srcPath & file, tgtPath, OVERWRITE
      wscript.echo srcPath & file & " copied over " & tgtPath & file
    else
      wscript.echo srcPath & file & " not newer then " & tgtPath & file
    end if
  end if
else
  wscript.echo srcPath & file & " does not exist"
end if
set fso = Nothing 
于 2012-06-11T10:47:49.670 に答える