Skip to content

Commit

Permalink
added command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
setsumi committed Apr 4, 2024
1 parent fe07dfd commit 43c8c43
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 54 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin
obj
.vs
*.suo
bin
obj
.vs
*.suo
*.user
142 changes: 94 additions & 48 deletions torrentwiper/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,60 @@ public Form1()
{
InitializeComponent();

// display version
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
this.Text += " " + fvi.FileVersion;

// command line
string[] arg = Environment.GetCommandLineArgs();
int argn = arg.Length;
string err_msg = "";
for (int i = 1; i < argn; i++)
{
var ai = arg[i];
if (ai.EndsWith(".torrent", StringComparison.OrdinalIgnoreCase))
_torrentName = ai;
else
_torrentFolder = ai;
}
if (!string.IsNullOrEmpty(_torrentName))
{
if (File.Exists(_torrentName))
OpenTorrent(_torrentName);
else
err_msg += $"Torrent file doesn't exist:\n{_torrentName}\n\n";
}
if (!string.IsNullOrEmpty(_torrentFolder))
{
if (Directory.Exists(_torrentFolder))
{
textBox2.Text = _torrentFolder;
if (_fileNum > 0) // .torrent is loaded
OpenFolder(_torrentFolder);
}
else
err_msg += $"Folder doesn't exist:\n{_torrentFolder}\n\n";
}
if (!string.IsNullOrEmpty(err_msg))
MessageBox.Show(err_msg, "torrentwiper", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

private void btnOpenTorrent_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.FileName = _torrentName;
dialog.Title = "Open Torrent File";
dialog.Filter = "Torrent files (*.torrent)|*.torrent";
if (dialog.ShowDialog() != DialogResult.OK) return;
if (dialog.ShowDialog() == DialogResult.OK)
{
OpenTorrent(dialog.FileName);
}
}

_torrentName = dialog.FileName;
private void OpenTorrent(string torrent_name)
{
_torrentName = torrent_name;
_torrentSize = 0;
_fileList.Clear();
_dirList.Clear();
Expand Down Expand Up @@ -93,64 +134,69 @@ private void btnOpenFolder_Click(object sender, EventArgs e)
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
_torrentFolder = fbd.SelectedPath;
_deleteSize = 0;
OpenFolder(fbd.SelectedPath);
}
}

on_torrent_ui_reset(_torrentFolder);
this.Refresh();
private void OpenFolder(string folder_name)
{
_torrentFolder = folder_name;
_deleteSize = 0;

on_torrent_ui_reset(_torrentFolder);
this.Refresh();

int totalFiles = 0, totalFolders = 0;
Int64 totalSize = 0;
int totalFiles = 0, totalFolders = 0;
Int64 totalSize = 0;

// find junk files
string[] filenames = Directory.GetFiles(_torrentFolder, "*", SearchOption.AllDirectories);
foreach (string file in filenames)
// find junk files
string[] filenames = Directory.GetFiles(_torrentFolder, "*", SearchOption.AllDirectories);
foreach (string file in filenames)
{
totalFiles++;
var fi = new FileInfo(file);
totalSize += fi.Length;
string tf = file.Substring(_torrentFolder.Length);
try
{
totalFiles++;
var fi = new FileInfo(file);
totalSize += fi.Length;
string tf = file.Substring(_torrentFolder.Length);
try
{
Int64 dummy = _fileList[tf];
}
catch (KeyNotFoundException)
{
add_file(tf);
_deleteSize += fi.Length;
}
Int64 dummy = _fileList[tf];
}
catch (KeyNotFoundException)
{
add_file(tf);
_deleteSize += fi.Length;
}
}

// find junk folders
var dirs = new Dictionary<string, Int64>();
string[] directories = Directory.GetDirectories(_torrentFolder, "*", SearchOption.AllDirectories);
foreach (string d in directories)
// find junk folders
var dirs = new Dictionary<string, Int64>();
string[] directories = Directory.GetDirectories(_torrentFolder, "*", SearchOption.AllDirectories);
foreach (string d in directories)
{
totalFolders++;
string dir = d.Substring(_torrentFolder.Length);
if (dir != "\\" && !is_subdir(dir))
{
totalFolders++;
string dir = d.Substring(_torrentFolder.Length);
if (dir != "\\" && !is_subdir(dir))
try
{
try
{
dirs.Add(dir, 0);
}
catch (ArgumentException) { }
dirs.Add(dir, 0);
}
catch (ArgumentException) { }
}
foreach (string d in dirs.Keys)
{
listBoxDirs.Items.Add(d);
}
}
foreach (string d in dirs.Keys)
{
listBoxDirs.Items.Add(d);
}

textBox3.Text = "Junk found: " + listBoxFiles.Items.Count + " files (" + FormatSize(_deleteSize) +
"), " + listBoxDirs.Items.Count + " folders | Total folder stats: " + totalFiles +
" files (" + FormatSize(totalSize) + "), " + totalFolders + " folders.";
textBox3.Text = "Junk found: " + listBoxFiles.Items.Count + " files (" + FormatSize(_deleteSize) +
"), " + listBoxDirs.Items.Count + " folders | Total folder stats: " + totalFiles +
" files (" + FormatSize(totalSize) + "), " + totalFolders + " folders.";

if (listBoxFiles.Items.Count == 0 && listBoxDirs.Items.Count == 0)
{
label1.Text = "Folder is clean";
label1.BackColor = Color.LightGreen;
}
if (listBoxFiles.Items.Count == 0 && listBoxDirs.Items.Count == 0)
{
label1.Text = "Folder is clean";
label1.BackColor = Color.LightGreen;
}
}

Expand Down
4 changes: 2 additions & 2 deletions torrentwiper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.2.0")]
[assembly: AssemblyFileVersion("1.1.2.0")]
[assembly: AssemblyVersion("1.1.3.0")]
[assembly: AssemblyFileVersion("1.1.3.0")]

0 comments on commit 43c8c43

Please sign in to comment.