使用VBScript重命名文件夹将前缀添加到文件夹

时间:2015-01-18 18:53:32

标签: file vbscript rename directory prefix

我试图更改以下脚本以重命名文件夹(在资源管理器的右侧ckick菜单中添加现有文件夹名称前面的前缀,此脚本适用于文件,如何更改脚本以向文件夹添加前缀? 例: 我有文件夹" C:\ Test" 添加前缀后,结果将为" C:\ Template.Test"此文件夹中的所有文件将保持不变

- =为文件添加前缀= -

Option Explicit

'On Error Resume Next

'''''''''' Declare variables and objects
Dim strFileName 'As String
Dim strShortName 'As String
Dim strPrefix 'As String
Dim fs 'As Scripting.FileSystemObject
Dim fol 'As Scripting.Folder
Dim fils 'As Scripting.Files
Dim fil 'As Scripting.File

''''''''''Create the fs object
Set fs = Wscript.CreateObject("Scripting.FileSystemObject")

''''''''''First check the filename argument
If Wscript.Arguments.Count <> 1 Then
    MsgBox "You must pass a path & file name on the command line"
    Wscript.Quit 1
End If
strFileName = Wscript.Arguments(0)
If Not fs.FileExists(strFileName) Then
    MsgBox Wscript.Arguments(0) & " is not a legitimate file name."
    Wscript.Quit 1
End If
Set fil = fs.GetFile(strFileName)
strShortName = fil.ShortName
Set fil = Nothing

''''''''''Find the long file name. Search the directory. 
Set fol = fs.GetFolder(fs.BuildPath(strFileName, "..\"))
Set fils = fol.Files
For Each fil In fils
'   Msgbox "'" & strShortName & "' ? '" & fil.Shortname & "'"
    If ucase(fil.ShortName) = ucase(strShortName) Then Exit For
Next
If ucase(fil.ShortName) <> ucase(strShortName) Then
    MsgBox "Oops -- I can't seem to locate that file"
    Wscript.Quit 1
End If

strPrefix = InputBox("Please enter a name for your file:",  "FilePrefix", "Template")

''''''''''Now rename it
 If InstrRev(fil.Name, ".") > InstrRev(fil.Name, "\") Then
     'The file name has a dot
     fil.Name = strPrefix & Left(fil.Name, InstrRev(fil.Name, ".") - 1) & Mid(fil.Name, InstrRev(fil.Name, "."))
 Else
     'The file name has no dot
     fil.Name =  strPrefix & fil.Name
 End If

If Err.Number <> 0 Then Wscript.Quit 1

''''''''''Clean up
Set fil = Nothing
Set fol = Nothing
Set fs = Nothing

1 个答案:

答案 0 :(得分:0)

我自己做了

添加到注册表

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\Add Folder Prefix]
"MUIVerb"="-=Add Folder Prefix=-"
"Icon"="%SystemRoot%\\System32\\shell32.dll,68"

[HKEY_CLASSES_ROOT\Folder\shell\Add Folder Prefix\command]
@="WSCRIPT \"C:\\Portable\\FFPrefix\\FolderPrefix.vbs\" \"%1\""

''' - =添加文件夹前缀= - '''

Option Explicit
'On Error Resume Next

'Declare variables and objects
Dim fs 'As Scripting.FileSystemObject
Dim fol 'As Scripting.Folder
Dim strFolderName 'As Folder
Dim strPrefix 'As String
Dim strMenu 'As String
Dim strDate 'As String

'Create the fs object
Set fs = Wscript.CreateObject("Scripting.FileSystemObject")

'First check the filename argument
If Wscript.Arguments.Count <> 1 Then
    MsgBox "You must pass a path & file name on the command line"
    Wscript.Quit 1
End If
strFolderName = Wscript.Arguments(0)
If Not fs.FolderExists(strFolderName) Then
    MsgBox Wscript.Arguments(0) & " is not a legitimate folder name."
    Wscript.Quit 1
End If
Set fol = fs.GetFolder(strFolderName)
'Create the Menu
strMenu="Select a prefix for your Folder:" & VbCrLf &_
 "1 VSTi."                 & VbCrLf &_
 "2 Film."                 & VbCrLf &_
 "3 XXX."                  & VbCrLf &_
 "4 Audio."                & VbCrLf &_
 "5 Template."             & VbCrLf &_ 
 "6 Tutorial."             & VbCrLf &_
 "7 Adobe."                & VbCrLf &_
 "8 Photoshop."            & VbCrLf &_
 "9 Lightroom."            & VbCrLf &_
 "10 ebook."               & VbCrLf &_
 "11 Plugins."             & VbCrLf &_
 "12 Presets."             & VbCrLf &_
 "13 Theme."               & VbCrLf &_
 "14 -=[ Manual Edit ]=-." & VbCrLf &_
 "15 -=[ Add Date Modified prefix ]=-"

 strPrefix=InputBox(strMenu,"FolderPrefix Menu",1)
 If IsNumeric(strPrefix) Then
     Select Case strPrefix
'Now rename it   
        Case 1
    fol.Name =  "VSTi." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 2
    fol.Name =  "Film." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 3
    fol.Name =  "XXX."  & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 4
    fol.Name =  "Audio." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 5
    fol.Name =  "Template." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 6
    fol.Name =  "Tutorial." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 7
    fol.Name =  "Adobe." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 8
    fol.Name =  "Photoshop." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 9
    fol.Name =  "Lightroom." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 10
    fol.Name =  "ebook." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 11
    fol.Name =  "Plugins." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 12
    fol.Name =  "Presets." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 13
    fol.Name =  "Theme." & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 14
    strPrefix = InputBox("Please enter a Prefix for your Folder:" , "FolderPrefix Nenu")
    fol.Name =  strPrefix & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

        Case 15
    strDate = Year(fol.DateLastModified) & "-" & Right("0" & Month(fol.DateLastModified),2) & "-" & Right("0" & Day(fol.DateLastModified),2) & "_"
    fol.Name =  strDate & fol.Name
If Err.Number <> 0 Then Wscript.Quit 1

Case Else
       Wscript.Echo "Prefix Creation Aborted."
     End Select
     Else
       WScript.Echo "Invalid option. Numbers only."
 End If

'Clean up
Set fol = Nothing
Set fs = Nothing