使用 movefolder 方法时找不到 vbscript 错误路径 [英] vbscript error path not found while using movefolder method
问题描述
我对 vbscript 还很陌生,我正在尝试编写一个脚本,该脚本将选取带有月份和年份标记的文件夹 (2012_04) 并将它们移动到带有年份标记的文件夹 (2012).但是当我尝试移动文件夹时,我收到了一个 Path not found 错误,而且我似乎无法在任何地方找到关于它为什么发生的答案.
I am fairly new to vbscript, and attempting to write a script that will pick up month and year stamped folders (2012_04) and move them to a year stamped folder (2012). I am getting a Path not found error though when I attempt to move the folder, and I can't seem to find an answer anywhere as to why it is happening.
for i = 0 to UBound(yearArray)
Set folder = fso.GetFolder(InputP)
Set subFold = Folder.Subfolders
yearStamp = yearArray(i)
if not fso.FolderExists(ArchiveP & yearStamp) then
fso.createFolder(ArchiveP & yearStamp)
end if
ArchiveP = ArchiveP & yearStamp & "\"
for each dateFold in subFold
Set fo = fso.GetFolder(InputP & dateFold.Name)
folderName = InputP & dateFold.name & "\"
foldName = fo.name & "\"
if left(foldName,4) = yearStamp then
fso.MoveFolder folderName , ArchiveP & foldName
end if
next
ArchiveP = UnChangeP & PreArchP
Next
错误发生在 fso.MoveFolder folderName , ArchiveP &foldName,我不知道发生了什么.
The error happens at fso.MoveFolder folderName , ArchiveP & foldName and I can't figure out what is happening.
推荐答案
您得到的错误是由错误构造的路径引起的.你想要做的是这样的:
The error you're getting is caused by misconstructed paths. What you're trying to do is something like this:
fso.MoveFolder "C:\input\2013_03", "D:\archive\2013\2013_03"
但是,您实际上正在做的是:
However, what you're acutally doing is this:
fso.MoveFolder "C:\input\2013_03\", "D:\archive\2013\2013_03\"
^ ^
尾随反斜杠仅在目标路径中有效,并且仅当目标路径是您要将源文件夹移动到的父文件夹时,即您的语句应如下所示:
A trailing backslash is only valid in the destination path, and only if the destination path is the parent folder to which you want to move the source folder, i.e. your statement should look either like this:
fso.MoveFolder "C:\input\2013_03", "D:\archive\2013\"
或者像这样:
fso.MoveFolder "C:\input\2013_03", "D:\archive\2013\2013_03"
避免通过字符串连接构建路径.FileSystemObjects
提供了一种方法 BuildPath
将正确处理路径分隔符.
Avoid building paths via string concatenation. The FileSystemObjects
provides a method BuildPath
that will handle path separators correctly.
顺便说一句,您的代码相当复杂.您可以简单地使用 For Each
循环遍历所有元素,而不是使用对 yearArray
的索引访问.此外,您对 InputP
子文件夹的迭代已经为您提供了 Folder
对象.fso.GetFolder(InputP & dateFold.Name)
是与 dateFold
完全相同的对象.另外,Folder
对象带有 Move
方法,所以你只需要处理目标路径.
Your code is rather convoluted, BTW. Instead of using indexed access to yearArray
you could simply iterate over all elements with a For Each
loop. Also, your iteration over the subfolders of InputP
already provides you with Folder
objects. fso.GetFolder(InputP & dateFold.Name)
is the exact same object as dateFold
. Plus, Folder
objects come with a Move
method, so you'd only need to handle the destination path.
我相信您的代码可以简化为以下内容,这应该可以满足您的需求:
I believe your code could be simplified to the following, which should do what you want:
For Each year In yearArray
dst = fso.BuildPath(ArchiveP, year)
If Not fso.FolderExists(dst) Then fso.CreateFolder dst
For Each dateFold In fso.GetFolder(InputP).SubFolders
If Left(dateFold.Name, 4) = year Then dateFold.Move dst & "\"
Next
Next
不过,就性能而言,切换两个循环可能是个好主意.遍历文件夹意味着您必须从磁盘读取,而 yearArray
在内存中,因此前者的迭代肯定会比后者慢.通过使子文件夹迭代成为外循环(并将目标文件夹的创建置于单独的循环中),您可以消除这个瓶颈,因为这样您只需读取每个子文件夹一次.
In terms of performance it might be a good idea to switch the two loops, though. Iterating over folders means you have to read from disk whereas yearArray
is in memory, thus the former iteration is bound to be slower than the latter. By making the subfolder iteration the outer loop (and putting the destination folder creation in a separate loop) you eliminate this bottleneck, because that way you read each subfolder just once.
For Each year In yearArray
dst = fso.BuildPath(ArchiveP, year)
If Not fso.FolderExists(dst) Then fso.CreateFolder dst
Next
For Each dateFold In fso.GetFolder(InputP).SubFolders
For Each year In yearArray
dst = fso.BuildPath(ArchiveP, year)
If Left(dateFold.Name, 4) = year Then dateFold.Move dst & "\"
Next
Next
这篇关于使用 movefolder 方法时找不到 vbscript 错误路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!