file - 如何使用VBScript重命名目录中位于文件夹名称之前的所有文件?

标签 file vbscript compiler-errors runtime-error option-explicit

我有大量的图像文件,跨越了近二十年,其中的主题由目录名称标识,并且大多数照片本身都具有通用名称,但是其中一些具有更特定的名称。
我正在编写一个脚本,以将目录名添加到特定目录中所有文件的文件名之前。好吧,我至少要努力。
自从我使用VBScript已经好几年了,似乎我非常使用rust 。
我在语法格式方面面临挑战。

当我有Option Explicit(第6行)时,它给出了第6行,Char 1,错误:预期语句,代码:800A0400(在我的共享代码中,由于添加了Beginning of File行,因此将是第7行)

如果我将其注释掉,则会在import语句而不是第3行char 1上出错,错误:类型不匹配:'Imports',代码:800A000D(在我的共享代码中,由于添加了Beginning,它将是第4行文件行)

我花了几个小时来寻找可能的原因,但无济于事,所以我向社区寻求帮助,以正确设置此脚本的格式。

对于完成此任务的更好脚本方法的任何评论也将不胜感激。

我将输入文件的整个代码,因为我不知道文件的哪一部分将成为相关方面。

在代码中,当前设置为仅为每个文件创建一个消息框作为测试手段,以确保变量具有我认为的值。
用于文件重命名的注释掉的代码是真正的目的。
但是,我坚持文件第一部分的正确格式。
通常,我使用以下命令从命令行执行此命令: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的格式问题,或者我可能遗漏或有错误的某些其他语法,当前没有输出。

感谢您的宝贵时间,以及任何人都能提供的帮助。这是我第一次向社区发布信息,但是我一直很感谢所提供的答案。通常,我可以找到已经回答的问题,但是我对此感到困惑。

最佳答案

好的,通过大量的尝试和错误,我找到了一种在不使用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

关于file - 如何使用VBScript重命名目录中位于文件夹名称之前的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56045100/

相关文章:

javascript - Firefox 中的 HTML 5 拖放无法按预期工作

python - 如何以格式化字符串打开文件?

c++ - 将文本文件读入 vector ( double 、 double 、字符串)? C++

excel - "Object Required"声明Excel.Application对象后出错

windows - 如何从 VB 脚本确定 Windows 版本?

vbscript - VBS预期语句错误

ios - XCode 8.3.1 缺少默认的 C++ ObjectiveC 编译器。无法构建项目

sql - 如何识别括号内的标识符

C++ 继承(覆盖构造函数)

c - 使用 fgets 和 put 读取和打印文件不起作用