【发布时间】:2015-03-16 19:12:31
【问题描述】:
我正在尝试更改以下脚本以重命名文件夹(在资源管理器的右侧 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
【问题讨论】:
标签: file vbscript rename directory prefix