web-dev-qa-db-ja.com

vbscriptingを使用して、あるフォルダーから別のフォルダーにファイルをコピーする

誰も私がインターネットで提供された情報からこれを試したvbscriptingを使用して、あるフォルダから別のフォルダにファイルをコピーする方法を教えてください。

dim filesys

set filesys=CreateObject("Scripting.FileSystemObject")

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

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

これを実行すると、許可が拒否されたことがわかります。

16
maddy

これを試して。ファイルが宛先フォルダーに既に存在するかどうかを確認し、存在する場合はファイルが読み取り専用かどうかを確認します。ファイルが読み取り専用の場合は、読み取り/書き込みに変更し、ファイルを置き換えて、読み取り専用にします。

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
32
Tester101

これは、Tester101の回答に基づいた(そして改善されたと思う)回答です。サブルーチンとして表され、CopyFile行は3回ではなく1回で、コピーの作成時にファイル名の変更を処理する準備ができています(ハードコーディングなし)宛先ディレクトリ)。これを機能させるには、コピーする前にターゲットファイルを削除する必要がありましたが、これはWindows 7の問題かもしれません。 WScript.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
5
Chuck Wilbur

単一のファイルをコピーするためのコードは次のとおりです。

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
3
user2043336

同様のプロジェクトの完成したコードを投稿しました。私のコードの特定の拡張子のファイルをpdf tifとtiffにコピーします。コピーしたいものに変更するか、1つまたは2つのタイプのみが必要な場合はifステートメントを削除します。ファイルが作成または変更されると、このコードはアーカイブ属性を取得し、このコードはその属性を検索し、存在する場合にのみコピーし、コピー後に削除して不要なファイルをコピーしないようにします。また、ログのセットアップも含まれているため、前回スクリプトを実行したときから何日と何が転送されたかのログが表示されます。それが役に立てば幸い!リンクは エラー:オブジェクトが必要; 'objDIR'コード:800A01A8

0
ELewis