Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vbscript 使用文件系统对象创建名称与字符串匹配的文件日志_Vbscript_Filesystemobject - Fatal编程技术网

Vbscript 使用文件系统对象创建名称与字符串匹配的文件日志

Vbscript 使用文件系统对象创建名称与字符串匹配的文件日志,vbscript,filesystemobject,Vbscript,Filesystemobject,我有一个目录,其中包含许多嵌套在不同文件夹和子文件夹中的同名文件。我正在尝试制作一个vbs脚本,它将在目录中搜索并找到任何名为“history”的文件,并将名称、路径和修改日期写入文件 我已经成功地创建了一个脚本来返回文件夹中的所有文件,但是还没有创建脚本,所以只能编写名为“History”的文件。我试图在for中添加If语句,因此如下所示: For Each ObjFolder In ObjSubFolders If ObjFolder.Name = "history*.*" Then

我有一个目录,其中包含许多嵌套在不同文件夹和子文件夹中的同名文件。我正在尝试制作一个vbs脚本,它将在目录中搜索并找到任何名为“history”的文件,并将名称、路径和修改日期写入文件

我已经成功地创建了一个脚本来返回文件夹中的所有文件,但是还没有创建脚本,所以只能编写名为“History”的文件。我试图在for中添加If语句,因此如下所示:

For Each ObjFolder In ObjSubFolders
    If ObjFolder.Name = "history*.*" Then
    ResultLogFile.WriteLine(ObjFolder.Name & vbab & ObjFolder.Path)
End If
但这是行不通的

这就是我到目前为止所做的:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim fso
Dim OutputFileName
Dim ResultLogFile


vCurrentDate = Now
'Year string
vCurrentYear = CStr(DatePart("yyyy",vCurrentDate))

'Month string
If DatePart("m",vCurrentDate) < 10 Then
    vCurrentMonth = "0" & CStr(DatePart("m",vCurrentDate))
Else
    vCurrentMonth = CStr(DatePart("m",vCurrentDate))
End If

'Day string
If DatePart("d",vCurrentDate) < 10 Then
    vCurrentDay = "0" & CStr(DatePart("d",vCurrentDate))
Else
    vCurrentDay = CStr(DatePart("d",vCurrentDate))
End If  

Set fso = CreateObject("Scripting.FileSystemObject")

OutputFileName = "C:\historylogs\" & vCurrentYear & "-" & vCurrentMonth & "-" & vCurrentDay & ".tsv"

'MySourcePath = "C:\Test Folder\"
'Check if file already exists:

If fso.FileExists(OutputFileName) Then
    'File exists, so open it for appending and add no new header info:
    Set ResultLogFile = fso.OpenTextFile(OutputFileName, ForAppending, True, TristateTrue)
Else
    'File did not exist, so create it and add a header
    Set ResultLogFile = fso.OpenTextFile(OutputFileName, ForWriting, True, TristateTrue)
    'Create log headers
    ResultLogFile.WriteLine "FileName" & vbTab & "FilePath" & vbTab & "DateLastModified"
End If

GetFiles("C:\Test Folder")

Function GetFiles(FolderName)
    'On Error Resume Next

Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile 

Set ObjFolder = fso.GetFolder(FolderName)
Set ObjFiles = ObjFolder.Files

'Write all files to output files
For Each ObjFile In ObjFiles
    ResultLogFile.WriteLine(ObjFile.Name & vbTab & ObjFile.Path & vbTab & ObjFile.DateLastModified)
Next
'Getting all subfolders
Set ObjSubFolders = ObjFolder.SubFolders

For Each ObjFolder In ObjSubFolders
    'Writing SubFolder Name and Path
    ResultLogFile.WriteLine(ObjFolder.Name & vbab & ObjFolder.Path)

    'Getting all Files from subfolder
    GetFiles(ObjFolder.Path)
Next

End Function
Const for reading=1,for write=2,for appending=8
常量TristateUseDefault=-2,TristateTrue=-1,TristateFalse=0
模糊fso
Dim输出文件名
Dim结果日志文件
vCurrentDate=现在
'年串
vCurrentYear=CStr(日期部分(“yyyy”,vCurrentDate))
'月字符串
如果DatePart(“m”,vCurrentDate)<10,则
vCurrentMonth=“0”和CStr(日期部分(“m”,vCurrentDate))
其他的
vCurrentMonth=CStr(日期部分(“m”,vCurrentDate))
如果结束
“日线
如果DatePart(“d”,vCurrentDate)<10,则
vCurrentDay=“0”和CStr(日期部分(“d”,vCurrentDate))
其他的
vCurrentDay=CStr(日期部分(“d”,vCurrentDate))
如果结束
设置fso=CreateObject(“Scripting.FileSystemObject”)
OutputFileName=“C:\historylogs\”&vCurrentYear&“-”&vCurrentMonth&“-”&vCurrentDay&“.tsv”
'MySourcePath=“C:\Test Folder\”
'检查文件是否已存在:
如果存在fso.files(OutputFileName),则
'文件存在,因此打开它进行追加,并且不添加新的头信息:
设置ResultLogFile=fso.OpenTextFile(OutputFileName,ForAppending,True,TristateTrue)
其他的
'文件不存在,请创建它并添加头
设置ResultLogFile=fso.OpenTextFile(OutputFileName,ForWriting,True,TristateTrue)
'创建日志头
ResultLogFile.WriteLine“FileName”&vbTab&“FilePath”&vbTab&“DateLastModified”
如果结束
GetFiles(“C:\Test文件夹”)
函数GetFiles(FolderName)
'出现错误时,请继续下一步
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim OBJJ文件
Dim ObjFile
设置ObjFolder=fso.GetFolder(FolderName)
设置ObjFiles=ObjFolder.Files
'将所有文件写入输出文件
对于ObjFiles中的每个ObjFile
ResultLogFile.WriteLine(ObjFile.Name&vbTab&ObjFile.Path&vbTab&ObjFile.DateLastModified)
下一个
'获取所有子文件夹
设置ObjSubFolders=ObjFolder.SubFolders
对于ObjSubFolders中的每个ObjFolder
'正在写入子文件夹名称和路径
ResultLogFile.WriteLine(ObjFolder.Name&vbab&ObjFolder.Path)
'正在从子文件夹获取所有文件
GetFiles(ObjFolder.Path)
下一个
端函数

你可以用几种方法来实现。。如果…,请更改此选项

For Each ObjFile In ObjFiles
  ResultLogFile.WriteLine(ObjFile.Name & vbTab & ObjFile.Path & vbTab & ObjFile.DateLastModified)
Next
..您希望在名称中查找任何具有历史记录的文件,而不管在何处:

For Each ObjFile In ObjFiles
  If InStr(1, ObjFile.Name, "history") Then
    ResultLogFile.WriteLine(ObjFile.Name & vbTab & ObjFile.Path & vbTab & ObjFile.DateLastModified)
  End If
Next
..要查找以历史记录开头的所有文件:

For Each ObjFile In ObjFiles
  If LCase(Mid(ObjFile.Name, 1, 7)) = "history" Then
    ResultLogFile.WriteLine(ObjFile.Name & vbTab & ObjFile.Path & vbTab & ObjFile.DateLastModified)
  End If
Next
编辑:回应你的评论,你可以有这样一个递归子。它将搜索所有文件夹和子文件夹,查找以您传递的内容开头的任何文件。。因此,如果您传递“历史记录”和文件历史记录*将被找到

Set objFSO = CreateObject("Scripting.FileSystemObject")

Call findFiles(objFSO.GetFolder("C:\temp"), "history")

Sub findFiles(objFolder, strMatchString)

  For Each objSubFolder In objFolder.SubFolders
    Call findFiles (objSubFolder, strMatchString)
  Next

  Set objFiles = objFolder.Files

  For Each objFile In objFiles
    If LCase(Mid(objFile.Name, 1, Len(strMatchString))) = LCase(strMatchString) Then
      MsgBox objFile.Name & vbTab & objFile.Path & vbTab & objFile.DateLastModified
    End If
  Next 
End Sub

谢谢这对于从主文件夹中获取历史文件是有效的,但是在子文件夹中也有一些我尝试过的,但是它失败了!这可以将历史文件从主文件夹中取出,但在子文件夹中也有一些文件对您的评论进行了响应。您需要将MsgBox更改为ResultLogFile.WriteLine对象并删除一些不需要的代码,但这应该满足您的要求。