使用 VB 重命名和移动文件夹 [英] Rename and Move Folders Using VB
问题描述
如果我需要重命名文件夹中的文件,我有以下脚本非常有用,但现在我想将文件夹从一个映射驱动器移动并重命名到另一个映射驱动器.有人可以帮我修改脚本吗?我是 VB 的新手,所以如果我无法解决这个问题,请原谅,但我花了一点时间才弄明白这个问题,现在我不知道如何修改这个脚本.先感谢您!
I have the following script which works great if I need to rename files in a folder but now I want to move and rename folders from one mapped drive to another mapped drive. Can someone help me to modify the script to do so? I am failry new to VB so pardon if I can't firgure this out but it took me a little while to figure this one out and now I am not sure how to modify this script. Thank you in advance!
默认情况下,文件夹标记为 A.1234、A.5678 等,并且始终分配有不同的编号.我会将这些数字保留在标签中,因为它们是 PO 编号.所以我想要的最终结果是 Ack~1234、Ack~5678 等等.
The folders by default are labeled A.1234, A.5678, and so on and will always have a different number assigned to them. I will keep the numbers in the label as they are PO numbers. So my end result desired is Ack~1234, Ack~5678, and so on.
Dim fso, f, f1, fc, s Set
fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move f1.ParentFolder & "\" & replace(f1.Name, "A.", "Ack~")
同样,这些文件夹位于映射驱动器的根目录下,需要移动到具有新名称的另一个映射驱动器.如果需要更多信息,请随时询问.
Again these folders exist on the root of a mapped drive and need to move to another mapped drive with the new names. If more info is needed please do not hesitate to ask.
更新
我修改了下面的脚本以了解我要做什么.
I modified the script below to give an idea of what I'm looking to do.
Dim fso, objFol
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Z:\")
Set objFolders = objFol.Folders
For each folder in objFolders
fso.Movefolder folder, "Y:\" & Replace(fso.Name, "A.", "Ack~")
Next
这给我一个错误,指出它不支持文件夹".Z 驱动器中将有任意数量的文件夹,我需要将它们全部移动到 Y 驱动器.抱歉,如果我在上一篇文章中没有正确解释.
This give me an error stating it does not support "Folder". There will be any number of folders in the Z drive and I need to move them all to the Y drive. Sorry if I didn't explain properly in the previous post.
推荐答案
移动命令正在获取一个字符串,您为其提供文件的父文件夹,因此文件不会移动.我认为提供不同的文件夹位置就足够了.例如:
The move command is taking a string, for which you're providing the parent folder of the file, so the file isn't moving. I think it'll be sufficient to provide a different folder location. For example:
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move "Z:\TargetFolder\" & replace(f1.Name, "A.", "Ack~")
注意:我没有测试过这个,但如果有问题就问
Note: I've not tested this, but if there are problems then just ask
更新后的其他内容:
以下代码将允许您移动文件夹,并对文件夹名称字符串进行所需的替换.
The following code will allow you to move folders, and do the replacements you need to the folder name string.
Dim fso, objFol, objMoveFol, strPathBuild
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Y:\Test")
For Each objMoveFol In objFol.SubFolders
'Replace the root folder locations in the path
strPathBuild = Replace(objMoveFol, "Y:\Test", "Z:\TargetFolder\")
'Do the required other fiddle
strPathBuild = Replace(strPathBuild, "A.", "Ack~")
fso.Movefolder objMoveFol, strPathBuild
Next
确保你对这类事情非常小心,因为错误可能会相当壮观.
Make sure you're really careful with this sort of thing, as getting is wrong can be fairly spectacular.
这篇关于使用 VB 重命名和移动文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!