0
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?

More than 1 year has passed since last update.

ファイルを読み込んで空ファイルを作成するバッチの作り方

Posted at

概要

ファイルを読み込んで空ファイルを作成するバッチの作り方です。
テキストファイルより作成したいファイルの名称を読み込み、ファイルを作成するバッチで、
名前や種類の異なるファイルを一度に大量に作成したいときに役立ちます。

必要なもの

①作成したいファイルの名称を記載したテキストファイル
②バッチファイル
③出力先フォルダ

バッチの中身と解説

前提

・バッチが読み込むテキストファイルの名称は「fileList.txt」とする。
・バッチの名前は「createFile.bat」とする。
・ファイル出力先フォルダは「カレントディレクトリ\output」とする。

中身

createFile.bat
@echo off

setlocal enabledelayedexpansion

set n=0
set filePass=%~dp0\output

for /f %%s in (fileList.txt) do (
  set string[n]=%%s
  type nul > !filePass!\!string[n]!
  set /a n=n+1
)

endlocal

解説

大まかにはテキストファイル「fileList.txt」を読み込み、
ループの中で一行ずつデータを読み込んで、ファイルを出力していく処理になっています。
image.png

使い方

例えば今回のバッチの設定ではこんな感じに配置しておく。
image.png

①バッチが読み込むテキストファイル「fileList.txt」に出力したいファイルの名称を記載する。
今回は例として様々な種類のファイルを記載しておく

fileList.txt
sample1.txt
sample2.txt
sample3.csv
sample4.xlsx
sample5.xlsm
sample6.doc

②バッチを実行する。(バッチファイルをダブルクリックorコマンドプロンプトで実行)
③outputフォルダに空ファイルが出力されている。
image.png

0
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
0
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?