0

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.

TheDataPanda
  • 187
  • 1
  • 6
  • Does this answer your question? [read text file and make it in array - vbscript](https://stackoverflow.com/questions/44581406/read-text-file-and-make-it-in-array-vbscript) – LesFerch Jul 07 '22 at 12:23
  • Thanks. I'll take a look. I did find another solution just using a text file. – TheDataPanda Jul 07 '22 at 12:37
  • The key takeaways are to use `ReadAll` to load the file contents into a string variable and then use `Split` to convert the string into an array. In your case, split on `VBCRLF`. – LesFerch Jul 07 '22 at 13:15
  • I see you did just that in your answer. Perfect! – LesFerch Jul 07 '22 at 13:17
  • Thank you all. This was helpful. I've posted my final solution below. Only thing I'm worried about is if the text file is empty, I think `textFilePath` will compute to just the root directory (e.g. `"C:\project\Target"`). I just need to find a way to add some error handling to make sure this doesn't happen – TheDataPanda Jul 07 '22 at 13:23
  • You don't have to hard-code the path. You can use a relative path since the data file will be in the same folder as the script. See [this answer](https://stackoverflow.com/a/70043076/15764378) – LesFerch Jul 07 '22 at 14:04

1 Answers1

0

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
TheDataPanda
  • 187
  • 1
  • 6