Skip to content

Commit be8122a

Browse files
committed
Touch up BatchSet program
1 parent ddd2849 commit be8122a

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

examples/BatchSet/BatchSet.cs renamed to examples/BatchSet/Program.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
using System.Collections.Generic;
33
using TagLib;
44

5-
public class BatchSet
5+
namespace BatchSet;
6+
7+
public class Program
68
{
7-
private enum Mode {
8-
Tag, Value, File
9+
enum Mode {
10+
Tag,
11+
Value,
12+
File
913
}
10-
14+
1115
public static void Main(string [] args)
1216
{
1317
if(args.Length < 3) {
1418
Console.Error.WriteLine ("USAGE: BatchSet.exe -tag value [-tag2 value ...] File1 [File2 ...]");
1519
return;
1620
}
17-
21+
1822
Mode mode = Mode.Tag;
19-
List<string> files = new List<string> ();
20-
Dictionary<string,string> tags = new Dictionary<string,string> ();
21-
23+
var files = new List<string> ();
24+
var tags = new Dictionary<string,string> ();
25+
2226
string tag = null;
23-
27+
2428
foreach (string str in args) {
2529
if (mode == Mode.Tag) {
2630
if (str [0] == '-') {
@@ -30,30 +34,30 @@ public static void Main(string [] args)
3034
tag = str.Substring (1);
3135
mode = Mode.Value;
3236
}
33-
37+
3438
continue;
3539
}
3640
mode = Mode.File;
3741
}
38-
42+
3943
if (mode == Mode.Value) {
4044
if (!tags.ContainsKey (tag))
4145
tags.Add (tag, str);
4246
mode = Mode.Tag;
4347
continue;
4448
}
45-
49+
4650
if (mode == Mode.File)
4751
files.Add (str);
4852
}
49-
53+
5054
foreach (string filename in files) {
51-
TagLib.File file = TagLib.File.Create (filename);
55+
using var file = TagLib.File.Create (filename);
5256
if (file == null)
5357
continue;
54-
55-
Console.WriteLine ("Updating Tags For: " + filename);
56-
58+
59+
Console.WriteLine ($"Updating Tags For: {filename}");
60+
5761
foreach (string key in tags.Keys) {
5862
string value = tags [key];
5963
try {
@@ -63,19 +67,15 @@ public static void Main(string [] args)
6367
if (number == 1) {
6468
file.RemoveTags (TagTypes.Id3v2);
6569
} else {
66-
TagLib.Id3v2.Tag v2 =
67-
file.GetTag (TagTypes.Id3v2, true)
68-
as TagLib.Id3v2.Tag;
69-
70-
if (v2 != null)
70+
if (file.GetTag (TagTypes.Id3v2, true) is TagLib.Id3v2.Tag v2)
7171
v2.Version = number;
7272
}
7373
break;
7474
case "album":
7575
file.Tag.Album = value;
7676
break;
7777
case "artists":
78-
file.Tag.AlbumArtists = value.Split (new char [] {';'});
78+
file.Tag.AlbumArtists = value.Split ([';']);
7979
break;
8080
case "comment":
8181
file.Tag.Comment = value;
@@ -84,7 +84,7 @@ public static void Main(string [] args)
8484
file.Tag.Lyrics = value;
8585
break;
8686
case "composers":
87-
file.Tag.Composers = value.Split (new char [] {';'});
87+
file.Tag.Composers = value.Split ([';']);
8888
break;
8989
case "disc":
9090
file.Tag.Disc = uint.Parse (value);
@@ -93,10 +93,10 @@ public static void Main(string [] args)
9393
file.Tag.DiscCount = uint.Parse (value);
9494
break;
9595
case "genres":
96-
file.Tag.Genres = value.Split (new char [] {';'});
96+
file.Tag.Genres = value.Split ([';']);
9797
break;
9898
case "performers":
99-
file.Tag.Performers = value.Split (new char [] {';'});
99+
file.Tag.Performers = value.Split ([';']);
100100
break;
101101
case "title":
102102
file.Tag.Title = value;
@@ -111,20 +111,20 @@ public static void Main(string [] args)
111111
file.Tag.Year = uint.Parse (value);
112112
break;
113113
case "pictures":
114-
List<Picture> pics = new List<Picture> ();
114+
var pics = new List<Picture> ();
115115
if (!string.IsNullOrEmpty (value))
116-
foreach (string path in value.Split (new char [] {';'})) {
116+
foreach (string path in value.Split ([';'])) {
117117
pics.Add (new Picture (path));
118118
}
119119
file.Tag.Pictures = pics.ToArray ();
120120
break;
121121
}
122122
} catch (Exception e) {
123-
Console.WriteLine ("Error setting tag " + key + ":");
123+
Console.WriteLine ($"Error setting tag {key}:");
124124
Console.WriteLine (e);
125125
}
126126
}
127-
127+
128128
file.Save();
129129
}
130130
}

0 commit comments

Comments
 (0)