Vbscript 是否有办法使用vbs将驱动器中的每个文件和文件夹移动到驱动器本身的文件夹中
我需要一个程序在VBS(我也可以使用批处理,但VBS会更好),采取我的USB驱动器的所有文件和文件夹,并在USB文件夹中移动。例子: 如果在我的USB驱动器中有以下目录:Vbscript 是否有办法使用vbs将驱动器中的每个文件和文件夹移动到驱动器本身的文件夹中,vbscript,Vbscript,我需要一个程序在VBS(我也可以使用批处理,但VBS会更好),采取我的USB驱动器的所有文件和文件夹,并在USB文件夹中移动。例子: 如果在我的USB驱动器中有以下目录: E:\folder1\file.txt E:\folder2\foder3\file3.txt E:\file.txt 运行程序后,将有以下路径: E:\newfolder\folder1\file.txt E:\newfolder\folder2\foder3\file3.txt E:\newfolder\file.txt
E:\folder1\file.txt
E:\folder2\foder3\file3.txt
E:\file.txt
运行程序后,将有以下路径:
E:\newfolder\folder1\file.txt
E:\newfolder\folder2\foder3\file3.txt
E:\newfolder\file.txt
我不知道这是否可能。我使用for the cycle制作了一个程序,但它仅适用于文件而不适用于文件夹:
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("E:/")
Sub ShowSubFolders(Folder)
set fs = CreateObject("Scripting.FileSystemObject")
For Each Subfolder in Folder.SubFolders
fs.movefolder Subfolder.Path , "E:\newfolder\"
next
End Sub
With CreateObject("Scripting.FileSystemObject")
.MoveFile "E:\*.*", "E:\newfolder\"
End With
*在此代码中,新文件夹已经存在。您可以使用对象的方法:
“
MoveFolder
实际上不是FileSystemObject
的方法。这解释了使用MoveFolder()
方法时目标中尾随反斜杠的常见问题。它还建议使用你在这里强调的方法,更准确、更详细,这是7年前的事了。请不要留下“明显的”重复问题的答案,只需标记它并继续。这是否回答了您的问题?
Dim sSourcePath
Dim sDestinationPath
Dim objFSO
Dim objSourceFolder
Dim objDestinationFolder
Dim objFolder
' Define paths
sSourcePath = "E:\"
sDestinationPath = "E:\newfolder\"
' Get source and destination folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFSO.GetFolder(sSourcePath)
Set objDestinationFolder = objFSO.GetFolder(sDestinationPath)
For Each objFolder In objSourceFolder.Subfolders
If objFolder Is objDestinationFolder Then
' Don't move destination folder
Else
' Move folder to destination folder
objFolder.Move sDestinationPath
End If
Next