diy toy: image auto-handler

备忘之:)

 

config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <zoom width="1296" height="100">
    </zoom>
    <rotation angle="270">
    </rotation>
    <save>
        <path>D:\0122</path>
    </save>
    <author prefix="xxoo">
    </author>
    <monitor total="10">
    </monitor>
</config>

 

image-auto-handler.vbs

  1 'Whatfor:  rename pictures with Exif property [artist]
  2 'Author:   lichmama nextgodhand@163.com
  3 'Runtime:  win-NT series(x86/x64), which installed WIA component.
  4 'LICENSE:  
  5 '    Copyright ;copy; 2014-2015 lichmama nextgodhand@163.com 
  6 '    All rights reserved.
  7 '    FOR NON-COMMERCIAL USE: you can use, copy, modify and distribute this code,
  8 '    but please keep the information of LICENSE & Author.
  9 
 10 '*ChangeLog:
 11 '    1.add zoom and rotation
 12 '    2.backup the folder [*.NEW] to config path
 13 '    reedited: 2015/04/16
 14 '    3.change the naming style, which supports the [alpha] property
 15 '    3.*not keeping the folder [*.NEW]
 16 '    reedited: 2015/10/27
 17 '    4.add monitor function, which supports the [total] property
 18 '    reedited: 2015/10/31, happy halloween:)
 19 '    5.change the config file to config.xml [original, config.ini]
 20 '    5.*add verifying for configurations
 21 '    reedited: 2015/11/1
 22 
 23 Set fso = CreateObject("scripting.filesystemobject")
 24 Set imgfile = CreateObject("wia.imagefile")
 25 Set sdp = CreateObject("scripting.dictionary")
 26 Set regex = CreateObject("vbscript.regexp")
 27 Set monitor = CreateObject("scripting.dictionary")
 28 set config = new Config_Class
 29 
 30 '================script starts!================
 31 Call sdp.Add("PNG", "PNG FILE")
 32 Call sdp.Add("GIF", "GIF FILE")
 33 Call sdp.Add("BMP", "BMP FILE")
 34 Call sdp.Add("JPG", "JPG FILE")
 35 
 36 If WScript.Arguments.Count = 0 Then
 37     Call Quit_Job(True)
 38 Else
 39     path = WScript.Arguments(0)
 40     If Not fso.FolderExists(path) Then
 41         Call Quit_Job(True)
 42     End If
 43 End If
 44 
 45 If Not ReadConfig() Then
 46     Call Wscript.Echo("[配置文件错误]:运行前请保证配置文件config.ini存在,并且正确!")
 47     Call Quit_Job(False)
 48 End If
 49 
 50 If Not fso.FolderExists(path & ".NEW") Then
 51     Call fso.CreateFolder(path & ".NEW")
 52 End If
 53 
 54 For Each picture In fso.GetFolder(path).Files
 55     extension = UCase(fso.GetExtensionName(picture))
 56     If sdp.Exists(extension) Then
 57         Call imgfile.LoadFile(picture)
 58         For index = 1 To imgfile.Properties.Count
 59             name = imgfile.Properties(index).Name
 60             If name = "Artist" Then
 61                 regex.Pattern ="\d*"
 62                 value = imgfile.Properties(index).Value
 63                 value = regex.Execute(value)(0)
 64                 index = CInt(value)
 65                 regex.Pattern = "^(0*)"
 66                 value = regex.Replace(value, config.author_prefix)
 67                 Call Zoom_Rotate()
 68                 Call imgfile.SaveFile(path & ".NEW\" & value & "." & extension)
 69                 monitor.Item(index) = path & ".NEW\" & value & "." & extension
 70                 Exit For
 71             End If
 72         Next
 73     End If
 74 Next
 75 Call Monitor_Total()
 76 
 77 'move the folder [*.NEW] to config path
 78 regex.Pattern = "([^\\]+)$"
 79 dest = fso.GetAbsolutePathName(path)
 80 dest = regex.Execute(dest)(0).SubMatches(0)
 81 For i=0 To config.save_path.Count-1
 82     Call fso.CopyFolder(path & ".NEW", config.save_path.Item(i) & "\" & dest)
 83 Next
 84 Call fso.DeleteFolder(path & ".NEW")
 85 
 86 MsgBox "批处理执行完毕!",vbInformation,"Image Auto-Handler v0.1"
 87 Call Quit_Job(False)
 88 '================script ends here==============
 89 Function ReadConfig()
 90 On Error Resume Next
 91     xmlsource = Replace(WScript.ScriptFullName, WScript.ScriptName, "config.xml")
 92     If fso.FileExists(xmlsource) Then 
 93         Call config.init_config(xmlsource)
 94         For i = 1 To config.monitor_total
 95             Call monitor.Add(i, "")
 96         Next
 97         For i = 0 To config.save_path.Count-1
 98             If Not fso.FolderExists(config.save_path.Item(i)) Then
 99                 WScript.Echo "[配置文件错误]:保存路径不存在!"
100                 Call Quit_Job(False)
101             End If
102         Next
103         ReadConfig = True
104     Else
105         ReadConfig = False
106     End If
107 If Err.Number <> 0 Then
108     ReadConfig = False
109 End If
110 End Function
111 
112 Sub Monitor_Total()
113     regex.Pattern = "(" & config.author_prefix & ")\d+"
114     If monitor.Item(1) = "" Then
115         For i = 2 To monitor.Count
116             If monitor.Item(i) <> "" Then
117                 clone = regex.Replace(monitor.Item(i), "$1" & 1)
118                 monitor.Item(1) = clone
119                 Call fso.CopyFile(monitor.Item(i), monitor.Item(1))
120                 Exit For
121             End If
122         Next
123     End If
124     
125     For i = 2 To monitor.Count
126         If monitor.Item(i) = "" Then
127             clone = regex.Replace(monitor.Item(i-1), "$1" & i)
128             monitor.Item(i) = clone
129             Call fso.CopyFile(monitor.Item(i-1), monitor.Item(i))
130         End If
131     Next
132 End Sub
133 
134 Sub Zoom_Rotate()
135     Set imgproc = CreateObject("wia.imageprocess")
136     'zoom
137     Call imgproc.Filters.Add(imgproc.FilterInfos("Scale").FilterID)
138     imgproc.Filters(1).Properties("MaximumWidth") = config.zoom_width
139     imgproc.Filters(1).Properties("MaximumHeight") = config.zoom_width
140     'rotate
141     Call imgproc.Filters.Add(imgproc.FilterInfos("RotateFlip").FilterID)
142     imgproc.Filters(2).Properties("RotationAngle") = 0
143     'apply
144     Set imgfile = imgproc.Apply(imgfile)
145 End Sub
146 
147 Sub Quit_Job(force)
148     If force = True Then
149         MsgBox "[使用方法]: 将文件夹拖放到脚本上,开始重命名图片。", vbInformation, "Image Auto-Handler v0.1"
150     End If
151     Set fso = Nothing
152     Set imgfile = Nothing
153     Set sdp = Nothing
154     Set regex = Nothing
155     Set monitor = Nothing
156     Call WScript.Quit()
157 End Sub
158 
159 Class Config_Class
160     Private my_zoom_width
161     Private my_zoom_height
162     Private my_rotation_angle
163     Private my_save_path
164     Private my_author_prefix
165     Private my_monitor_total
166     
167     Public Sub init_config(xmlsource)
168         Set xml = CreateObject("msxml2.domdocument")
169         Call xml.load(xmlsource)
170         
171         Set zoom = xml.getElementsByTagName("zoom").item(0)
172         my_zoom_width = zoom.attributes.getNamedItem("width").text
173         my_zoom_height = CInt(zoom.attributes.getNamedItem("height").text)
174         
175         Set rotation = xml.getElementsByTagName("rotation").item(0)
176         my_rotation_angle = rotation.attributes.getNamedItem("angle").text
177         
178         Set save = xml.getElementsByTagName("save").item(0)
179         Set my_save_path = CreateObject("scripting.dictionary")
180         For i = 0 To save.childNodes.length - 1
181             Call my_save_path.Add(i, save.childNodes.item(i).text)
182         Next
183         
184         Set author = xml.getElementsByTagName("author").item(0)
185         my_author_prefix = author.attributes.getNamedItem("prefix").text
186         
187         Set xml_monitor = xml.getElementsByTagName("monitor").item(0)
188         my_monitor_total = xml_monitor.attributes.getNamedItem("total").text
189     End Sub
190     
191     Public Property Get zoom_width
192         zoom_width = my_zoom_width
193     End Property
194     
195     Public Property Get zoom_height
196         zoom_height = my_zoom_height
197     End Property
198     
199     Public Property Get rotation_angle
200         rotation_angle = my_rotation_angle
201     End Property
202     
203     Public Property Get save_path
204         Set save_path = my_save_path
205     End Property
206     
207     Public Property Get author_prefix
208         author_prefix = my_author_prefix
209     End Property
210     
211     Public Property Get monitor_total
212         monitor_total = my_monitor_total
213     End Property
214 End Class

 

posted @ 2015-11-01 20:37  lichmama  阅读(288)  评论(0编辑  收藏  举报