1

I have a system that's running an old FoxPro program which generates 8 character long DBF files. We make a back up of the program folder each day, but at 5pm the program has generated so many of these garbage dbf's that it's a nuisance. I would just set a del *.dbf in the back up script but there are a few dbf with letters in their name that are needed to run the program.

Files are located in F:\Clean This\

Any numerically titled .dbf files need to be deleted

Any alphabetically titled .dbf file should be left alone

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "F:\Clean This\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
collide = "ABCEDFGHIJKLMNOPQRSTUVWXYZ"

For Each objFile in colFiles
    If UCase(objFSO.GetExtensionName(objFile.name)) = "DBF" Then
        num = 1
        For num = 1 to 26 'find files with names start with # 0-9
            If Left(objFile.Name,1) = Left(collide,num) Then
                Wscript.Echo "Save " & objFile.Name
            Else If int(Left(objFile.Name,1)) > 0 Then
                Wscript.Echo "Delete!"
            End IF
            End If
        Next
    End If
Next

As you can tell the If statements can be done better, I'm unsure how to better work it out. The two Wscript.Echo commands are just placeholders because if anything else I can't get find a suitable delete function that would work in a dos environment (I've already tried kill, no).

Suggestions and improvements would be much appreciated!

4

2 回答 2

1

使用 IsNumeric() 检查仅由数字组成的文件名:

>> For Each sN In Split("abc 123 1O1 101")
>>     If IsNumeric(sN) Then
>>        WScript.Echo "delete", sN
>>     Else
>>        WScript.Echo "keep", sN
>>     End If
>> Next
>>
keep abc
delete 123
keep 1O1
delete 101

您的检查失败,因为您在应该使用 Mid() 的地方使用了 Left():

>> collide = "ABCEDFGHIJKLMNOPQRSTUVWXYZ"
>> num = 5
>> WScript.Echo Left(collide,num)
>> WScript.Echo Mid(collide,num,1)
>>
ABCED
D

即使这样,Left(objFile.Name,1)也只会查看文件名的第一个字符。

更新(写评论):

将 IsNumeric() 应用于基本名称:

  Dim oFile
  For Each oFile In goFS.GetFolder("..\testdata\17817161").Files
      WScript.Stdout.Write oFile.Name
      If "dbf" = LCase(goFS.GetExtensionName(oFile.Name)) Then
         If IsNumeric(goFS.GetBaseName(oFile.Name)) Then
            WScript.Stdout.WriteLine " delete"
         Else
            WScript.Stdout.WriteLine " keep"
         End If
      Else
         WScript.Stdout.WriteLine " ignore"
      End If
  Next

输出:

123.dbf delete
123.txt ignore
abc.dbf keep
于 2013-07-23T17:56:05.247 回答
0

我终于让它工作了,这就是最终结果。它运行得非常好,高层和其他人对它比我们正在备份的程序的内置实用程序更好地印象深刻。分数!

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "..\System Folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set DirFiles = objFolder.Files
Dim oFile
For Each objFile in DirFiles
If "dbf" = LCase(objFSO.GetExtensionName(objFile.Name)) Then
    If IsNumeric(objFSO.GetBaseName(objFile.Name)) Then
        objFSO.DeleteFile(objFile)
    End If
End If    
Next
于 2013-07-26T17:13:40.080 回答