1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows10 標準コンパイラでファイル分割してみた

Last updated at Posted at 2025-01-22

以前にVB.netを使用してファイル分割のアプリを作りましたが、
新しい職場はVB.netもインストール禁止という事なので、Windowsに標準で搭載されているVBC.exeを使用してファイル分割アプリを作ってみました。
メモ帳に下記を張り付けて、BinarySplit6.vbでファイル名を保存し、
Windowsフォルダ配下のVBC.exeを使用してコンパイルします。
※私の環境は C:\Windows\Microsoft.NET\Framework64\v4.0.30319\VBC.exe でした。
適当はフォルダに(例としてC:¥work)BinarySplit6.vbを保存し、
コマンドプロンプトから
cd C:¥work
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\VBC.exe BinarySplit6.vb
を実行します。
出来上がったBinarySplit6.exeを使用してファイルを分割します。

コマンドプロンプトからBinarySplit6.exe 分割したいファイル名 分割サイズ
の指定でファイルが分割されます。(標準の分割サイズは1MBです)

BinarySplit6.vb
'コマンドラインでファイル分割
Imports System
Imports System.IO
Imports System.Text


Class BinarySplit

	' コマンドライン引数の読込み配列 CmdArgsで読込み
	Shared Sub Main(ByVal CmdArgs() As String)

		Dim FileName as String	' ファイル名変数宣言
		FileName = Dir(System.Reflection.Assembly.GetExecutingAssembly().Location)	' 実行ファイル名を取得
		' MsgBox(FileName)

		' 実行ファイル名変更チェック
		If FileName <> "BinarySplit6.exe" Then	' 実行ファイル名がコンパイル時と異なるのであれば
			MsgBox("ファイルが改ざんされた可能性があります。")
			Exit Sub	' プログラム終了
		End If

		' 引数が入力されていない場合
		If (CmdArgs.Length = 0) Then	' 引数の配列の長さが0ならば
			MsgBox("引数がありません")
			Exit Sub	' プログラム終了
		End If


		Dim s As String	' 引数取得の変数宣言

		Dim cnt As Integer = 0	' カウンタ変数宣言

		Dim f_name As String	' 第1引数(ファイル名)代入用変数宣言
		f_name = ""

		Dim size As Integer = 1	' 第2引数(ファイルサイズ)代入用変数宣言(デフォルト1MB)

		For Each s In CmdArgs	' 各引数をメッセージボックスで表示
			Select Case cnt
				Case 0
 					MsgBox("分割するファイル名:" + s )
					f_name = s

					If System.IO.File.Exists(f_name) Then
					Else
						MsgBox("'" + f_name + "'は存在しません。")
						Exit Sub	' プログラム終了
					End If
				Case 1
					MsgBox("分割するサイズ" + s + "MB" )
				'
				
					Try
						size = Integer.Parse(s)

					Catch ex As Exception

						MsgBox("数字以外の文字が含まれています")
						Exit Sub
 
					End Try				

				Case Else
					MsgBox("引数に誤りがあります")
					Exit Sub	' プログラム終了
			End Select
		
			cnt = cnt + 1	' カウントアップ
			' Msgbox(s)
		Next

		' アウトプットファイル用パス名を削除したファイル名(拡張子付き)を取得
		Dim out_fileName As String = IO.Path.GetFileName(f_name)

		' アウトプット用ファイル名の設定
		Dim output_f_name As String
		output_f_name = System.IO.Path.GetFileNameWithoutExtension(f_name)	' 引数のファイル名から拡張子を除いた名前を設定

		' 結合用バッチファイル作成。ストリームライター宣言
		Dim writer As StreamWriter = New StreamWriter(output_f_name + ".bat", True, System.Text.Encoding.Default)
		' 結合用コマンドcopy /Bの書込み
		writer.Write("copy /B ")

		' バイナリ・ファイルの読み込み
		Dim src() As Byte = File.ReadAllBytes(f_name)

		' 分割サイズ指定
		Dim FILESIZE As Integer = 1024 * 1024 * size ' 分割サイズデフォルト1MB

		' カウンタ初期化
		Dim num As Integer = 0

		' 変数remainに分割元ファイルの長さを登録
		Dim remain As Integer = src.Length

		' 変数remainが0以上の間(ファイルが0バイトよりも残っている場合)ループ
		While remain > 0

			' 作成する分割ファイルの実際のサイズ
			' FILESIZE と remeinどちらが大きいか(最終ループの為)
			' 変数length = 切り取るバイト数となる
			Dim length As Integer = Math.Min(FILESIZE, remain)

			' 分割ファイルへ書き出すbyte配列の作成
			Dim dest(length - 1) As Byte

			' リファレンス
			' Array.Copyメソッドでコピー
			' Array.Copy(a, i, b, j, n)
			' 配列aの(i+1)番目の要素を配列bの(j+1)番目の位置へと、順にn個をコピーする。

			' 配列src(元ファイル)のnum*FILESIZE+1(カウンタ×分割サイズ+1)【切り出し開始】か
			' 配列dest(分割ファイル書き出し配列)の0+1目の位置にlength個コピーする
			Array.Copy(src, num * FILESIZE, dest, 0, length)

			' 出力ファイル名指定(例out0001.bin、out0002.bin、……)
			Dim name As String = String.Format(output_f_name + "_{0:D4}.bin", num + 1)

			' 結合用バッチファイル末尾ファイル以外ならば"ファイル名" + を書込み
			If FILESIZE < remain Then
				writer.Write(Chr(34) + name + Chr(34) + " + ")
			else
			' 結合用バッチファイル末尾ファイルならば"ファイル名"を書込み
				writer.Write(Chr(34) + name + Chr(34))
			End If


			' byte配列のファイルへの書き込み
			File.WriteAllBytes(name, dest)

			' カウントアップ
			num += 1
			' 変数remainから出力したファイルサイズを引く
			remain -= FILESIZE
		End While
	

		' 結合用バッチファイル書込み "ファイル名"の書込み
		writer.Write(" " + Chr(34) + out_fileName + Chr(34))
		' 結合用バッチファイルクローズ
		writer.Close()

	End Sub
End Class

試しに使ってみて頂いて感想などをお聞かせいただければ幸いです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?