Windows操作系统可以实现重命名文件操作,却不能实现批量重命名。本实例实现了批量重命名功能,可以将一个文件夹内同一类型的文件按照一定的规则批量重命名。用户可以给出重命名模板,程序可以根据模板对相应的文件进行重命名。此外,还可以在重命名模板中添加特殊符号,程序会将这些特殊符号替换成重命名后的文件编号。
思路分析:
代码如下:
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 |
import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileFilter; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import
javax.swing.JLabel; import
javax.swing.JOptionPane; import
javax.swing.JPanel; import
javax.swing.JScrollPane; import
javax.swing.JSeparator; import
javax.swing.JSpinner; import
javax.swing.JTable; import
javax.swing.JTextField; import
javax.swing.border.EmptyBorder; import
javax.swing.table.DefaultTableModel; import
java.awt.GridBagLayout; import
java.awt.GridBagConstraints; import
java.awt.Insets; /** * 获取文件列表的过滤器 * * @author 李钟尉 */ public
class RenameFiles extends
JFrame { /** * */ private
static final long serialVersionUID = 4534371106024773867L; private
final class ExtNameFileFilter implements
FileFilter { private
String extName; public
ExtNameFileFilter(String extName) { this .extName = extName; // 保存文件扩展名 } @Override public
boolean accept(File pathname) { // 过滤文件扩展名 if
(pathname.getName().toUpperCase() .endsWith(extName.toUpperCase())) return
true ; return
false ; } } private
JPanel contentPane; private
JTextField forderField; private
JTextField templetField; private
File dir; private
JTable table; private
JTextField extNameField; private
JSpinner startSpinner; /** * Launch the application. */ public
static void main(String[] args) { EventQueue.invokeLater( new
Runnable() { public
void run() { try
{ RenameFiles frame = new
RenameFiles(); frame.setVisible( true ); } catch
(Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public
RenameFiles() { setResizable( false ); setTitle( "文件批量重命名" ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds( 100 , 100 , 383 , 409 ); contentPane = new
JPanel(); contentPane.setBorder( new
EmptyBorder( 5 , 5 , 5 , 5 )); setContentPane(contentPane); GridBagLayout gbl_contentPane = new
GridBagLayout(); gbl_contentPane.columnWidths = new
int [] { 72 , 54 , 60 , 87 , 91 , 0
}; gbl_contentPane.rowHeights = new
int [] { 25 , 25 , 10 , 25 , 24 , 25 , 2 , 216 , 0
}; gbl_contentPane.columnWeights = new
double [] { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , Double.MIN_VALUE }; gbl_contentPane.rowWeights = new
double [] { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , Double.MIN_VALUE }; contentPane.setLayout(gbl_contentPane); JLabel label = new
JLabel(); label.setText( "文件批量重命名模块:" ); GridBagConstraints gbc_label = new
GridBagConstraints(); gbc_label.fill = GridBagConstraints.VERTICAL; gbc_label.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_label.gridwidth = 3 ; gbc_label.gridx = 1 ; gbc_label.gridy = 0 ; contentPane.add(label, gbc_label); JLabel label_1 = new
JLabel(); label_1.setText( "文件路径:" ); GridBagConstraints gbc_label_1 = new
GridBagConstraints(); gbc_label_1.anchor = GridBagConstraints.EAST; gbc_label_1.fill = GridBagConstraints.VERTICAL; gbc_label_1.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_label_1.gridx = 0 ; gbc_label_1.gridy = 1 ; contentPane.add(label_1, gbc_label_1); forderField = new
JTextField(); forderField.setText( "" ); GridBagConstraints gbc_forderField = new
GridBagConstraints(); gbc_forderField.fill = GridBagConstraints.HORIZONTAL; gbc_forderField.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_forderField.gridwidth = 3 ; gbc_forderField.gridx = 1 ; gbc_forderField.gridy = 1 ; contentPane.add(forderField, gbc_forderField); JButton button = new
JButton(); button.addActionListener( new
ActionListener() { public
void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); button.setText( "浏览" ); GridBagConstraints gbc_button = new
GridBagConstraints(); gbc_button.anchor = GridBagConstraints.NORTHWEST; gbc_button.insets = new
Insets( 0 , 0 , 5 , 0 ); gbc_button.gridx = 4 ; gbc_button.gridy = 1 ; contentPane.add(button, gbc_button); JSeparator separator_1 = new
JSeparator(); GridBagConstraints gbc_separator_1 = new
GridBagConstraints(); gbc_separator_1.fill = GridBagConstraints.BOTH; gbc_separator_1.insets = new
Insets( 0 , 0 , 5 , 0 ); gbc_separator_1.gridwidth = 5 ; gbc_separator_1.gridx = 0 ; gbc_separator_1.gridy = 2 ; contentPane.add(separator_1, gbc_separator_1); JLabel label_5 = new
JLabel(); label_5.setText( "使用#可以指定数字计数所占的位置,使用*可以插入原文件名:" ); GridBagConstraints gbc_label_5 = new
GridBagConstraints(); gbc_label_5.fill = GridBagConstraints.VERTICAL; gbc_label_5.insets = new
Insets( 0 , 0 , 5 , 0 ); gbc_label_5.gridwidth = 5 ; gbc_label_5.gridx = 0 ; gbc_label_5.gridy = 3 ; contentPane.add(label_5, gbc_label_5); JLabel label_3 = new
JLabel(); label_3.setText( " 模板:" ); GridBagConstraints gbc_label_3 = new
GridBagConstraints(); gbc_label_3.anchor = GridBagConstraints.EAST; gbc_label_3.fill = GridBagConstraints.VERTICAL; gbc_label_3.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_label_3.gridx = 0 ; gbc_label_3.gridy = 4 ; contentPane.add(label_3, gbc_label_3); templetField = new
JTextField(); templetField.setText( "catrestaurant###" ); GridBagConstraints gbc_templetField = new
GridBagConstraints(); gbc_templetField.anchor = GridBagConstraints.SOUTH; gbc_templetField.fill = GridBagConstraints.HORIZONTAL; gbc_templetField.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_templetField.gridwidth = 3 ; gbc_templetField.gridx = 1 ; gbc_templetField.gridy = 4 ; contentPane.add(templetField, gbc_templetField); JLabel label_4 = new
JLabel(); label_4.setText( "开始于:" ); GridBagConstraints gbc_label_4 = new
GridBagConstraints(); gbc_label_4.fill = GridBagConstraints.VERTICAL; gbc_label_4.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_label_4.gridx = 0 ; gbc_label_4.gridy = 5 ; contentPane.add(label_4, gbc_label_4); startSpinner = new
JSpinner(); GridBagConstraints gbc_startSpinner = new
GridBagConstraints(); gbc_startSpinner.fill = GridBagConstraints.HORIZONTAL; gbc_startSpinner.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_startSpinner.gridx = 1 ; gbc_startSpinner.gridy = 5 ; contentPane.add(startSpinner, gbc_startSpinner); JLabel label_2 = new
JLabel(); label_2.setText( " 扩展名:" ); GridBagConstraints gbc_label_2 = new
GridBagConstraints(); gbc_label_2.fill = GridBagConstraints.HORIZONTAL; gbc_label_2.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_label_2.gridx = 2 ; gbc_label_2.gridy = 5 ; contentPane.add(label_2, gbc_label_2); JButton startButton = new
JButton(); startButton.addActionListener( new
ActionListener() { public
void actionPerformed(ActionEvent e) { do_startButton_actionPerformed(e); } }); extNameField = new
JTextField(); extNameField.setText( "jpg" ); GridBagConstraints gbc_extNameField = new
GridBagConstraints(); gbc_extNameField.fill = GridBagConstraints.HORIZONTAL; gbc_extNameField.insets = new
Insets( 0 , 0 , 5 , 5 ); gbc_extNameField.gridx = 3 ; gbc_extNameField.gridy = 5 ; contentPane.add(extNameField, gbc_extNameField); startButton.setText( "开始" ); GridBagConstraints gbc_startButton = new
GridBagConstraints(); gbc_startButton.anchor = GridBagConstraints.NORTH; gbc_startButton.insets = new
Insets( 0 , 0 , 5 , 0 ); gbc_startButton.gridx = 4 ; gbc_startButton.gridy = 5 ; contentPane.add(startButton, gbc_startButton); JSeparator separator_2 = new
JSeparator(); GridBagConstraints gbc_separator_2 = new
GridBagConstraints(); gbc_separator_2.anchor = GridBagConstraints.NORTH; gbc_separator_2.fill = GridBagConstraints.HORIZONTAL; gbc_separator_2.insets = new
Insets( 0 , 0 , 5 , 0 ); gbc_separator_2.gridwidth = 5 ; gbc_separator_2.gridx = 0 ; gbc_separator_2.gridy = 6 ; contentPane.add(separator_2, gbc_separator_2); JScrollPane scrollPane = new
JScrollPane(); GridBagConstraints gbc_scrollPane = new
GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; gbc_scrollPane.gridwidth = 5 ; gbc_scrollPane.gridx = 0 ; gbc_scrollPane.gridy = 7 ; contentPane.add(scrollPane, gbc_scrollPane); table = new
JTable(); table.setModel( new
DefaultTableModel( new
Object[][] {}, new
String[] { "旧文件名" , "新文件名"
})); scrollPane.setViewportView(table); } /** * 浏览按钮的事件处理方法 * * @param e */ protected
void do_button_actionPerformed(ActionEvent e) { JFileChooser chooser = new
JFileChooser(); // 创建文件选择器 // 设置只选择文件夹 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int
option = chooser.showOpenDialog( this ); // 显示打开对话框 if
(option == JFileChooser.APPROVE_OPTION) { dir = chooser.getSelectedFile(); // 获取选择的文件夹 } else
{ dir = null ; } forderField.setText(dir + "" ); // 显示文件夹信息 } /** * 开始按钮的事件处理方法 * * @param e */ protected
void do_startButton_actionPerformed(ActionEvent e) { String templet = templetField.getText(); // 获取模板字符串 if
(templet.isEmpty()) { JOptionPane.showMessageDialog( this , "请确定重命名模板" , "信息对话框" , JOptionPane.WARNING_MESSAGE); return ; } // 获取表格数据模型 DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setRowCount( 0 ); // 清除表格数据 int
bi = (Integer) startSpinner.getValue(); // 获取起始编号 int
index = templet.indexOf( "#" ); // 获取第一个“#”的索引 String code = templet.substring(index); // 获取模板中数字占位字符串 // 把模板中数字占位字符串替换为指定格式 templet = templet.replace(code, "%0"
+ code.length() + "d" ); String extName = extNameField.getText().toLowerCase(); if
(extName.indexOf( "." ) == - 1 ) extName = "."
+ extName; // 获取文件中文件列表数组 File[] files = dir.listFiles( new
ExtNameFileFilter(extName)); for
(File file : files) { // 变量文件数组 // 格式化每个文件名称 String name = String.format(templet, bi++) + extName; // 把文件的旧名称与新名称添加到表格的数据模型 model.addRow( new
String[] { file.getName(), name }); File parentFile = file.getParentFile(); // 获取文件所在文件夹对象 File newFile = new
File(parentFile, name); file.renameTo(newFile); // 文件重命名 } } } |
效果如图:
原文:http://www.cnblogs.com/cysolo/p/3572390.html