以编程方式使用VBScript重命名文件编号

时间:2012-02-17 16:03:37

标签: shell vbscript

我想我想使用VBScript,但我可以使用批处理文件。

我想重命名一些我这样命名的文件:

Season (1)
Season (2)
..snip..
Season (9)
Season (10)
Season (11)
Season (12)

为:

Season (01)
Season (02)
..snip..
Season (09)
Season (10)
Season (11)
Season (12)

对于100多个有超过10个赛季的电视节目而不是手动执行此操作,有没有办法以编程方式实现这一目标?感谢。

2 个答案:

答案 0 :(得分:1)

在VBScript中,您可以执行以下操作:

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder("C:\test")

For i = 1 To 15
    strPath = objFolder.Path & "\Season (" & i & ")"
    If Not objFso.FolderExists(strPath) Then objFso.CreateFolder strPath
Next

For Each objSubfolder In objFolder.SubFolders
    strName = objSubfolder.Name
    intPos1 = InStr(strName, "(")
    intPos2 = InStr(strName, ")")
    strOrder = Mid(strName, intPos1 + 1, intPos2 - intPos1 - 1)
    If Len(strOrder) = 1 Then
        objSubfolder.Name = Left(strName, intPos1) _
                            & "0" & strOrder _
                            & Right(strName, Len(strName) - intPos2 + 1)
    End If
Next

这将查看给定文件夹中的每个子文件夹并查找季节编号。如果它是一个数字,则填充该数字并重命名该文件夹。代码经过测试和运行。

答案 1 :(得分:0)

在VB / VBA中,它可以是:

For x = 1 to 100 ' or however many you have
Name "Season (" & cstr(x) & ")" as "Season (" & Format(x,"00") & ")"
Next

或者这个(再次在VB / VBA中)可以在bat文件中生成你需要的命令:

Sub Thing()

Dim x As Long

For x = 1 To 100 ' or however many you have
  Debug.Print "Ren " & Chr$(34) & "Season (" & CStr(x) & ")" & Chr$(34) & " " _
  & Chr$(34) & "Season (" & Format(x, "00") & ")" & Chr$(34)
Next

End Sub

给你这个:

Ren "Season (1)" "Season (01)"
Ren "Season (2)" "Season (02)"
Ren "Season (3)" "Season (03)"
Ren "Season (4)" "Season (04)"
Ren "Season (5)" "Season (05)"
Ren "Season (6)" "Season (06)"
Ren "Season (7)" "Season (07)"
Ren "Season (8)" "Season (08)"
Ren "Season (9)" "Season (09)"
Ren "Season (10)" "Season (10)"
Ren "Season (11)" "Season (11)"
Ren "Season (12)" "Season (12)"
Ren "Season (13)" "Season (13)"
Ren "Season (14)" "Season (14)"
Ren "Season (15)" "Season (15)"
Ren "Season (16)" "Season (16)"
Ren "Season (17)" "Season (17)"
Ren "Season (18)" "Season (18)"
Ren "Season (19)" "Season (19)"
Ren "Season (20)" "Season (20)"
Ren "Season (21)" "Season (21)"
Ren "Season (22)" "Season (22)"
Ren "Season (23)" "Season (23)"
Ren "Season (24)" "Season (24)"
Ren "Season (25)" "Season (25)"
Ren "Season (26)" "Season (26)"
Ren "Season (27)" "Season (27)"
Ren "Season (28)" "Season (28)"
Ren "Season (29)" "Season (29)"
Ren "Season (30)" "Season (30)"
Ren "Season (31)" "Season (31)"
Ren "Season (32)" "Season (32)"
Ren "Season (33)" "Season (33)"
Ren "Season (34)" "Season (34)"
Ren "Season (35)" "Season (35)"
Ren "Season (36)" "Season (36)"
Ren "Season (37)" "Season (37)"
Ren "Season (38)" "Season (38)"
Ren "Season (39)" "Season (39)"
Ren "Season (40)" "Season (40)"
Ren "Season (41)" "Season (41)"
Ren "Season (42)" "Season (42)"
Ren "Season (43)" "Season (43)"
Ren "Season (44)" "Season (44)"
Ren "Season (45)" "Season (45)"
Ren "Season (46)" "Season (46)"
Ren "Season (47)" "Season (47)"
Ren "Season (48)" "Season (48)"
Ren "Season (49)" "Season (49)"
Ren "Season (50)" "Season (50)"
Ren "Season (51)" "Season (51)"
Ren "Season (52)" "Season (52)"
Ren "Season (53)" "Season (53)"
Ren "Season (54)" "Season (54)"
Ren "Season (55)" "Season (55)"
Ren "Season (56)" "Season (56)"
Ren "Season (57)" "Season (57)"
Ren "Season (58)" "Season (58)"
Ren "Season (59)" "Season (59)"
Ren "Season (60)" "Season (60)"
Ren "Season (61)" "Season (61)"
Ren "Season (62)" "Season (62)"
Ren "Season (63)" "Season (63)"
Ren "Season (64)" "Season (64)"
Ren "Season (65)" "Season (65)"
Ren "Season (66)" "Season (66)"
Ren "Season (67)" "Season (67)"
Ren "Season (68)" "Season (68)"
Ren "Season (69)" "Season (69)"
Ren "Season (70)" "Season (70)"
Ren "Season (71)" "Season (71)"
Ren "Season (72)" "Season (72)"
Ren "Season (73)" "Season (73)"
Ren "Season (74)" "Season (74)"
Ren "Season (75)" "Season (75)"
Ren "Season (76)" "Season (76)"
Ren "Season (77)" "Season (77)"
Ren "Season (78)" "Season (78)"
Ren "Season (79)" "Season (79)"
Ren "Season (80)" "Season (80)"
Ren "Season (81)" "Season (81)"
Ren "Season (82)" "Season (82)"
Ren "Season (83)" "Season (83)"
Ren "Season (84)" "Season (84)"
Ren "Season (85)" "Season (85)"
Ren "Season (86)" "Season (86)"
Ren "Season (87)" "Season (87)"
Ren "Season (88)" "Season (88)"
Ren "Season (89)" "Season (89)"
Ren "Season (90)" "Season (90)"
Ren "Season (91)" "Season (91)"
Ren "Season (92)" "Season (92)"
Ren "Season (93)" "Season (93)"
Ren "Season (94)" "Season (94)"
Ren "Season (95)" "Season (95)"
Ren "Season (96)" "Season (96)"
Ren "Season (97)" "Season (97)"
Ren "Season (98)" "Season (98)"
Ren "Season (99)" "Season (99)"
Ren "Season (100)" "Season (100)"