我有从某个特定路径删除文件并将删除的文件名保存在.csv文件中的方案。当我在excel文件中运行开发人员脚本但在直接执行时没有运行时,它运行正常。 vbs文件也尝试使用cmd脚本。
InputFilePath = "C:\Users\Desktop\vba\FoldersCleanupInput.xlsx"
' Create the Excel object
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
Exit Sub
End If
' Don't display any alert messages
objExcel.DisplayAlerts = 0
' Open the document as read-only
On Error Resume Next
Call objExcel.Workbooks.Open(InputFilePath, False, True)
If (Err.Number <> 0) Then
Exit Sub
End If
' If you wanted to read all sheets, you could call
' objExcel.Worksheets.Count to get the number of sheets
' and the loop through each one. But in this example, we
' will just read the first sheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
Set objSheet2 = objExcel.ActiveWorkbook.Worksheets(2)
' Get the number of used rows
nUsedRows = objSheet.UsedRange.Rows.Count
' Get the number of used columns
nUsedCols = objSheet.UsedRange.Columns.Count
' Get the topmost row that has data
nTop = objSheet.UsedRange.Row
' Get leftmost column that has data
nLeft = objSheet.UsedRange.Column
' Get the used cells
Set objCells = objSheet.Cells
Set objSheet2Cells = objSheet2.Cells
logpath = objSheet2Cells(1, 2).Value
numberOfDays = objSheet2Cells(2, 2).Value
If objFSO.FileExists(logpath) Then
Set objFile = objFSO.OpenTextFile(logpath)
Else
Set objFile = objFSO.CreateTextFile(logpath, True)
Set objFile = objFSO.OpenTextFile(logpath)
End If
'Open logpath For Output As #1
' Dimension the sheet array
ReDim arrSheet(nUsedRows - 1, nUsedCols - 1)
' Loop through each row
For nRow = 0 To (nUsedRows - 1)
' Loop through each column
For nCol = 0 To (nUsedCols - 1)
For Each oFile In objFSO.GetFolder(objCells(nRow + nTop, nCol + nLeft).Value).Files
If oFile.DateLastModified < (Now() - numberOfDays) Then
'Dim i As Integer
' Print #1, "Deleted File Name:" & oFile & "Deleted on:" & Date & " " & Time
Set dd = "Deleted File Name:" & oFile & "Deleted on:" & Date & " " & Time
objFile.WriteLine dd
oFile.Delete (True)
End If
Next
Next
Next
'Close #1
' Close the workbook without saving
Call objExcel.ActiveWorkbook.Close(False)
' Quit Excel
objExcel.Application.Quit
Close objFile
End Sub
请建议我解决这个问题。谢谢你。