使用VBScript将文件从一个文件夹复制到另一个文件夹

使用VBScript将文件从一个文件夹复制到另一个文件夹,vbscript,Vbscript,谁能告诉我如何使用VBScript将文件从一个文件夹复制到另一个文件夹 我从互联网上提供的信息中尝试了下面的方法 dim filesys set filesys=CreateObject("Scripting.FileSystemObject") If filesys.FileExists("c:\sourcefolder\anyfile.txt") Then filesys.CopyFile "c:\sourcefolder\anyfile.txt", "c:\destfolder\"

谁能告诉我如何使用VBScript将文件从一个文件夹复制到另一个文件夹 我从互联网上提供的信息中尝试了下面的方法

dim filesys

set filesys=CreateObject("Scripting.FileSystemObject")

If filesys.FileExists("c:\sourcefolder\anyfile.txt") Then

filesys.CopyFile "c:\sourcefolder\anyfile.txt", "c:\destfolder\"

当我执行这个命令时,我得到了拒绝的许可

试试这个。它将检查目标文件夹中是否已存在该文件,如果已存在,将检查该文件是否为只读文件。如果文件为只读,则会将其更改为读写,替换该文件,然后再次使其为只读

Const DestinationFile = "c:\destfolder\anyfile.txt"
Const SourceFile = "c:\sourcefolder\anyfile.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
    'Check to see if the file already exists in the destination folder
    If fso.FileExists(DestinationFile) Then
        'Check to see if the file is read-only
        If Not fso.GetFile(DestinationFile).Attributes And 1 Then 
            'The file exists and is not read-only.  Safe to replace the file.
            fso.CopyFile SourceFile, "C:\destfolder\", True
        Else 
            'The file exists and is read-only.
            'Remove the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
            'Replace the file
            fso.CopyFile SourceFile, "C:\destfolder\", True
            'Reapply the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
        End If
    Else
        'The file does not exist in the destination folder.  Safe to copy file to this folder.
        fso.CopyFile SourceFile, "C:\destfolder\", True
    End If
Set fso = Nothing

这里有一个答案,基于(我认为是对)Tester101的答案的改进,用一个子程序表示,CopyFile行是一次而不是三次,并且准备在复制时更改文件名(没有硬编码的目标目录)。我还发现我必须在复制之前删除目标文件才能使其正常工作,但这可能是Windows7的问题。Echo语句是因为我没有调试器,如果需要,当然可以删除它

Sub CopyFile(SourceFile, DestinationFile)

    Set fso = CreateObject("Scripting.FileSystemObject")

    'Check to see if the file already exists in the destination folder
    Dim wasReadOnly
    wasReadOnly = False
    If fso.FileExists(DestinationFile) Then
        'Check to see if the file is read-only
        If fso.GetFile(DestinationFile).Attributes And 1 Then 
            'The file exists and is read-only.
            WScript.Echo "Removing the read-only attribute"
            'Remove the read-only attribute
            fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
            wasReadOnly = True
        End If

        WScript.Echo "Deleting the file"
        fso.DeleteFile DestinationFile, True
    End If

    'Copy the file
    WScript.Echo "Copying " & SourceFile & " to " & DestinationFile
    fso.CopyFile SourceFile, DestinationFile, True

    If wasReadOnly Then
        'Reapply the read-only attribute
        fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
    End If

    Set fso = Nothing

End Sub

刚刚发布了我完成的类似项目代码。它在我的代码中复制某些扩展名的文件pdf tif和tiff如果只需要1或2种类型,您可以将它们更改为您想要复制的任何内容,或者删除if语句。当一个文件被创建或修改时,它会获得存档属性。这段代码也会查找该属性,并且只复制该属性(如果该属性存在),然后在复制后将其删除,这样就不会复制不需要的文件。它还有一个日志设置,这样您就可以看到一个日志,记录从上次运行脚本时起,每件事情的传输时间和日期。希望有帮助!
用于复制单个文件的链接为

,以下是代码:

