- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有大量的图像文件,跨越了近二十年,其中的主题由目录名称标识,并且大多数照片本身都具有通用名称,但是其中一些具有更特定的名称。
我正在编写一个脚本,以将目录名添加到特定目录中所有文件的文件名之前。好吧,我至少要努力。
自从我使用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
最佳答案
好的,通过大量的尝试和错误,我找到了一种在不使用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/
我正在使用下面的脚本来调用另一个脚本。问题是我必须将 WScript.Arguments 检索到的参数传递给我正在调用的第二个脚本。有人可以告诉我该怎么做吗。 Dim objShell Set obj
我正在使用下面的脚本来调用另一个脚本。问题是我必须将我通过 WScript.Arguments 检索的参数传递给我正在调用的第二个脚本。有人可以告诉我该怎么做吗。 Dim objShell Set o
我有一个非常简单的 vbscript 代码: Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Prog
我需要将名称基于日期的文件移动到另一个文件夹。 文件结构为: 来源: \\network_location\folder\Filename_09-11-2012.txt 目的地: C:\Dump\Fi
我有一个关于 VBScript 中变量作用域的问题。我知道有以下关键字(来自 autoitscript.com ): Dim = 局部作用域,如果变量名在全局不存在(在这种情况下它重用全局变量!) G
我正在尝试在 VBScript 中运行以下代码但它没有编译最后一条语句。是不是因为VBScript不允许命名参数? Filename_Argument = WScript.Arguments(0) S
在VBScript中,我需要确保用户输入一个整数。 这是我现在所拥有的: WScript.Echo "Enter an integer number : " Number = WScript.StdI
使用这些信息,我学会了here和here。 我在下面创建了短代码。 当我运行代码并有意识地输入任务计划程序库的“\”文件夹中存在的任务时,我收到“任务不存在” msgbox。 我尝试在传递给函数的变量
我试图仅用VBScript编写一个小的脚本供家庭使用,该脚本在Macrium Reflect中进行预定备份之前运行。 我被困在一个看似很小的问题上,这是在物理断开网络驱动器(即未连接电缆)时的错误处理
我阅读了 Eric Lippert 关于 VBScript 中默认属性语义的文章:http://blogs.msdn.com/b/ericlippert/archive/2005/08/30/4580
我正在尝试使用 VBScript 过滤二维数组,但 VBScript 的内置“过滤器”函数仅适用于单维数组。我使用的是“rs.GetRows()”数组,那么是否有一个可以处理二维数组的简单函数? 编辑
尝试在 HP-UFT 中运行时,下面的 VBScript 代码让我感到困惑,因为第一个语句打印 True 而不是 False (这似乎不合逻辑),而第二个打印 False (这似乎合乎逻辑) 代码:
我刚刚了解到 $ 需要转义字符。 VBScript 中还有哪些特殊字符? 还有一个 bool 函数可以用来判断一个字符是否是特殊字符? 最佳答案 嗯? WScript.Echo "$" 输出 $ 而不
我需要将批处理文件转换为 vbscript,但我对两者都不熟悉。如果我能理解批处理文件中发生的事情,我就可以很容易地计算出 vbscript。问题是批处理文件运行一些 cscript 命令,其语法应该
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
长期以来,人们一直在批处理文件中嵌入和执行 VBScript。但是我看到的所有已发布的解决方案(在最初提出这个问题时)都涉及编写临时 VBS 文件。例如:Embed VBScript inside W
如何检查文件夹中是否存在任意名称的文件?我还想忽略子文件夹。 谢谢。 编辑: 我想我已经明白了,但也感谢任何贡献...... If Folder.Files.Count > 0 Then 'Do
我对编程很陌生,我想制作一个程序,从 Vb 脚本的列表中随机选择一个名称或句子。 这是列表: Jacob James Jason Caleb Ashlee John 程序需要从该列表中选择一个随机名称
如何检查文件夹中是否存在任意名称的文件?我还想忽略子文件夹。 谢谢。 编辑: 我想我已经明白了,但也感谢任何贡献...... If Folder.Files.Count > 0 Then 'Do
我需要一个 Vbscript,它可以从我的 PC 打开一个图像文件,并在几分钟后自动关闭。我计划通过命令提示符运行脚本,非常感谢任何帮助。 最佳答案 使用 HTML Application 可能更容易
我是一名优秀的程序员,十分优秀!