1. 前言
最近老板让师弟写一个程序把制定目录下的tif 文件全部重命名为 1, 2, 3 的形式。 觉得好简单, 于是动手写了一下。
基本思路就是遍历文件夹下的文件名称, 然后通过调用 rename 方法进行名字修改
2. 实现
2.1 C++
主要借助 _findfirst, _findnext, _findclose 进行操作
http://www.cnblogs.com/surgewong/p/3351577.html
http://blog.csdn.net/tianxiawuzhei/article/details/43052347
#include <windows.h>
#include <iostream>
#include <string>
#include <io.h>
using namespace std;
int main()
{
struct _finddata_t fileinfo;
long hFile;
if ((hFile = _findfirst("*.png", &fileinfo)) == -1)
return -1;
else {
int count = 0;
do {
/*Process File*/
cout << fileinfo.name << endl;
string newFile = to_string(++count) + ".png";
rename(fileinfo.name, newFile.c_str());
cout << "==============>" << newFile << endl;
} while (_findnext(hFile, &fileinfo) == 0);
}
_findclose(hFile);
return 0;
}
2.2 VB.net
- 主要借助 dir 进行文件查找, 遍历 http://blog.csdn.net/zhyh1435589631/article/details/51363651
- 支持拖拽
http://blog.csdn.net/zhyh1435589631/article/details/51363767 - 使用RenameFile 改名
- 效果
Imports Microsoft.VisualBasic.FileIO.FileSystem
Public Class Form1
Dim SettingPath As String
Private Sub SELECTED_SelectedIndexChanged(sender As Object, e As EventArgs) Handles SELECTED.SelectedIndexChanged
LoadSettings()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SELECTED.SelectedIndex = 1
TextBox_PATH.Text = My.Application.Info.DirectoryPath
SettingPath = My.Application.Info.DirectoryPath
Timer1.Interval = 500
Timer1.Enabled = True
End Sub
Private Sub ChangePath_Click(sender As Object, e As EventArgs) Handles ChangePath.Click
FolderBrowserDialog1.ShowDialog()
TextBox_PATH.Text = FolderBrowserDialog1.SelectedPath
SettingPath = FolderBrowserDialog1.SelectedPath
End Sub
Private Sub LoadSettings()
Dim filter As String = SELECTED.SelectedItem
filter = SettingPath + "\\" + filter
TextBox_RES.Text = showAllFiles(filter)
End Sub
Private Function showAllFiles(PathPattern As String) As String
Dim MyName As String = Dir(PathPattern, vbNormal) ' 找寻第一项。
Dim res As String = ""
Do While MyName <> "" ' 开始循环。
res += MyName + vbCrLf
MyName = Dir()
Loop
Return res
End Function
Private Sub RENAME_Click(sender As Object, e As EventArgs) Handles RENAME.Click
Dim filter As String = SELECTED.SelectedItem
Dim filter_bak As String = filter.Substring(1)
If filter_bak = ".*" Then
Return
End If
filter = SettingPath + "\\" + filter
Dim MyName As String = Dir(filter, vbNormal) ' 找寻第一项。
Dim res As String = ""
Dim count As Integer = 0
Do While MyName <> "" ' 开始循环。
' rename
count = count + 1
Dim newFile As String = Str(count) + filter_bak
Dim File As String = SettingPath + "\" + MyName
Try
RenameFile(File, newFile)
Catch
End Try
MyName = Dir()
Loop
LoadSettings()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
LoadSettings()
End Sub
Private Sub TextBox_PATH_DragEnter(sender As Object, e As DragEventArgs) Handles TextBox_PATH.DragEnter
e.Effect = DragDropEffects.All
End Sub
Private Sub TextBox_PATH_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox_PATH.DragDrop
Dim MyFiles() As String
MyFiles = e.Data.GetData(DataFormats.FileDrop)
TextBox_PATH.Text = MyFiles(0)
SettingPath = MyFiles(0)
End Sub
Private Sub TextBox_PATH_TextChanged(sender As Object, e As EventArgs) Handles TextBox_PATH.TextChanged
SettingPath = TextBox_PATH.Text
End Sub
End Class