Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在下面给出的代码中运行vbscript中的文件?_File_Date_Vbscript_Copy - Fatal编程技术网

File 如何在下面给出的代码中运行vbscript中的文件?

File 如何在下面给出的代码中运行vbscript中的文件?,file,date,vbscript,copy,File,Date,Vbscript,Copy,下面是我找到的符合我的条件的代码。下面的代码用于将文件从源路径复制到目标路径 暗示的条件: 仅当目标路径上不存在文件或文件存在但较旧时,才会覆盖源路径和目标文件 如何在此代码中运行目标文件,使目标文件仅在文件被覆盖或目标文件被新复制时运行 Option Explicit Dim WshShell Dim fso Dim USERPROFILE Dim srcPath Dim tgtPath On Error Resume Next Set WshShell = WScript.CreateO

下面是我找到的符合我的条件的代码。下面的代码用于将文件从源路径复制到目标路径

暗示的条件: 仅当目标路径上不存在文件或文件存在但较旧时,才会覆盖源路径和目标文件

如何在此代码中运行目标文件,使目标文件仅在文件被覆盖或目标文件被新复制时运行

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
选项显式
昏暗的地狱
模糊fso
Dim用户配置文件
昏暗的道路
Dim tgtPath
出错时继续下一步
设置WshShell=WScript.CreateObject(“WScript.Shell”)
设置fso=WScript.CreateObject(“Scripting.FilesystemObject”)
'USERPROFILE=WshShell.ExpandEnvironmentStrings(“%USERPROFILE%”)
srcPath=“C:\test.exe”
tgtPath=“D:\”
如果不存在fso.FileExists(tgtPath),则
fso.CopyFile srcPath,tgtPath,True
ElseIf fso.FileExists(srcPath)然后
替换路径,tgtPath
如果结束
子替换更新(strSourceFile、strTargetFile)
Const OVERWRITE_EXISTING=True
Dim objFso
Dim对象目标文件
Dim DTM目标日期
模糊对象源文件
暗DTM源数据
设置objFso=WScript.CreateObject(“Scripting.FileSystemObject”)
设置objTargetFile=objFso.GetFile(strTargetFile)
dtmTargetDate=objTargetFile.DateLastModified
Set objSourceFile=objFso.GetFile(strSourceFile)
dtmSourceDate=objSourceFile.DateLastModified
如果(dtmTargetDate
这里是您的脚本的重新编写和工作版本

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 

杰出的谢谢你,彼得。这就是我要找的。谢谢你的剧本,彼得。我想知道在复制Targetfile之后如何运行它。我尝试使用WshShell.Run tgtpath&file,但由于文件正在从网络复制到目标计算机,因此这对我不起作用。