任何人都可以告诉我如何使用vbscripting将文件从一个文件夹复制到另一个文件夹 我从互联网上提供的信息中尝试了以下内容。
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\sourcefolder\anyfile.txt") Then
filesys.CopyFile "c:\sourcefolder\anyfile.txt", "c:\destfolder\"
当我执行这个时,我得到了许可被拒绝。
答案 0 :(得分:31)
试试这个。它将检查文件是否已存在于目标文件夹中,如果存在,将检查该文件是否为只读。如果文件是只读的,它会将其更改为读写,替换文件,并使其再次为只读。
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
答案 1 :(得分:5)
这是一个答案,基于(并且我认为有所改进)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
答案 2 :(得分:3)
要复制单个文件,请输入以下代码:
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 :(得分:0)
刚刚发布了类似项目的完成代码。它在我的代码中复制某些扩展名的文件,其pdf tif和tiff你可以将它们更改为你想复制的任何内容,如果你只需要1或2种类型,则删除if语句。创建或修改文件时,它会获取存档属性,此代码也会查找该属性,并且仅在存在时复制它,然后在复制后将其删除,这样您就不会复制不需要的文件。它还有一个日志设置,以便您可以看到上次运行脚本时转移的时间和日期的日志。希望能帮助到你! 链接是Error: Object Required; 'objDIR' Code: 800A01A8
答案 4 :(得分:-3)
请找到以下代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css" rel="stylesheet"/>
<table class="table table-bordered">
<tbody>
<tr>
<td>Timestamp</td>
<td>Cash & Carry</td>
<td>Shop Name</td>
<td>Amount</td>
<td>Comments</td>
</tr>
<tr>
<td>10/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>J's Pizza Stop </td>
<td>72.75£</td>
<td>PAID 2018-01-11 16:53 Full Amount: 75 Discount of 3% Discount Total: 2.25</td>
</tr>
<tr>
<td>13/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>93.42£</td>
<td>PAID 2018-01-18 10:08 Full Amount: 96.31 Discount of 3% Discount Total: 2.8893</td>
</tr>
<tr>
<td>14/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>67.08£</td>
<td>PAID 2018-01-18 10:05 Full Amount: 69.15 Discount of 3% Discount Total: 2.0745</td>
</tr>
<tr>
<td>18/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>94.00£</td>
<td>PAID 2018-01-25 10:34 Full Amount: 96.91 Discount of 3% Discount Total: 2.9073</td>
</tr>
<tr>
<td>19/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>48.50£</td>
<td>PAID 2018-01-25 10:34 Full Amount: 50 Discount of 3% Discount Total: 1.5</td>
</tr>
<tr>
<td>20/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Mexican House</td>
<td>34.85£</td>
<td>PAID 2018-01-25 10:33 Full Amount: 35.93 Discount of 3% Discount Total: 1.0779</td>
</tr>
<tr>
<td>25/01/2018</td>
<td>New Delhi Cash & Carry</td>
<td>Eastham Express</td>
<td>212.97£</td>
<td>PAID 2018-02-01 10:51 Full Amount: 219.56 Discount of 3% Discount Total: 6.5868</td>
</tr>
<tr>
<td>03/02/2018</td>
<td>New Delhi Cash & Carry</td>
<td>J's Pizza Stop </td>
<td>14.55£</td>
<td>Full Amount: 15 Discount of 3% Discount Total: 0.45</td>
</tr>
</tbody>
</table>