使用路径名重命名文件的 VBS 脚本 [英] VBS script to rename files using the pathname

查看:41
本文介绍了使用路径名重命名文件的 VBS 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VBS 脚本的新手,之前我很少使用 Excel VBA.现在我有一个脚本,它用文件的路径名重命名单个文件(每个文件截断为 4 个字母))见下文.这是一些脚本,我对其进行了一些修改以适合我的目的.但是,我想自动化文件重命名过程,并以 scipt 对单个文件的相同方式重命名文件夹及其子文件夹中的所有文件.有人可以帮我解答这个问题吗?

i am new to VBS scripting and I have done few stuff with Excel VBA before. Now I have a script which renames single files with the pathname of the files (truncated to 4 letter each))see below. It is some script which I modified a bit to fit my purpose. However, I would like to automatize the file rename process and rename all files in a folder and its subfolders in the same way the scipt works for single files. Can anybody help me with this question?

Set Shell = WScript.CreateObject("WScript.Shell")
Set Parameter = WScript.Arguments
For i = 0 To Parameter.Count - 1
    Set fso = CreateObject("Scripting.FileSystemObject")
    findFolder = fso.GetParentFolderName(Parameter(i))
    PathName = fso.GetAbsolutePathName(Parameter(i))
    FileExt = fso.GetExtensionName(Parameter(i))
    Search = ":"
    findFolder2= Right(PathName, Len(PathName) - InStrRev(PathName, Search))
    arr = Split(findFolder2, "\")
    For j=0 To UBound(arr)-1
    arr(j) = ucase(Left(arr(j), 4))
    Next
    joined = Join(arr, "%")   
    prefix = right(joined, len(joined)-1)    
    fso.MoveFile Parameter(i), findFolder + "\" + prefix 
next

希望我能得到一些有用的想法.

Hoping that I can get some useful ideas.

赫比

推荐答案

遍历一棵树需要递归,每个层级都有一个函数调用自身.

Walking a tree requires recursion, a function calling itself for each level.

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
ProcessFolder DirName

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.DateLastModified 
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub

来自关于如何运行另一个文件的帮助.

From Help on how to run another file.

Set Shell = WScript.CreateObject("WScript.Shell")
shell.Run(strCommand, [intWindowStyle], [bWaitOnReturn]) 

所以在循环之外,

Set Shell = WScript.CreateObject("WScript.Shell")

在循环中

shell.Run("wscript Yourscript.vbs thing.name, 1, True) 

此外,MS 网站上的 VBS 帮助文件最近已被删除.它在我的 Skydrive 上可用,地址为 https://1drv.ms/f/s!AvqkaKIXzvDieQFjUcKneSZhDjw称为script56.chm.

Also the VBS help file has recently been taken down at MS web site. It is available on my skydrive at https://1drv.ms/f/s!AvqkaKIXzvDieQFjUcKneSZhDjw It's called script56.chm.

这篇关于使用路径名重命名文件的 VBS 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