概要
今日はサブディレクトリのすべてのファイルをテキストファイルに保存するコマンドを作成します。
コマンドのオプションによってファイルのハッシュも表示するようにします。
ファイルが以前のものと変更があるかないか確認のためです。
ソースコード
「VSを起動」→「新規プロジェクト作成」→「コンソール」検索→「コンソールAPP(.NET Framework)」→「filelist」プロジェクト名のソリューションを作成します。
その後、Program.csに下記のソースを貼り付けます。
using System;
using System.IO;
using System.Security.Cryptography;
namespace filelist
{
class Program
{
static readonly HashAlgorithm hashProvider = new SHA1CryptoServiceProvider();
/// <summary>
/// Returns the hash string for the file.
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static string ComputeFileHash(string filePath)
{
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var bs = hashProvider.ComputeHash(fs);
return BitConverter.ToString(bs).ToLower().Replace("-", "");
}
static void Main(string[] args)
{
string path1 = @".\";
try
{
var filename = Directory.EnumerateFiles(path1, "*.*", System.IO.SearchOption.AllDirectories);
StreamWriter sw = new StreamWriter(@".\file" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");
foreach (string f in filename)
{
if (args.Length == 1 && args[0].ToString().ToUpper() == "-H")
{
Console.WriteLine(ComputeFileHash(f) + " " + f.Substring(2,f.Length - 2));
sw.WriteLine(ComputeFileHash(f) + " " + f.Substring(2, f.Length - 2));
}
else
{
Console.WriteLine(f.Substring(2, f.Length - 2));
sw.WriteLine(f.Substring(2, f.Length - 2));
}
}
sw.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
コマンド実行
コマンド実行には引数「-h」を指定して実行と引数を指定なしで実行します。
①引数「-h」を指定して実行
C:\repos\filelist>filelist.exe -h
da39a3ee5e6b4b0d3255bfef95601890afd80709 file20211116001523.txt
51056beb9e77695439244e703aed9b8dc8a5fdc0 filelist.exe
efd94a88cb73edbe4193f220ab25f7cd957fbc5a filelist.sln
c6bdab9d66a8a22010f7ecffdee3ca88bf5adb67 .vs\filelist\v16\.suo
2711de49785aba673df043f543686685e3b53e73 filelist\App.config
b282edf79d5e38c2e046b47c65f71161feb79f04 filelist\filelist.csproj
ee51f6f822dd4b593345f3fc7e6e1f62db7fdcf1 filelist\Program.cs
620106eba2a07b032917b5f2576d48ffb2d98503 filelist\bin\Debug\file.txt
51056beb9e77695439244e703aed9b8dc8a5fdc0 filelist\bin\Debug\filelist.exe
2711de49785aba673df043f543686685e3b53e73 filelist\bin\Debug\filelist.exe.config
d5317f95b2778bdfd91c90fdf477a7cf6dfad30f filelist\bin\Debug\filelist.pdb
7e086fd5c5e77213f7b2b89cb8e1f82bdd6e2a8c filelist\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
6d5a1db8b80683c8e82601af7b0679bb8338eb7e filelist\obj\Debug\filelist.csproj.CoreCompileInputs.cache
28afd52d819d8f9d9ab07c96580d659a582caa8e filelist\obj\Debug\filelist.csproj.FileListAbsolute.txt
51056beb9e77695439244e703aed9b8dc8a5fdc0 filelist\obj\Debug\filelist.exe
d5317f95b2778bdfd91c90fdf477a7cf6dfad30f filelist\obj\Debug\filelist.pdb
41525e3927742a96f9f2edca06ecc0481a28f553 filelist\Properties\AssemblyInfo.cs
②コマンドを実行(引数を指定なし)
C:\repos\filelist>filelist.exe
file20211116001523.txt
file20211116001558.txt
filelist.exe
filelist.sln
.vs\filelist\v16\.suo
filelist\App.config
filelist\filelist.csproj
filelist\Program.cs
filelist\bin\Debug\file.txt
filelist\bin\Debug\filelist.exe
filelist\bin\Debug\filelist.exe.config
filelist\bin\Debug\filelist.pdb
filelist\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
filelist\obj\Debug\filelist.csproj.CoreCompileInputs.cache
filelist\obj\Debug\filelist.csproj.FileListAbsolute.txt
filelist\obj\Debug\filelist.exe
filelist\obj\Debug\filelist.pdb
filelist\Properties\AssemblyInfo.cs
参照サイト
・[C#] ファイルハッシュを求める (MD5, SHA-1, SHA-256)
https://mseeeen.msen.jp/compute-hash-string-of-files/
・C# ファイルの一覧を取得するサンプル
https://itsakura.com/csharp-filelist
終わりに
サブディレクトリの全ファイルと各ファイルのハッシュも表示できるようにコマンドを作成しました。
一応実行結果もテキストファイルに保存も追加しました。
次回は2つのディレクトリを比較し、差分、新規、削除など比較できるようにオプションを追加してみます。