从文件中读取配置信息到VBS数组

发布于 2025-02-13 17:09:23 字数 1122 浏览 2 评论 0原文

我已经编写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2025-02-20 17:09:23

我设法在其他地方找到了一些代码来提供帮助。基本上,我只是存储在文本文件中的文件夹名称中,并在以下内容中读取这些文件。

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOpen = objFSO.OpenTextFile("C:\project\Target\test.txt", ForReading)
Dim code()

sRoot = "C:\project\Target"
today = Date
nMaxFileAge = 3

FileContent = objOpen.ReadAll
msgbox FileContent
codes = Split(FileContent, VbCrLF)
objOpen.Close

Set objOpen = Nothing

For Each code in codes
    textFilePath = objFSO.BuildPath(sRoot, code)
    msgbox textFilePath
    remove_files(textFilePath)
Next

Function remove_files(path)
  Set oFolder = objFSO.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

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:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOpen = objFSO.OpenTextFile("C:\project\Target\test.txt", ForReading)
Dim code()

sRoot = "C:\project\Target"
today = Date
nMaxFileAge = 3

FileContent = objOpen.ReadAll
msgbox FileContent
codes = Split(FileContent, VbCrLF)
objOpen.Close

Set objOpen = Nothing

For Each code in codes
    textFilePath = objFSO.BuildPath(sRoot, code)
    msgbox textFilePath
    remove_files(textFilePath)
Next

Function remove_files(path)
  Set oFolder = objFSO.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文