17 vbscript
任何人都可以告诉我如何使用vbscripting将文件从一个文件夹复制到另一个文件夹我从下面尝试过这一点来自互联网提供的信息.
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\sourcefolder\anyfile.txt") Then
filesys.CopyFile "c:\sourcefolder\anyfile.txt", "c:\destfolder\"
Run Code Online (Sandbox Code Playgroud)
当我执行这个时,我得到了许可被拒绝.
Tes*_*101 32
试试这个.它将检查文件是否已存在于目标文件夹中,如果存在,将检查该文件是否为只读.如果文件是只读的,则会将其更改为读写,替换文件,并使其再次为只读.
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
Run Code Online (Sandbox Code Playgroud)
小智 6
对于复制单个文件,这里是代码:
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
Run Code Online (Sandbox Code Playgroud)
这是一个答案,基于(并且我认为有所改进)Tester101的答案,表示为子程序,一次使用CopyFile行而不是三次,并准备在复制时更改文件名(没有硬编码)目的地目录).我还发现在复制之前我必须删除目标文件以使其工作,但这可能是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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
311068 次 |
最近记录: |