Skip to content

Dev/sshaw/code cleanup #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions examples/BatchSet/BatchSet.cs → examples/BatchSet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
using System.Collections.Generic;
using TagLib;

public class BatchSet
namespace BatchSet;

public class Program
{
private enum Mode {
Tag, Value, File
enum Mode {
Tag,
Value,
File
}

public static void Main(string [] args)
{
if(args.Length < 3) {
Console.Error.WriteLine ("USAGE: BatchSet.exe -tag value [-tag2 value ...] File1 [File2 ...]");
return;
}

Mode mode = Mode.Tag;
List<string> files = new List<string> ();
Dictionary<string,string> tags = new Dictionary<string,string> ();
var files = new List<string> ();
var tags = new Dictionary<string,string> ();

string tag = null;

foreach (string str in args) {
if (mode == Mode.Tag) {
if (str [0] == '-') {
Expand All @@ -30,30 +34,30 @@ public static void Main(string [] args)
tag = str.Substring (1);
mode = Mode.Value;
}

continue;
}
mode = Mode.File;
}

if (mode == Mode.Value) {
if (!tags.ContainsKey (tag))
tags.Add (tag, str);
mode = Mode.Tag;
continue;
}

if (mode == Mode.File)
files.Add (str);
}

foreach (string filename in files) {
TagLib.File file = TagLib.File.Create (filename);
using var file = TagLib.File.Create (filename);
if (file == null)
continue;
Console.WriteLine ("Updating Tags For: " + filename);

Console.WriteLine ($"Updating Tags For: {filename}");

foreach (string key in tags.Keys) {
string value = tags [key];
try {
Expand All @@ -63,19 +67,15 @@ public static void Main(string [] args)
if (number == 1) {
file.RemoveTags (TagTypes.Id3v2);
} else {
TagLib.Id3v2.Tag v2 =
file.GetTag (TagTypes.Id3v2, true)
as TagLib.Id3v2.Tag;

if (v2 != null)
if (file.GetTag (TagTypes.Id3v2, true) is TagLib.Id3v2.Tag v2)
v2.Version = number;
}
break;
case "album":
file.Tag.Album = value;
break;
case "artists":
file.Tag.AlbumArtists = value.Split (new char [] {';'});
file.Tag.AlbumArtists = value.Split ([';']);
break;
case "comment":
file.Tag.Comment = value;
Expand All @@ -84,7 +84,7 @@ public static void Main(string [] args)
file.Tag.Lyrics = value;
break;
case "composers":
file.Tag.Composers = value.Split (new char [] {';'});
file.Tag.Composers = value.Split ([';']);
break;
case "disc":
file.Tag.Disc = uint.Parse (value);
Expand All @@ -93,10 +93,10 @@ public static void Main(string [] args)
file.Tag.DiscCount = uint.Parse (value);
break;
case "genres":
file.Tag.Genres = value.Split (new char [] {';'});
file.Tag.Genres = value.Split ([';']);
break;
case "performers":
file.Tag.Performers = value.Split (new char [] {';'});
file.Tag.Performers = value.Split ([';']);
break;
case "title":
file.Tag.Title = value;
Expand All @@ -111,20 +111,20 @@ public static void Main(string [] args)
file.Tag.Year = uint.Parse (value);
break;
case "pictures":
List<Picture> pics = new List<Picture> ();
var pics = new List<Picture> ();
if (!string.IsNullOrEmpty (value))
foreach (string path in value.Split (new char [] {';'})) {
foreach (string path in value.Split ([';'])) {
pics.Add (new Picture (path));
}
file.Tag.Pictures = pics.ToArray ();
break;
}
} catch (Exception e) {
Console.WriteLine ("Error setting tag " + key + ":");
Console.WriteLine ($"Error setting tag {key}:");
Console.WriteLine (e);
}
}

file.Save();
}
}
Expand Down
Loading
Loading