Skip to content

Commit 6879e38

Browse files
committed
Improve single file conversion
1 parent e485025 commit 6879e38

1 file changed

Lines changed: 65 additions & 42 deletions

File tree

TraverseFolderEncoded/Program.cs

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal abstract class Program
99
static async Task Main(string[] args)
1010
{
1111
Console.WriteLine("Welcome to use TraverseFolderEncoded!");
12-
if (args.Length != 2)
12+
if (args.Length != 2) // 判断启动参数是否正确
1313
{
1414
Console.WriteLine("Incorrect startup parameters.");
1515
Console.WriteLine("Example: TraverseFolderEncoded.exe [Encoded(utf-8/gbk/gb2312/65001...)] [File/Folder Path]");
@@ -18,54 +18,77 @@ static async Task Main(string[] args)
1818
}
1919
else
2020
{
21-
string folderPath = args[1]; // 指定文件夹路径
22-
string targetEncodingStringName = args[0]; // 指定目标编码名称
23-
int.TryParse(args[0], out var targetEncodingIntName);
24-
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
25-
// 获取文件夹中的所有文件
26-
string[] files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
27-
28-
Encoding targetEncoding;
29-
if (targetEncodingIntName != 0)
30-
targetEncoding = Encoding.GetEncoding(targetEncodingIntName);
31-
else
21+
string path = args[1]; // 指定路径
22+
string targetEncodingStringName = args[0]; // 指定目标编码名称
23+
int.TryParse(args[0], out var targetEncodingIntName); // 判断是否为数字,如果是则使用数字作为目标编码
24+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // 注册编码
25+
Encoding targetEncoding; // 目标编码
26+
if (targetEncodingIntName != 0) // 如果是数字则使用数字作为目标编码
27+
targetEncoding = Encoding.GetEncoding(targetEncodingIntName);
28+
else // 如果不是数字则使用名称作为目标编码
3229
targetEncoding = Encoding.GetEncoding(targetEncodingStringName);
33-
int totalTicks = files.Length;
34-
var options = new ProgressBarOptions
35-
{
36-
// 设置前景色
37-
ForegroundColor = ConsoleColor.Yellow,
38-
// 设置完成时的前景色
39-
ForegroundColorDone = ConsoleColor.DarkGreen,
40-
// 设置背景色
41-
BackgroundColor = ConsoleColor.DarkGray,
42-
// 设置背景字符
43-
BackgroundCharacter = '\u2593'
44-
};
45-
Stopwatch stopwatch = new Stopwatch();
46-
using (var pbar = new ProgressBar(totalTicks, "Processing", options))
30+
Stopwatch stopwatch = new Stopwatch(); // 计时器
31+
32+
if (File.Exists(path)) // 判断路径是否为文件
4733
{
48-
//double percent = 100.00 / files.Length;
4934
stopwatch.Start();
50-
// 遍历所有文件并修改编码
51-
foreach (string file in files)
35+
// 读取文件内容
36+
byte[] bytes = File.ReadAllBytes(path);
37+
// 将文件内容从原编码转换为目标编码
38+
Encoding sourceEncoding = Encoding.Default; // 自动检测原编码
39+
byte[] targetBytes = Encoding.Convert(sourceEncoding, targetEncoding, bytes);
40+
// 将转换后的内容写入文件
41+
File.WriteAllBytes(path, targetBytes);
42+
Console.WriteLine("File {0} have been converted to {1} encoding, taking time {2}",
43+
path, targetEncoding.EncodingName, stopwatch.Elapsed);
44+
stopwatch.Stop();
45+
}
46+
else if (Directory.Exists(path)) // 判断路径是否为文件夹
47+
{
48+
// 获取文件夹中的所有文件
49+
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
50+
51+
int totalTicks = files.Length;
52+
var options = new ProgressBarOptions
53+
{
54+
// 设置前景色
55+
ForegroundColor = ConsoleColor.Yellow,
56+
// 设置完成时的前景色
57+
ForegroundColorDone = ConsoleColor.DarkGreen,
58+
// 设置背景色
59+
BackgroundColor = ConsoleColor.DarkGray,
60+
// 设置背景字符
61+
BackgroundCharacter = '\u2593'
62+
};
63+
using (var pbar = new ProgressBar(totalTicks, "Processing", options))
5264
{
53-
pbar.Message = $"Start {Array.IndexOf(files, file) + 1} of {files.Length}: {file}";
54-
// 读取文件内容
55-
byte[] bytes = File.ReadAllBytes(file);
56-
// 将文件内容从原编码转换为目标编码
57-
Encoding sourceEncoding = Encoding.Default; // 自动检测原编码
58-
byte[] targetBytes = Encoding.Convert(sourceEncoding, targetEncoding, bytes);
59-
await Task.Delay(1);
60-
// 将转换后的内容写入文件
61-
File.WriteAllBytes(file, targetBytes);
62-
pbar.Tick($"End {Array.IndexOf(files, file) + 1} of {files.Length}: {file}");
65+
//double percent = 100.00 / files.Length;
66+
stopwatch.Start();
67+
// 遍历所有文件并修改编码
68+
foreach (string file in files)
69+
{
70+
pbar.Message = $"Start {Array.IndexOf(files, file) + 1} of {files.Length}: {file}";
71+
// 读取文件内容
72+
byte[] bytes = File.ReadAllBytes(file);
73+
// 将文件内容从原编码转换为目标编码
74+
Encoding sourceEncoding = Encoding.Default; // 自动检测原编码
75+
byte[] targetBytes = Encoding.Convert(sourceEncoding, targetEncoding, bytes);
76+
await Task.Delay(1);
77+
// 将转换后的内容写入文件
78+
File.WriteAllBytes(file, targetBytes);
79+
pbar.Tick($"End {Array.IndexOf(files, file) + 1} of {files.Length}: {file}");
80+
}
81+
stopwatch.Stop();
6382
}
64-
stopwatch.Stop();
83+
84+
Console.WriteLine("All files have been converted to {0} encoding, taking time {1}",
85+
targetEncoding.EncodingName, stopwatch.Elapsed);
86+
}
87+
else // 路径无效
88+
{
89+
Console.WriteLine("The path is invalid.");
6590
}
6691

67-
Console.WriteLine("All files have been converted to {0} encoding, taking time {1}",
68-
targetEncoding.EncodingName, stopwatch.Elapsed);
6992
Console.Write("Press any key to exit...");
7093
Console.ReadKey();
7194
}

0 commit comments

Comments
 (0)