简体   繁体   English

如何根据单元格值删除文件?

[英]How can I delete a file based on cell value?

I'm having trouble deleting a file based on cell value. 我在删除基于单元格值的文件时遇到问题。 I get an error message on the line with the Kill command below: 我在下面的Kill命令中收到一条错误消息:

Kill path & r.Offset(1, -4) & "\" & r.Offset(1, -3)

Any ideas? 有任何想法吗?

Sub INACTIVE_files()

    Const path = "C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\"

    Dim r As Range
    Dim x As Integer

    Set r = Cells(1, 5)
    Do Until r = ""
        If UCase(r.value) = "INACTIVE" Then

            Kill path & r.Offset(1, -4) & "\" & r.Offset(1, -3)
          End If
          Set r = r.Offset(1, 0)
    Loop

End Sub

The code starts from cell E1 and looks for INACTIVE files in the same column, until there's no more files to look for. 该代码从单元格E1开始,并在同一列中查找INACTIVE文件,直到没有其他文件可查找为止。 Then, it checks the folder name (Column A), combines it with the Cube (Column B) and puts both of them in a path: 然后,它检查文件夹名称(A列),将其与多维数据集(B列)合并,然后将它们都放在路径中:

path = "C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\"

so for example: for cell E2 which is INACTIVE, the path should be: 因此,例如:对于无效的单元格E2,路径应为:

C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\WPO 17 02 04 3MMT All Periods\BG023104.txt

It then deletes the INACTIVE files (Cubes) from the appropriate folder. 然后,它从相应的文件夹中删除INACTIVE文件(多维数据集)。

在此处输入图片说明

Wrap your path in double quotes to avoid issues with spaces in filenames and folders. 将路径用双引号引起来,以避免文件名和文件夹中的空格出现问题。

Even better is to put the path in a string variable so you can debug it easily 更好的是将路径放在字符串变量中,以便您轻松调试它

Outside your loop: 在循环之外:

Dim strPath As String

Inside your if block: 在您的if块内:

   strPath = """" & path & r.Offset(1,-4) & "\" & r.Offset(1,-3) & """"
   Debug.Print strPath ' Ctrl-G to view results
   Kill strPath

EDIT - add a check for file before deleting 编辑-删除前添加文件检查

Under Tools | References Tools | References Tools | References

Add a reference to Windows Script Hosting 添加对Windows脚本托管的引用

Then at top of sub code add 然后在子代码顶部添加

Dim fso as New FileSystemObject

Replace your Kill command with a check for existence 用检查是否存在来替换您的Kill命令

If fso.FileExists(strPath) Then
   Kill strPath
Else
   Msgbox "File Doesn't Exist: " & strPath
End If

UPDATED FOR CONTINUE TO NEXT FILE 已更新以继续到下一个文件

Change loop to be: 将循环更改为:

Do Until r = ""
   If UCase(r.value) = "INACTIVE" AND fso.FileExists(strPath) Then
        Kill strPath
   End If
   Set r = r.Offset(1, 0)
Loop

It works! 有用! I've commented out some parts of the code that were used for checking if a file exists. 我已经注释掉了用于检查文件是否存在的部分代码。

 Sub delete_INACTIVE_files()


Const path = "C:\Users\Dn\AppData\Local\Temp\vbakillfunction\"
Dim r As Range


Set r = Cells(1, 5)

Do Until r = ""


    If UCase(r.Value) = "INACTIVE" Then

        If Dir(path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt") <> "" Then 'Does the file exist?

            'MsgBox "file" & path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt" & " exists"

            Kill path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt"


        'Else

            'MsgBox "file" & path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt" & " not here"


        End If

    End If

    Set r = r.Offset(1, 0)

Loop

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM