我有大量的图像文件,跨越了近二十年,其中的主题由目录名称标识,大多数照片本身具有通用名称,但是其中一些具有更特定的名称。 我正在编写一个脚本,以将目录名添加到特定目录中所有文件的文件名之前。好吧,我至少要努力。 自从我使用VBScript已经好几年了,似乎我非常生锈。 我在语法格式方面面临挑战。
当我有Option Explicit(第6行)时,它给出了第6行的错误,Char 1,错误:期望的语句,代码:800A0400(在我的共享代码中,由于添加了文件开头,所以它将是第7行)
如果我将其注释掉,则会在导入语句而不是第3行char 1上出错,错误:类型不匹配:'Imports',代码:800A000D(在我的共享代码中,由于添加的“文件开头”行)
我花了几个小时来寻找可能的原因,但无济于事,所以我向社区寻求帮助,以正确设置此脚本的格式。
对于完成此任务的更好脚本方法的任何评论也将不胜感激。
我要为文件放入整个代码,因为我不知道文件的哪一部分是相关方面。
在代码中,当前设置为仅为每个文件创建一个消息框作为测试手段,以确保变量具有我认为的值。 用于文件重命名的注释掉的代码是真正的目的。 但是,我坚持文件第一部分的正确格式。 通常,我使用以下命令从命令行执行此命令:cscript.exe c:\ MyTools \ addDir2FileName.vbs
我通过Windows资源管理器启动了它,但可以获取行号更为具体的错误代码。
为清楚起见,我添加了文件开头和文件结尾注释。
' ####### Beginning of File
' Renames all files in a directory prepending the directory name to the file name
Imports System
Imports System.IO
Option Explicit
Dim WshShell, strOldFull, strNewFull, strFullPath, strCurDir
Dim strCurrentName, strNewName, strMessage, dir, fileObj, fs, fo
' Get the current directory
Set WshShell = CreateObject("WScript.Shell")
strFullPath = WshShell.CurrentDirectory
'Get folder properties to get just the name without path
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder(strFullPath)
strCurDir = fo.Name
'Iterate through the directory
set dir = DirectoryInfo(strFullPath)
For Each fileObj In dir.GetFiles()
strCurrentName = fileObj.Name
strNewName = strCurDir & " - " & strCurrentName
' For testing purposes to make sure everything is as expected
' Creates a message box for each file instead of actually renaming it
strMessage = "Old Name: " & strCurrentName & chr(13) & chr(10) & "New Name: " & strNewName
MsgBox strMessage
' Renaming the file
' strOldFull = fs.BuildPath(CurrentDirectory, strCurrentName)
' strNewFull = fs.BuildPath(CurrentDirectory, strNewName)
' My.Computer.FileSystem.RenameFile(strOldFull, strNewFull)
Next
WshShell = Nothing
fo = Nothing
fs = Nothing
' ### End of File
期望文件“ C:\ Pictures \ Trip到Nice \ DCM001.jpg”将重命名为“ C:\ Pictures \ Trip到Nice \ Trip到Nice-DCM001.jpg”,并且其中的所有文件运行脚本的目录将类似地重命名。 好吧,更准确地说,当前格式的输出将产生一个消息框,其中显示“旧名称:C:\ Pictures \ Trip到Nice \ DCM001.jpg新名称:C:\ Pictures \ Trip到Nice \ Trip到Nice -DCM001.jpg”,然后将为目录中的所有文件生成一个消息框。是的,我只会在具有3个文件的测试目录中运行消息框版本。我不希望收到50,000个消息框,大声笑。
由于Import语句或Option Explicit的格式问题,或者我可能缺少或有其他错误的语法,当前无输出。
感谢您的时间以及任何人都能提供的帮助。这是我第一次向社区发布信息,但是我一直很感谢所提供的答案。通常,我可以找到已经回答的问题,但是我对此感到困惑……
答案 0 :(得分:0)
好的,通过大量的试验和错误,我找到了一种方法来完成不使用System的任务,从而避免了以前收到的错误。 我想发布最终脚本,以防万一有人感兴趣。
' Renames all files in a directory prepending the directory name to the file name
Option Explicit
Dim WshShell, strOldFull, strFullPath, strCurDir, lastSlash
Dim strCurrentName, strNewName, strMessage, fileObj, fileSpec, fs, fo, ff
' Get the current directory
Set WshShell = CreateObject("WScript.Shell")
strFullPath = WshShell.CurrentDirectory
'Get folder object
Set fs = CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(strFullPath)
set ff = fo.Files
'Get just the folder name
lastSlash = inStrRev(strFullPath, "\")
strCurDir = right(strFullPath, len(strFullPath) - lastSlash )
'Iterate through the directory
For Each fileObj in ff
strCurrentName = fileObj.Name
strNewName = strCurDir & " - " & strCurrentName
' For testing purposes to make sure everything is as expected
' Creates a message box for each file instead of actually renaming it
' strMessage = "Old Name: " & strCurrentName & chr(13) & chr(10) & "New Name: " & strNewName
' MsgBox strMessage
' Renaming the file
strOldFull = strFullPath & "\" & strCurrentName
set fileSpec = fs.GetFile(strOldFull)
fileSpec.Name = strNewName
Next