现在的位置: 首页 > 综合 > 正文

利用VBScript批量复制、移动文件或文件夹

2013年10月08日 ⁄ 综合 ⁄ 共 2050字 ⁄ 字号 评论关闭

问题提出:

1.有一个文件需要复制到文件夹bin中所有目录下
2.需要复制出bin目录下所有文件夹中的*.dll文件到另外一个新的目录 ,并保持原有的目录结构

解决方案:

1.VBS中FileSystemObject.CopyFile实现,代码如下:

文件名:CopyFileToFolders.vbs

Dim srcFile
Dim dstFolder,dstFolderName

Dim objFSO
Dim objDstFolder

srcFile 
= InputBox("Input the File Full Name where you will copy from:","SourceFile","FullName")
dstFolder 
= InputBox("Input the Floder Full Path Where you will copy to:","DestinationFolder","FullPath"

Set objFSO    = CreateObject("Scripting.FileSystemObject")
Set objDstFolder = objFso.GetFolder(dstFolder)

For Each objSubfolder in objDstFolder.Subfolders
    
If Right(dstFolder,1)="" Then
        dstFolderName 
= dstFolder & objSubfolder.Name & ""
    
Else
        dstFolderName 
= dstFolder & "" & objSubfolder.Name & ""
    
End If
    objFSO.CopyFile srcFile,dstFolderName
Next
MsgBox "Copied Successful!",32,"Alert"

需要删除重新复制即可以用DOS命令:

del /s

 

2.1.VBS中FileSystemObject.MoveFile实现,代码如下:

文件名:MoveFolderFiles.vbs

Dim srcFolder,srcFile
Dim srcFileType
Dim dstFolder,dstFolderName

Dim objFSO
Dim objSrcFolder,objDstFolder
Dim objSubSrcFolders,objSubDstFolder


srcFolder 
= InputBox("Input the Floder Full Path where you will move from:","SourceFolder","FullPath")
srcFileType 
= InputBox("Input the Type of File which you will move:","SourceFileType","Postfix")
dstFolder 
= InputBox("Input the Floder Full Path Where you will move to:","DestinationFolder","FullPath")

Set objFSO    = CreateObject("Scripting.FileSystemObject")
Set objSrcFolder = objFso.GetFolder(srcFolder)
Set objDstFolder = objFso.GetFolder(dstFolder)

For Each objSubSrcFolders In objSrcFolder.SubFolders

    
If Right(srcFolder,1= "/" Then
        srcFile 
= srcFolder & objSubSrcFolders.Name & "/" & srcFileType 
    
Else
        srcFile 
= srcFolder & "/" & objSubSrcFolders.Name & "/" & srcFileType 
    
End If

    dstFolderName 
= dstFolder & "/" & objSubSrcFolders.Name & "/"
    
If Not objFSO.FolderExists(dstFolderName) Then
        objFSO.CreateFolder(dstFolderName)
    
End If
    objFSO.MoveFile srcFile,dstFolderName

Next

MsgBox "Moved Successful!",32,"Alert"

另:可以用DOS命令的xcopy /s + del /s 取代,但当所复制文件较大时,建议用VBS处理。

抱歉!评论已关闭.