VBS batch renames files and backs up the original files before operation

Time:2021-10-18

Core function

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME:
'
' AUTHOR: Microsoft , Microsoft
' DATE : 2014/7/9
'
'comment:' batch modify all corresponding file names under the folder
'
'==========================================================================
'select my computer as the root directory to select the directory
Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
(WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath)
If objFolder Is Nothing Then
Wscript.Quit
End If
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'MsgBox objFolderItem.name
'===================================================================
'select the directory under the specified drive letter
' Const WINDOW_HANDLE = 0
' Const OPTIONS = 0
'
' Set objShell = CreateObject("Shell.Application")
' Set objFolder = objShell.BrowseForFolder _
' (WINDOW_HANDLE, "Select a folder:", OPTIONS, "C:\")
'
' If objFolder Is Nothing Then
' Wscript.Quit
' End If
'
' Set objFolderItem = objFolder.Self
' objPath = objFolderItem.Path
'
' MsgBox objPath
'=========================================================================
'define variables
dim file_path,prefix_name,suffix_name,repeat_name,repeat_edit
Dim OneLine,TwoLine,ThreeLine,FourLine,FiveLine
i=0
test = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
'Wscript.echo test
filepath=test&"\config.ini"
'WScript.Echo filepath
' file_ Path = "C: \ users \ administrator \ desktop \ 1 \ music" 'the path to the destination folder
dst_file_path="C:\"&objFolderItem.name&"_bak"
file_path=objPath
'----- get the folder path and open the configuration file
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(file_path)
Set fs = folder.files
Set file=fso.OpenTextFile(filepath,1)
'---------------- back up the original files before operation
fso.CopyFolder file_path,dst_file_path,True
'----------------------------------
'Take out the two numbers in the first row
OneLine=file.ReadLine
OneLineStr=Split(OneLine,"=")
OneLineCount=UBound(split(OneLine,"="))
For i1=0 To OneLineCount
'WScript.Echo OneLineStr(i1)
Next
'-------------------------------------
'Take out the two numbers in the second row
TwoLine=file.ReadLine
TwoLineStr=Split(TwoLine,"=")
TwoLineCount=UBound(split(TwoLine,"="))
For i2=0 To TwoLineCount
'WScript.Echo TwoLineStr(i2)
Next
'-------------------------------------------
'Take out the two numbers in the third row
ThreeLine=file.ReadLine
ThreeLineStr=Split(ThreeLine,"=")
ThreeLineCount=UBound(split(ThreeLine,"="))
For i3=0 To ThreeLineCount
'WScript.Echo ThreeLineStr(i3)
Next
'-------------------------------------------
'Take out the two numbers in the fourth row
FourLine=file.ReadLine
FourLineStr=Split(FourLine,"=")
FourLineCount=UBound(split(FourLine,"="))
For i4=0 To FourLineCount
'WScript.Echo FourLineStr(i4)
Next
'-----------------------------------------
'Take out the two numbers in the fifth row
FiveLine=file.ReadLine
FiveLineStr=Split(FiveLine,"=")
FiveLineCount=Ubound(split(FiveLine,"="))
For i5=0 To FiveLineCount
'WScript.Echo FiveLineStr(i5)
Next
'---------------------------------------------
'call procedure
'Function_Main()
Function Function_Main()
If OneLineStr(1)="true" Then
Function_Prefix_Name()
Elseif OneLineStr(1)="false" Then
Function_Suffix_Name()
Elseif OneLineStr(1)="number" Then
Function_Number_Value()
Elseif OneLineStr(1)="array" Then  
Function_MyArrayReName()
Elseif OneLineStr(1)="" Then
WScript.Quit
End If
End Function
'-----------------------------------------
'prefix the original name
Function Function_Prefix_Name()
For Each file in fs
File.Name=TwoLineStr(1)&File.Name
Next
End Function
'--------------------------------------
'add suffix before original name
Function Function_Suffix_Name()
For Each file in fs
Name=Mid(file.name,1,instrrev(file.name,".")-1) 'get the file name before the. Sign
Format=Mid(file.name,instrrev(file.name,".")) 'get the suffix format after the. Sign
file.Name=Name&ThreeLineStr(1)&Format
Next
End Function
'--------------------------------------------
'add an ordered self increasing number before the original name
Function Function_Number_Value()
For Each file In fs
FourLineStr(1)=FourLineStr(1)+1
file.name=FourLineStr(1)&file.name
Next
End Function
'Function_Suffix_Name()
'--------------------------------------------------
'batch change file name
Function Function_MyArrayReName()
Const BeforAlarm="In case of prison violence, please pay attention"
Const AfterAlarm="In case of prison violence, all teams shall deal with it according to the plan"
Dim MyArray(12)
n=1
y=0
For i=0 To 12
If i=11 Then
MyArray(i)="Gate guard"
Elseif i=12 Then
MyArray(i)="Self defense post"
Else
MyArray(i)=n&"Whistle"
n=n+1
End If
' WScript.Echo MyArray(i)
Next
For Each file In fs
Format=Mid(file.name,instrrev(file.name,"."))
'MsgBox Format
'MsgBox MyArray(y)
If FiveLineStr(1)="before" Then
file.name=MyArray(y)&BeforAlarm&Format
Elseif FiveLineStr(1)="after" Then
file.name=MyArray(y)&AfterAlarm&Format
Else
MsgBox "Please set whether to confirm before or after confirmation!",,"Prompt"
WScript.Quit
End If
y=y+1
'WScript.Echo file.name
Next
End Function
 
'=======================================================================
' If prefix_ Name < > "then" batch prefix
' For each f in fs
' f.name = prefix_name&f.name
' Next
' End If
'
' if suffix_ Name < > "then" batch suffix
' For each f in fs
' name = Mid(f.name,1,InstrRev(f.name,".")-1)
' format = Mid(f.name,InstrRev(f.name,"."))
' f.name = name & suffix_name & format
' Next
' end If
'
' if repeat_ Name < > "" then 'delete the same characters in batch
' For each f in fs
' On Error Resume Next
' f.name = Replace(f.name,repeat_name,repeat_edit)
' Next
' end If
'----- end of file operation
'
'set FSO = nothing 'free memory
'
'msgbox ("done!")

Required profile

?
1
2
3
4
5
6
Config.ini file content:
statue=
prefix_name=[320kbp]
suffix_ Name = [End]
i=20140100
array=

Parameter configuration usage:

Add prefix when status = true
When state = false, the suffix is added
When status = number, it is an ordered self increasing number.
Status = array is to call the array function
When status = null value, it is null and will not be processed. Exit the script operation.
When array = before, it is set to before confirmation.
When array = after, set it to after confirmation.
When array = empty, a prompt message will pop up and exit the script operation.

Well, that’s all for this article. It mainly uses the filesystembobject and mid functions

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]