【问题标题】:Using Xcopy In A Vbs Script but skip copied files在 Vbs 脚本中使用 Xcopy 但跳过复制的文件
【发布时间】:2014-06-18 05:17:53
【问题描述】:
Set oShell = CreateObject("WScript.Shell")
strCommand = oShell.Run("Xcopy ""p:\"" ""d:\12"" /S /Y", 0, True)
If strCommand <> 0 Then
MsgBox "Error: Not_Copy" & strCommand
Else
MsgBox "Copy_Done"

End If

我使用上面的 vb 脚本将我的所有数据从笔式驱动器复制到“d:\12”文件夹。我有一个问题。

很多时候,我在 pendrive 中有相同文件夹路径的相同文件,例如“p:\”中的“java-setup.exe”以及“d:\12”中的“java-setup.exe”。 当我播放这个VBScript文件时,它会替换所有文件,浪费了很多时间。

是否有任何选项使其停止替换文件(跳过已复制的文件),而只关注那些未复制的文件?

我是脚本新手,初学者。所以请给直接编码,不要这样,

$srcdir = "\\server\source\";
$destdir = "C:\destination\";
$files = (Get-ChildItem $SrcDir -recurse -filter *.* | where-object {-not ($_.PSIsContainer)});
$files|foreach($_){
if (!([system.io.file]::Exists($destdir+$_.name))){
            cp $_.Fullname ($destdir+$_.name)
};

很多时候看不懂。

我的笔式驱动器路径是“p:\”,我必须复制的文件是“d:\12”

请提供脚本。

【问题讨论】:

标签: vbscript


【解决方案1】:

我找不到满足您需要的XCOPY 开关。不过,这个 VBScript 应该可以工作。

With CreateObject("Scripting.FileSystemObject")
    For Each File In .GetFolder("P:\").Files
        If Not .FileExists("D:\12\" & File.Name) Then
            .CopyFile File.Path, "D:\12\" & File.Name
        End If
    Next
End With

【讨论】:

  • 请阅读我上面的问题,我编辑了它。并尝试给出答案是你知道的。谢谢。
  • 我们不是来为您编写脚本的。我们在这里帮助编写脚本。但是,如果您对脚本一无所知,您就会意识到这是一个完整的脚本!
【解决方案2】:

是的,正如其他人所说,xcopy 不支持这一点。但是有批处理命令可以。

将其放入(“CopyWithoutReplace.bat”)

For %%F In ("P:\*.*") Do If Not Exist "D:\12\%%~nxF" Copy "%%F" "D:\12\%%~nxF"

还有你修改​​过的脚本:

Set oShell = CreateObject("WScript.Shell")
strCommand = oShell.Run("P:\CopyWithoutReplace.bat", 0, True)
If strCommand <> 0 Then
MsgBox "Error: Not_Copy" & strCommand
Else
MsgBox "Copy_Done"
End If

你也可以使用Robocopy

Robocopy 或“Robust File Copy”是一个命令行目录复制命令。从 Windows NT 4.0 开始,它已作为 Windows 资源工具包的一部分提供,并作为 Windows Vista、Windows 7 和 Windows Server 2008 的标准功能引入。

Or you could use Powershell

$srcdir = "\\server\source\";
$destdir = "C:\destination\";
$files = (Get-ChildItem $SrcDir -recurse -filter *.* | where-object {-not ($_.PSIsContainer)});
$files|foreach($_){
if (!([system.io.file]::Exists($destdir+$_.name))){
            cp $_.Fullname ($destdir+$_.name)
};

最后:Here is an entire SO Thread on copying without replacing.

【讨论】:

  • 请阅读我上面的问题,我编辑了它。并尝试给出答案是你知道的。谢谢。
猜你喜欢
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 2014-05-29
相关资源
最近更新 更多