Function CopyFiles(FiletoCopy,DestinationFolder)
   Dim fso
                Dim Filepath,WarFileLocation
                Set fso = CreateObject("Scripting.FileSystemObject")
                If  Right(DestinationFolder,1) <>"\"Then
                    DestinationFolder=DestinationFolder&"\"
                End If
    fso.CopyFile FiletoCopy,DestinationFolder,True
                FiletoCopy = Split(FiletoCopy,"\")

End Function
函数复制文件(FiletoCopy,DestinationFolder)
模糊fso
Dim文件路径,WarFileLocation
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果正确(DestinationFolder,1)“\”则
DestinationFolder=DestinationFolder&“\”
如果结束
fso.CopyFile FiletoCopy,DestinationFolder,True
FiletoCopy=Split(FiletoCopy,“\”)
端函数

请查找以下代码:

If ComboBox21.Value = "Delimited file" Then
    'Const txtFldrPath As String = "C:\Users\513090.CTS\Desktop\MACRO"      'Change to folder path containing text files
    Dim myValue2 As String
    myValue2 = ComboBox22.Value
    Dim txtFldrPath As Variant
    txtFldrPath = InputBox("Give the file path")
    'Dim CurrentFile As String: CurrentFile = Dir(txtFldrPath & "\" & "LL.txt")
    Dim strLine() As String
    Dim LineIndex As Long
    Dim myValue As Variant
    On Error GoTo Errhandler
    myValue = InputBox("Give the DELIMITER")

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    While txtFldrPath <> vbNullString
        LineIndex = 0
        Close #1
        'Open txtFldrPath & "\" & CurrentFile For Input As #1
        Open txtFldrPath For Input As #1
        While Not EOF(1)
            LineIndex = LineIndex + 1
            ReDim Preserve strLine(1 To LineIndex)
            Line Input #1, strLine(LineIndex)
        Wend
        Close #1

        With ActiveWorkbook.Sheets(myValue2).Range("A1").Resize(LineIndex, 1)
            .Value = WorksheetFunction.Transpose(strLine)
            .TextToColumns Other:=True, OtherChar:=myValue
        End With

        'ActiveSheet.UsedRange.EntireColumn.AutoFit
        'ActiveSheet.Copy
        'ActiveWorkbook.SaveAs xlsFldrPath & "\" & Replace(CurrentFile, ".txt", ".xls"), xlNormal
        'ActiveWorkbook.Close False
       ' ActiveSheet.UsedRange.ClearContents

        CurrentFile = Dir
    Wend
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End If
如果ComboBox21.Value=“分隔文件”,则
'Const txtFldrPath As String=“C:\Users\513090.CTS\Desktop\MACRO”'更改为包含文本文件的文件夹路径
将myValue2设置为字符串
myValue2=ComboBox22.Value
尺寸txtFldrPath作为变型
txtFldrPath=InputBox(“给出文件路径”)
'Dim CurrentFile As String:CurrentFile=Dir(txtFldrPath&“\”和“LL.txt”)
Dim strLine()作为字符串
变暗线索引为长
Dim myValue作为变量
关于错误转到错误处理程序
myValue=InputBox(“给出分隔符”)
Application.ScreenUpdating=False
Application.DisplayAlerts=False
而txtFldrPath vbNullString
LineIndex=0
关闭#1
'打开txtFldrPath&“\”&CurrentFile以作为#1输入
打开输入为#1的txtFldrPath
而不是EOF(1)
LineIndex=LineIndex+1
ReDim保留strLine(1到行索引)
行输入#1,strLine(行索引)
温德
关闭#1
使用ActiveWorkbook.Sheets(myValue2).Range(“A1”).Resize(LineIndex,1)
.Value=工作表函数.Transpose(strLine)
.TextToColumns Other:=True,OtherChar:=myValue
以
'ActiveSheet.UsedRange.entireclumn.AutoFit
'ActiveSheet.Copy
'ActiveWorkbook.SaveAs xlsFldrPath&“\”&Replace(当前文件“.txt”、“.xls”),xlNormal
'ActiveWorkbook.Close为False
'ActiveSheet.UsedRange.ClearContents
CurrentFile=Dir
温德
Application.DisplayAlerts=True
Application.ScreenUpdating=True
如果结束

您在什么环境下运行此脚本?我在一个文件夹中获得一些输出,我只需要将该输出从该文件夹复制到另一个文件夹,在该文件夹中,此输出将作为另一个可执行文件的输入。您是否在IE等中以.VBS脚本文件的形式运行此脚本。?您能在批处理文件中以同一用户的身份运行相同的副本吗?好的,我正在VBScript文件中运行此副本。让我尝试一下,我正在尝试确定脚本是否有权写入c:\destfolder\。如果用shell调用集objShell=CreateObject(“Wscript.shell”)objShell替换最后一行,是否会出现同样的错误。运行“copy c:\sourcefolder\anyfile.txt c:\destfolder\”?谢谢测试人员,这解决了我的问题。实际上,我在给定的文件名路径上遇到了一些问题。我们可以用上述代码将文件复制到Unix系统吗?如果复制时需要用户名/密码,我们应该在哪里传递它。谢谢