从文件中读取配置信息到VBS数组
我已经编写了一个VBS脚本,以尝试从目录中的预定义子文件夹中删除所有文件。
这些子文件夹是在配置文件中定义的:
[folders]
des
dcs
我正在尝试在运行时加载此数据到VBS脚本中。配置文件将位于与VBS脚本同一文件夹中,在称为config
的子文件夹中。
我基本上希望在数组中存储的[文件夹
]下的值。以下是我使用变量代码进行了硬编码的示例
。
有人可以协助吗?
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = "C:\project\Target"
today = Date
nMaxFileAge = 3
codes = Array("des", "dcs") 'hardcoded for now
For Each code in codes
textFilePath = oFileSys.BuildPath(sRoot, code)
remove_files(textFilePath)
Next
Function remove_files(path)
Set oFolder = oFileSys.GetFolder(path)
Set aFiles = oFolder.Files
Set aSubFolders = oFolder.SubFolders
For Each file in aFiles
dFileCreated = FormatDateTime(file.DateCreated, "2")
if DateDiff("d", dFileCreated, today) > nMaxFileAge Then
file.Delete(True)
End If
Next
For Each folder in aSubFolders
remove_files(folder.Path)
Next
End Function
编辑:配置文件是哪种类型的文件并不重要。在某个地方,我可以定义一个文件夹列表,然后将其阅读到VBS中。
在关闭我的问题之前,别人已经链接的答案没有回答这个问题。请参阅下面的解决方案。
I've written a vbs script to try delete all files over a certain age from pre-defined subfolders within a directory.
These subfolders are defined in a configuration file:
[folders]
des
dcs
I'm trying to load this data into a VBS script during runtime. The config file will be located in the same folder as the VBS script, in a subfolder called Config
.
I basically want the values under [folder
] stored in an array. Below is an example where I've hardcoded this using the variable codes
.
Can someone please assist?
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = "C:\project\Target"
today = Date
nMaxFileAge = 3
codes = Array("des", "dcs") 'hardcoded for now
For Each code in codes
textFilePath = oFileSys.BuildPath(sRoot, code)
remove_files(textFilePath)
Next
Function remove_files(path)
Set oFolder = oFileSys.GetFolder(path)
Set aFiles = oFolder.Files
Set aSubFolders = oFolder.SubFolders
For Each file in aFiles
dFileCreated = FormatDateTime(file.DateCreated, "2")
if DateDiff("d", dFileCreated, today) > nMaxFileAge Then
file.Delete(True)
End If
Next
For Each folder in aSubFolders
remove_files(folder.Path)
Next
End Function
Edit: It doesn't really matter what type of file the configuration file is. Just somewhere I can define a list of folder and read it into VBS.
The answer someone else has linked to before closing my question doesn't answer the question. See my solution below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法在其他地方找到了一些代码来提供帮助。基本上,我只是存储在文本文件中的文件夹名称中,并在以下内容中读取这些文件。
I managed to find some code elsewhere to help. Basically I just stored in the folder names in a text file, and read those in: