从图片获取标记元数据-文件夹和子文件夹-Vbscript
我正在组织我的照片,所以我想要一个VBscript,它可以将我照片中的所有标记写入Txt文件。脚本将从保存在不同子文件夹中的照片中读取标记,并写入所有标记,无需重复,因此我可以在此文件中列出唯一的标记 TXT文件将保存在Vbs文件的同一目录中。从图片获取标记元数据-文件夹和子文件夹-Vbscript,vbscript,metadata,photo,directory-structure,Vbscript,Metadata,Photo,Directory Structure,我正在组织我的照片,所以我想要一个VBscript,它可以将我照片中的所有标记写入Txt文件。脚本将从保存在不同子文件夹中的照片中读取标记,并写入所有标记,无需重复,因此我可以在此文件中列出唯一的标记 TXT文件将保存在Vbs文件的同一目录中。 “我的文件夹”有子文件夹。以下代码是为在Excel(VBA)上使用而开发的。我试图把它翻译成VBS,但没有成功。MVP Rick Rothstein的功劳。 如果我们能把代码修改成VBS,我想这是一个开始 Sub UniqueTextFileItems(
“我的文件夹”有子文件夹。以下代码是为在Excel(VBA)上使用而开发的。我试图把它翻译成VBS,但没有成功。MVP Rick Rothstein的功劳。 如果我们能把代码修改成VBS,我想这是一个开始
Sub UniqueTextFileItems()
Dim R As Long, FileNum As Long, TotalFile As String, Data As Variant
FileNum = FreeFile
Open "c:\temp\test.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum , , TotalFile
Close #FileNum
Data = Split(Join(Split(TotalFile, vbCrLf), ","), ",")
With CreateObject("Scripting.Dictionary")
For R = 0 To UBound(Data)
If Len(Data(R)) Then .Item(Data(R)) = 1
Next
Data = .Keys
End With
With CreateObject("System.Collections.ArrayList")
For R = 0 To UBound(Data)
.Add Data(R)
Next
.Sort
Range("A1").Resize(.Count) = Application.Transpose(.ToArray)
End With
End Sub
在这个论坛中搜索,我发现下面的代码非常棒,可以从数组中获取唯一的值 现在我需要知道如何解决循环中的在线代码问题:
Set objDirectory=objShell.Namespace(vFile)
Dim myArr作为变体
子测试函数()
将文件作为新集合
将MyPath设置为字符串
MyPath=“C:\Photos”
ReDim保留myArr(0)
RecursiveDir colFiles,MyPath,“*.jpg”,True
Dim objShell:Set objShell=CreateObject(“Shell.Application”)
Dim对象目录
作为变量的Dim vFile
对于colFiles中的每个vFile
'我在这里遇到错误-我无法动态引用命名空间
设置objDirectory=objShell.Namespace(vFile)
雷迪姆保留myArr(UBound(myArr)+1)
如果Len(Trim(objDirectory.GetDetailsOf(vrFile,18)))>0,那么
myArr(UBound(myArr))=objDirectory.GetDetailsOf(vrFile,18)
其他的
如果结束
下一个vFile
端接头
公共函数RecursiveDir(colFiles作为集合_
strFolder作为字符串_
strFileSpec作为字符串_
bIncludeSubfolders(作为布尔值)
将strTemp设置为字符串
将文件夹设置为新集合
Dim vFolderName作为变体
'将strFolder中与strFileSpec匹配的文件添加到colFiles
strFolder=TrailingSlash(strFolder)
strTemp=Dir(strFolder和strFileSpec)
当strTemp vbNullString时执行
添加strFolder和strTemp
strTemp=Dir
环
如果bIncludeSubfolders,则
'用strFolder的子目录列表填充colFolders
strTemp=Dir(strFolder,vbDirectory)
当strTemp vbNullString时执行
如果(strTemp“.”)和(strTemp“.”),则
如果(GetAttr(strFolder&strTemp)和vbDirectory)0,则
colFolders.addstrtemp
如果结束
如果结束
strTemp=Dir
环
'为colFolders中的每个子文件夹调用RecursiveDir
对于colFolders中的每个vFolderName
调用RecursiveDir(colFiles、strFolder和vFolderName、strFileSpec、True)
下一个vFolderName
如果结束
端函数
公共函数TrailingSlash(strFolder作为字符串)作为字符串
如果Len(strFolder)>0,则
如果正确(strFolder,1)=“\”则
TrailingSlash=strFolder
其他的
TrailingSlash=strFolder&“\”
如果结束
如果结束
端函数
Dim myArr As Variant
Sub TestFunction()
Dim colFiles As New Collection
Dim MyPath As String
MyPath = "C:\Photos"
ReDim Preserve myArr(0)
RecursiveDir colFiles, MyPath, "*.jpg", True
Dim objShell: Set objShell = CreateObject("Shell.Application")
Dim objDirectory
Dim vFile As Variant
For Each vFile In colFiles
'I'm getting Error here - I cannot dynamically refer the namespace
Set objDirectory = objShell.Namespace(vFile)
ReDim Preserve myArr(UBound(myArr) + 1)
If Len(Trim(objDirectory.GetDetailsOf(vrFile, 18))) > 0 Then
myArr(UBound(myArr)) = objDirectory.GetDetailsOf(vrFile, 18)
Else
End If
Next vFile
End Sub
Public Function RecursiveDir(colFiles As Collection, _
strFolder As String, _
strFileSpec As String, _
bIncludeSubfolders As Boolean)
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add files in strFolder matching strFileSpec to colFiles
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colFiles.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Fill colFolders with list of subdirectories of strFolder
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop
'Call RecursiveDir for each subfolder in colFolders
For Each vFolderName In colFolders
Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
Next vFolderName
End If
End Function
Public Function TrailingSlash(strFolder As String) As String
If Len(strFolder) > 0 Then
If Right(strFolder, 1) = "\" Then
TrailingSlash = strFolder
Else
TrailingSlash = strFolder & "\"
End If
End If
End Function