<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>for &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/for/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Mon, 03 Jun 2024 13:50:50 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sugublog.org/wp-content/uploads/2021/07/icon_20210703-min-150x150.png</url>
	<title>for &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【シェルスクリプト】for文で回数の指定方法！エラー対処法あり</title>
		<link>https://sugublog.org/shellscript-for-number-of-times/</link>
					<comments>https://sugublog.org/shellscript-for-number-of-times/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Mon, 20 Nov 2023 12:40:59 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[回数]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5924</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/html-007-5-1024x513.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトの繰り返しでforを使用する時に、ループする回数の指定方法をまとめます。 そういった疑問に答えます。 シェルスクリプトのfor文で回数の指定方法 シェルスクリプトのfor文の基本構文と使い方 シェルスクリ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/html-007-5-1024x513.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong>シェルスクリプト</strong>の繰り返しで<strong>for</strong>を使用する時に、<strong><span class="swl-inline-color has-swl-deep-01-color">ループする回数の指定方法</span></strong>をまとめます。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="blue"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">シェルスクリプトのforで繰り返し回数の指定方法を知りたい犬</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>シェルスクリプトでfor文を使いたいけど、回数の指定方法がわかりません。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p>そういった疑問に答えます。</p>


<div class="p-blogParts post_content" data-partsID="5030">
<p>windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。</p>
</div>


<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">シェルスクリプトのfor文で回数の指定方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="676" src="https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1024x676.jpg" alt="回数指定方法" class="wp-image-4185" srcset="https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1024x676.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-300x198.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-768x507.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトのfor文の基本構文と使い方</h3>



<p><strong>シェルスクリプト</strong>の<strong>for</strong>文を使った<strong>繰り返し</strong>の基礎は以下になります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in 変数に入れるリストなど
 do
  繰り返す処理
 done</code></pre></div>



<h3 class="wp-block-heading">for文を使ったカウントアップの方法</h3>



<p>for文の<strong><span class="swl-inline-color has-swl-deep-01-color">回数指定</span></strong>の基本は以下です</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in {1..9}
 do
  繰り返す処理
 done</code></pre></div>



<p>｛1..9｝は1から9まで順番に変数に入れていきます。この場合の回数は9になります。</p>



<h3 class="wp-block-heading">forの回数指定の基本実践</h3>



<p>以下のfor文のシェルスクリプトを記述してください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in {1..9}
do
 echo 回数指定で$i回繰り返しています
done</code></pre></div>



<p>＄iで変数iに入った値を展開しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-3-300x232.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-dollar-mean/">【シェルスクリプト】「$」の意味を詳しく解説！実例あり</a>
						<span class="p-blogCard__excerpt">シェルスクリプトで「$」はよく出てきますが、どういう意味なのかわからないと言った事はないでしょうか？ シェルスクリプトで「$」はよく出てきますが、どういう意味な&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>echoで文字列を表示します。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-3-300x188.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-echo/">【シェルスクリプト基礎】echoコマンドの使い方！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトのechoコマンドの使い方で疑問に思っている事などないでしょうか？ 思い通りに文字列を表示できなかったりしないでしょうか？ シェルスクリプトでecho&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>変数や文字列などを結合する場合は、特に何もせず文字などをつなげれば問題ありません。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/09/randam_0915-4-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-screipt-base-combine-strings/">【シェルスクリプト基礎】文字列を結合するには？改行、スペースなども結合</a>
						<span class="p-blogCard__excerpt">シェルスクリプトで文字列を結合したい場合などのやり方をまとめます。 変数と文字列の結合や改行、スペース、空白などを含めた結合、また引数と文字列なども結合してい&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">forの回数指定の基本実践結果</h3>



<p>ではこのシェルスクリプトを実行してみます。今回は「. シェルスクリプト名」で実行します。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-execution/">【シェルスクリプト基礎】コマンドを実行する様々な方法をご紹介！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトを実行する時に困った事はありませんか？ シェルスクリプトのコマンド実行での失敗メッセージ一覧 「実行権限がありません」 「コマンドが見つかりませ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>結果は9回実行されています。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="599" height="165" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9.png" alt="shellscript-forの実行結果" class="wp-image-5928" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9.png 599w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-300x83.png 300w" sizes="(max-width: 599px) 100vw, 599px" /><figcaption class="wp-element-caption">shellscript-for-1-9の実行結果</figcaption></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトで日本語が文字化けする場合</h3>



<p class="is-style-big_icon_caution">シェルスクリプトを実行した結果、文字化けする場合があります。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="580" height="363" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-count-日本語文字化け.png" alt="shellscriptで日本語文字化けした結果" class="wp-image-5936" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-count-日本語文字化け.png 580w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-count-日本語文字化け-300x188.png 300w" sizes="(max-width: 580px) 100vw, 580px" /><figcaption class="wp-element-caption">shellscriptで日本語文字化けした結果</figcaption></figure>
</div>


<p>この場合は文字コードが<span class="swl-bg-color has-swl-deep-01-background-color">「SJIS」</span>などになっています。<span class="swl-bg-color has-swl-deep-03-background-color">「UTF-8」</span>に変更しましょう。</p>



<h3 class="wp-block-heading">seqコマンドを使った回数指定の方法</h3>



<p><strong>「seq」</strong>でも<strong>回数指定可能</strong>です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `seq 1 9`
do
 echo seqで$i回繰り返しています
done</code></pre></div>



<h3 class="wp-block-heading">seqコマンドを使った回数指定の方法実践</h3>



<p>seqを使用して実行してみます。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="481" height="162" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-1.png" alt="shellscript-for-seqの実行結果" class="wp-image-5933" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-1.png 481w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-1-300x101.png 300w" sizes="(max-width: 481px) 100vw, 481px" /><figcaption class="wp-element-caption">shellscript-for-seqの実行結果</figcaption></figure>
</div>


<h3 class="wp-block-heading">seqコマンドを使った時のエラー結果</h3>



<p>seqを使用する時のよくある<span class="swl-bg-color has-swl-deep-01-background-color">エラー</span>を紹介しておきます。</p>



<p>以下のシェルスクリプトを実行して見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in seq 1 9
do
 echo seqで$i回繰り返しています
done</code></pre></div>



<p>「`」が抜けていて「seq」「1」「9」と3つの文字を順番にいれています。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="399" height="58" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-エラー.png" alt="" class="wp-image-5934" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-エラー.png 399w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-seq-1-9-エラー-300x44.png 300w" sizes="(max-width: 399px) 100vw, 399px" /><figcaption class="wp-element-caption">shellscript-for-seq-1-9-エラーの実行結果</figcaption></figure>
</div>


<p>意図しない結果です。<span class="swl-marker mark_yellow">「`」をつけ忘れないようにしましょう！</span></p>



<p class="is-style-icon_pen">「`」はキーボードのShift＋＠で入力できます。</p>



<h3 class="wp-block-heading">回数指定を(())で行う方法</h3>



<p>最後は(())を使用して<strong>回数</strong>を指定します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for ((i=1; i&lt;9; i++))
do
 echo &quot;(())&quot;で$i回繰り返しています
done</code></pre></div>



<p>このシェルスクリプトは((初期値; 条件; 次の値))で指定しており、初期値が1で「i++」がiに1を加算していき、iが9になれば終了となります。</p>



<p>これで回数を8回にすることができます。ではシェルスクリプトを実行します。</p>


<div class="wp-block-image is-style-browser_mac">
<figure class="aligncenter size-full"><img decoding="async" width="450" height="146" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-1.png" alt="shellscript-for" class="wp-image-5946" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-1.png 450w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-1-9-1-300x97.png 300w" sizes="(max-width: 450px) 100vw, 450px" /></figure>
</div>


<p>結果回数は想定通り８回となっています。</p>



<h2 class="wp-block-heading">回数指定を配列で行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="576" src="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-1024x576.jpg" alt="配列" class="wp-image-3959" srcset="https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/programing_0812-11.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">配列の使い方と書き方</h3>



<p>配列の基本的な書き方は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>array=(1 2 3 4 5 6 7 8 9)</code></pre></div>



<p>配列名に「=()」をつけて、その中に配列に入れたい文字などを順に入れていきます。</p>



<h3 class="wp-block-heading">回数指定を配列で行う時の構文</h3>



<p><strong>for</strong>で<strong>回数を指定</strong>するのに<strong><span class="swl-inline-color has-swl-deep-01-color">配列</span></strong>を使用する時の構文</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in ${array[@]}</code></pre></div>



<p class="is-style-icon_book">シェルスクリプトのfor文で配列を使用する方法は、下の記事で詳しく説明しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/10/table_20211010-1-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shellscript-for-array/">【シェルスクリプト】for文のループ処理を配列で行う方法</a>
						<span class="p-blogCard__excerpt">シェルスクリプトのループでfor文を使用しますが、順に入れる文字列が複雑なので配列で指定したい事はないでしょうか？ そういった場合の処理のやり方を、実践していき&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">回数指定を配列で行う実践</h3>



<p>では<strong>回数指定</strong>を<strong><span class="swl-inline-color has-swl-deep-01-color">配列</span></strong>で行う以下のシェルスクリプトを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>array=(1 2 3 4 5 6 7 8 9)
for i in ${array[@]}
do
 echo 配列で$i回繰り返しています
done</code></pre></div>



<h3 class="wp-block-heading">回数指定を配列で行った結果</h3>


<div class="wp-block-image is-style-browser_mac">
<figure class="aligncenter size-full"><img decoding="async" width="582" height="168" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-hairetsu-1-9.png" alt="shellscript-for-配列-1-9の実行結果" class="wp-image-5930" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-hairetsu-1-9.png 582w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-hairetsu-1-9-300x87.png 300w" sizes="(max-width: 582px) 100vw, 582px" /></figure>
</div>


<p>回数指定を配列で行い成功しています。</p>



<h2 class="wp-block-heading">回数指定をファイルを使用して行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="768" src="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1024x768.jpg" alt="ファイル" class="wp-image-3840" srcset="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1024x768.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-300x225.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-768x576.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong>ファイルの中身</strong>を取得して<strong>回数指定</strong>してみます。以下のシェルスクリプトを記述します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `cat shellscript-for-count.txt`
do
 echo ファイルから取得$i回目
done</code></pre></div>



<p>同階層に「shellscript-for-count.txt」を作成して中身を以下のように記述します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>1
2
3
4
5
6
7
8
9</code></pre></div>



<p>これで先程のシェルスクリプトを実行して見ます。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="415" height="164" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-file.png" alt="シェルスクリプトでforの回数指定をファイルに記述した結果" class="wp-image-5939" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-file.png 415w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-file-300x119.png 300w" sizes="(max-width: 415px) 100vw, 415px" /><figcaption class="wp-element-caption">シェルスクリプトでforの回数指定をファイルに記述した結果</figcaption></figure>
</div>


<p>きちんと回数指定をファイルから取得しています。</p>



<h2 class="wp-block-heading">応用編：回数をループの制御で行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/pixta_77773496_M-1024x791.jpg" alt="選択する" class="wp-image-3635"/><figcaption class="wp-element-caption">Business person stand on road fork with hard and easy directions. Choice way isometric</figcaption></figure>
</div>


<p>では回数を指定するが途中で制御を行ってみます。</p>



<h3 class="wp-block-heading">continueやbreakコマンドでループをスキップしたり終了する方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in {1..9}
do
 if [[ $i -eq 5 ]]; then
  continue
 elif [[ $i -eq 8 ]]; then
  break
 fi
 echo $i回目：5は飛ばす、8なら終了
done</code></pre></div>



<p>forの回数指定で9回繰り返します。その中でif文により場合分けしています。</p>



<p>5回目の時は<span class="swl-inline-color has-swl-deep-02-color">「continue」</span>で<span class="swl-bg-color has-swl-deep-02-background-color">スキップ</span>し、8回目の時は<span class="swl-inline-color has-swl-deep-03-color">「break」</span>で<span class="swl-bg-color has-swl-deep-03-background-color">処理を抜け</span>ています。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="476" height="110" src="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-break-continue.png" alt="shellscript-for-break-continueの実行結果" class="wp-image-5929" srcset="https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-break-continue.png 476w, https://sugublog.org/wp-content/uploads/2023/11/shellscript-for-break-continue-300x69.png 300w" sizes="(max-width: 476px) 100vw, 476px" /><figcaption class="wp-element-caption">shellscript-for-break-continueの実行結果</figcaption></figure>
</div>


<p>実行した結果、5番目がスキップされ、7番目までで終了しています。</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">まとめ：シェルスクリプトのfor文で回数の指定方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="522" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg" alt="繰り返し回数を指定する" class="wp-image-4417" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-300x153.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-768x392.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトのfor文の基本構文と使い方</h3>



<p>シェルスクリプトの<strong><span class="swl-inline-color has-swl-deep-01-color">for文を使った繰り返しの基礎</span></strong>は以下になります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in 変数に入れるリストなど
 do
  繰り返す処理
 done</code></pre></div>



<h3 class="wp-block-heading">for文を使ったカウントアップの方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in {1..9}</code></pre></div>



<h3 class="wp-block-heading">seqコマンドを使った回数指定の方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `seq 1 9`</code></pre></div>



<p class="is-style-icon_pen">「`」をつけ忘れないよう気を付ける。</p>



<h3 class="wp-block-heading">回数指定を(())で行う方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for ((i=1; i&lt;9; i++))</code></pre></div>



<h3 class="wp-block-heading">回数指定を配列で行う時の構文</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in ${array[@]}</code></pre></div>



<h3 class="wp-block-heading">回数指定をファイルを使用して行う方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in `cat shellscript-for-count.txt`</code></pre></div>



<h3 class="wp-block-heading">continueやbreakコマンドでループをスキップしたり終了する方法</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for i in {1..9}
do
 if [[ $i -eq 5 ]]; then
  continue
 elif [[ $i -eq 8 ]]; then
  break
 fi
 echo $i回目：5は飛ばす、8なら終了
done</code></pre></div>



<p>シェルスクリプトのforで繰り返し回数を指定する方法を、実践し画像を使用してわかりやすく説明しました！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shellscript-for-number-of-times/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【シェルスクリプト】for文のループ処理を配列で行う方法</title>
		<link>https://sugublog.org/shellscript-for-array/</link>
					<comments>https://sugublog.org/shellscript-for-array/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Mon, 20 Nov 2023 11:53:11 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[配列]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5941</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/10/table_20211010-1-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトのループでfor文を使用しますが、順に入れる文字列が複雑なので配列で指定したい事はないでしょうか？ そういった場合の処理のやり方を、実践していきます。 シェルスクリプトのfor文のループ処理を配列で行う方 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/10/table_20211010-1-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプト</span></strong>のループで<strong><span class="swl-inline-color has-swl-deep-01-color">for文</span></strong>を使用しますが、順に入れる文字列が複雑なので<strong><span class="swl-inline-color has-swl-deep-01-color">配列で指定したい</span></strong>事はないでしょうか？</p>



<p>そういった場合の処理のやり方を、実践していきます。</p>


<div class="p-blogParts post_content" data-partsID="5030">
<p>windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。</p>
</div>


<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">シェルスクリプトのfor文のループ処理を配列で行う方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="519" src="https://sugublog.org/wp-content/uploads/2021/11/nedan-1024x519.jpg" alt="配列" class="wp-image-4540" srcset="https://sugublog.org/wp-content/uploads/2021/11/nedan-1024x519.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/nedan-300x152.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/nedan-768x389.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/nedan.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong>シェルスクリプト</strong>で<strong>ループの処理</strong>を<strong><span class="swl-inline-color has-swl-deep-01-color">配列</span></strong>で行うには、以下のプログラミングを行います。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for 変数 in ${配列名[@]}
 do
  繰り返す処理
 done</code></pre></div>



<p>これでループ処理を配列で処理できます。<span class="swl-marker mark_orange"><strong>配列に入っている順番に要素が変数</strong>に入り処理されます。</span></p>



<h2 class="wp-block-heading">配列の宣言と要素の格納方法</h2>



<h3 class="wp-block-heading">配列の宣言方法</h3>



<p>では<strong><span class="swl-inline-color has-swl-deep-01-color">配列の宣言</span></strong>を行います。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>宣言方法</th><th>意味</th></tr></thead><tbody><tr><td>declare -a 変数</td><td>配列を表す</td></tr><tr><td>declare -ar 変数</td><td>読み取り専用の配列を表す</td></tr></tbody></table><figcaption class="wp-element-caption">配列の宣言方法</figcaption></figure>



<p>これで指定した変数は配列となります。※特に指定しなくても良いです。</p>



<h3 class="wp-block-heading">配列の要素の格納方法</h3>



<p>では<strong><span class="swl-inline-color has-swl-deep-01-color">配列に要素を格納</span></strong>していきます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>array=(1 2 3 4 5 6 7 8 9)</code></pre></div>



<p>配列名に「=」をつけてカッコの中に要素を詰めていきます。要素と要素はスペースで区切ります。</p>



<p class="has-border -border02">この配列をシェルスクリプトのループに用いると、１から順に９まで変数に入り繰り返されます。</p>



<h2 class="wp-block-heading">配列の追加や要素数の取得方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="522" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg" alt="配列の要素を追加" class="wp-image-4417" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-1024x522.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-300x153.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10-768x392.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-10.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>下のシェルスクリプトで<strong><span class="swl-inline-color has-swl-deep-01-color">配列のコピー</span></strong>も可能です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>新配列名=(&quot;${配列[@]}&quot;)</code></pre></div>



<p>下のシェルスクリプトで<strong><span class="swl-inline-color has-swl-deep-02-color">配列の前に要素を追加</span></strong>できます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>新配列名=(aaa bbb &quot;${配列[@]}&quot;)</code></pre></div>



<p>下のシェルスクリプトで<strong><span class="swl-inline-color has-swl-deep-03-color">配列の後ろに要素を追加</span></strong>できます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>新配列名=(&quot;${配列[@]}&quot; aaa bbb)</code></pre></div>



<p><strong>今の配列</strong>に簡単に追加する事も可能です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>配列名+=(aaa bbb)</code></pre></div>



<p>下のシェルスクリプトでは<span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">配列の要素数</span></strong>を数えます。</span></p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>${#配列[@]}</code></pre></div>



<p>全ての配列を表す「${配列[@]}」の中に「#」を入れる事で要素数の数を数えてくれます。</p>



<p>以下のシェルスクリプトでは配列に入っている要素数だけ繰り返す事ができます。</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>array=(3 4 5)

for ((i=1; i&lt;=${#array[@]}; i++))
 do
  echo $i回繰り
 done</code></pre></div>



<p>このシェルスクリプトを実行してみます。</p>


<div class="wp-block-image is-style-browser_mac">
<figure class="aligncenter size-full"><img decoding="async" width="412" height="57" src="https://sugublog.org/wp-content/uploads/2023/11/array_for_loop.png" alt="" class="wp-image-5951" srcset="https://sugublog.org/wp-content/uploads/2023/11/array_for_loop.png 412w, https://sugublog.org/wp-content/uploads/2023/11/array_for_loop-300x42.png 300w" sizes="(max-width: 412px) 100vw, 412px" /></figure>
</div>


<p>配列に格納されている数だけ繰り返されました！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shellscript-for-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチで繰り返しを行う！forコマンドの使用方法をご説明</title>
		<link>https://sugublog.org/windows-bat-for-base/</link>
					<comments>https://sugublog.org/windows-bat-for-base/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Wed, 28 Jul 2021 11:42:58 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[for]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=3763</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/PC007-5-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>そのような悩みを持っている人も多いのではないでしょうか？ bat（バッチファイル）で繰り返しを行いたい場合は、おなじみ「for」を使用します。バッチファイルの「for」は少し癖があるので、馴染みにくいかと思います。 しか [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/PC007-5-1024x683.jpg" class="webfeedsFeaturedVisual" /></p><div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="blue"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">batファイルでforの使い方に困っている人</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>バッチファイルを使用しますが、forコマンドが上手くいきません</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p>そのような悩みを持っている人も多いのではないでしょうか？</p>



<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat（バッチファイル）で繰り返し</span></strong>を行いたい場合は、おなじみ<strong>「for」</strong>を使用します。バッチファイルの「for」は少し癖があるので、馴染みにくいかと思います。</p>



<p class="is-style-icon_info">しかし、<strong>バッチファイルで「for」を使用</strong>する事によって、<strong>業務効率化する事も可能</strong>なので、是非覚えておきたいです。</p>



<p>この記事を最後まで読めば<strong><span class="swl-marker mark_orange">batファイルで「for」コマンドを使用し、繰り返しの処理を行う事が出来ます</span></strong>。それによって業務効率化の幅が広がります。</p>



<p>batでforを使用して繰り返す方法を、画像などを織り交ぜ具体的に見ていきましょう。</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">batファイルでforの使い方！基本構文</h2>



<p> <strong>bat（バッチファイル） でfor</strong>を使用する時の、基本の形は下記です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for オプション %%変数1文字 in (対象) do コマンド</code></pre></div>



<p class="is-style-icon_book">%%変数1文字（例%%iなど）はforの中だけで使用できる変数です。</p>



<p>他にも変数を使用した有効なバッチファイルを作成したい方は、下の記事を参考にしてください。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-11-300x195.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-hensu/">【bat】バッチファイルで変数や環境変数の使い方！エラー対処法あり</a>
						<span class="p-blogCard__excerpt">バッチファイルで変数や環境変数を設定するやり方を教えます。 その他にも変数に関わる事をご紹介します。 この記事でわかる事 変数を指定する時の注意点など 計算結果&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="is-style-big_icon_caution">「対象」はこれから紹介するオプションによって、指定する値が決まってきます。</p>



<p>コマンドを複数指定したい場合は下記でもOK ※ここでは変数を「i」としています。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for オプション %%i in (対象) do (
 コマンド1
 コマンド2
 コマンド3
)</code></pre></div>



<p>基本の形なので、覚えておきましょう。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/html-007-2-300x225.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/python-base-for/">【python基礎】forを使用して繰り返し処理をしよう。</a>
						<span class="p-blogCard__excerpt">pythonとは便利なプログラミング言語です。pythonでforを使用して繰り返し処理を行いたいけど、どう書けばよかったかな。。忘れてしまった時のために覚書しておきます。&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">batファイルでforを使用するオプション４つ</h2>



<p>batファイルでループするには<strong>for</strong>があり、そのオプションは<strong>4つ</strong>あります。</p>



<p class="is-style-icon_announce">デフォルト（オプション無し）はカレントディレクトリの「ファイル」とする。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batファイルのオプション一覧</span></div><div class="cap_box_content">
<ol class="is-style-num_circle wp-block-list">
<li>/d　・・・「対象」をカレントディレクトリの「ディレクトリ」のみとする。</li>



<li>/r　・・・「対象」をカレントディレクトリと、その配下のサブディレクトリの「ファイル」も含む。</li>



<li>/l　・・・「対象」の値を決める</li>



<li>/f　・・・「対象」をテキストファイル内にする</li>
</ol>
</div></div>



<p class="is-style-icon_book">カレントディレクトリとは、batファイルが存在するディレクトリ（フォルダ）です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="401" src="https://sugublog.org/wp-content/uploads/2021/07/image-44-1024x401.png" alt="" class="wp-image-3772" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-44-1024x401.png 1024w, https://sugublog.org/wp-content/uploads/2021/07/image-44-300x118.png 300w, https://sugublog.org/wp-content/uploads/2021/07/image-44-768x301.png 768w, https://sugublog.org/wp-content/uploads/2021/07/image-44.png 1051w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>画像の例で行くと<span class="swl-inline-color has-swl-deep-01-color">forフォルダ</span>に<span class="swl-inline-color has-swl-deep-01-color">for1.batファイル</span>があります。ここがカレントディレクトリになります。</p>



<p>１つの「sub_dir」フォルダと３つのファイル（text1.txtファイルからtext3.txtファイル）があります。</p>



<h2 class="wp-block-heading">batファイルのforのオプション、デフォルトパターン</h2>



<p>オプションなしのデフォルトパターンです。<span class="swl-inline-color has-swl-deep-01-color">「対象」にはカレントディレクトリにある<span class="swl-bg-color has-swl-deep-02-background-color">ファイル名</span></span>などを指定します。例えば「test*」や「*」などです。</p>



<p class="is-style-icon_book">「*」はアスタリスクとよみ、全てを意味します。</p>



<p>bat(バッチファイル)に下記コマンドを書いて、保存後実行してください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-c" data-lang="C"><code>@echo off
for %%i in (*) do
 echo %%i
pause</code></pre></div>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-8-300x229.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-echo-off/">【bat基礎】バッチで「@echo off」の意味や効果とは？</a>
						<span class="p-blogCard__excerpt">batファイルの初めに「@echo off」という記載があると思いますが、この意味は何でしょうか？ なんの効果があるのでしょうか？「@echo off」が無ければどうなるのでしょ&#8230;</span>					</div>
				</div>
			</div>
		</div>

<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/html-007-2-300x225.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-base-echo/">【bat基礎】バッチファイルのechoで文字列表示する！エラー対処法あり</a>
						<span class="p-blogCard__excerpt">batファイルで「echo」の使い方を今回は行います。 「echo」コマンドは指定の文字列を標準出力に表示する事が出来るコマンドです。 ただ文字列を表示するだけでなく、フ&#8230;</span>					</div>
				</div>
			</div>
		</div>

<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/09/jump0907-3-300x225.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-base-pause/">【bat基礎】バッチでPAUSEを使用して一時停止する！waitはありません</a>
						<span class="p-blogCard__excerpt">バッチファイルを使用していて、途中で止めたいところで一時停止したい場合ないでしょうか？ そんな時バッチファイルでは「PAUSE」を使用します。 PAUSEの実用例を現役&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="is-style-icon_book">bat(バッチファイル)を実行する方法は以下にまとめています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/analytics-2-300x157.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-execution/">【bat】バッチを実行する方法！実行できない時の対処法などをご紹介</a>
						<span class="p-blogCard__excerpt">batファイルが既にありそれを実行してと言われたけど、どうやって実行すればわからない人の悩みに答えます。 またbatを実行しようとしたけど、できなかった時の原因と対&#8230;</span>					</div>
				</div>
			</div>
		</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="630" height="176" src="https://sugublog.org/wp-content/uploads/2021/07/image-49.png" alt="" class="wp-image-3775" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-49.png 630w, https://sugublog.org/wp-content/uploads/2021/07/image-49-300x84.png 300w" sizes="(max-width: 630px) 100vw, 630px" /></figure>
</div>


<p class="has-border -border02"><strong><span class="swl-inline-color has-swl-deep-01-color">bat(バッチファイル)のデフォルト</span></strong>では、<span class="swl-marker mark_orange">カレントディレクトリに存在する「ファイル」が、すべて表示</span>されています。</p>



<h2 class="wp-block-heading">batファイルforのオプション①-ディレクトリを対象とする</h2>



<p>① <span style="font-size: revert; background-color: var(--color_content_bg); color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;">/d</span> ・・・カレントディレクトリの「ディレクトリ」を対象とする</p>



<p>forのオプションのデフォルトでは「ファイル」でしたが、カレントディレクトリにある「ディレクトリ」を対象にしたい場合には「/d」を使用します。</p>



<p><span class="swl-inline-color has-swl-deep-01-color">「対象」にはカレントディレクトリにある<span class="swl-bg-color has-swl-deep-02-background-color">フォルダ名</span></span>などを指定します。例えば「sub_dir」や「*」などです。</p>



<p class="is-style-big_icon_check"> bat(バッチファイル)に「/d」のオプションを追記し、保存後実行してください。 </p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-c" data-lang="C"><code>@echo off
for /d %%i in (*) do
 echo %%i
pause</code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="555" height="142" src="https://sugublog.org/wp-content/uploads/2021/07/image-50.png" alt="" class="wp-image-3778" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-50.png 555w, https://sugublog.org/wp-content/uploads/2021/07/image-50-300x77.png 300w" sizes="(max-width: 555px) 100vw, 555px" /></figure>
</div>


<p class="has-border -border02"> <strong><span class="swl-inline-color has-swl-deep-01-color">bat(バッチファイル)の「/d」オプション</span></strong>では、<span class="swl-marker mark_orange">カレントディレクトリに存在する「ディレクトリ」が、すべて表示</span>されています。 </p>



<h2 class="wp-block-heading">batファイルforのオプション②- ディレクトリ配下全てのファイルを対象とする</h2>



<p> ②<span style="font-size: revert; background-color: var(--color_content_bg); color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;">/r　・・・カレントディレクトリ</span>配下のすべての「ファイル」を対象とする</p>



<p> デフォルトではカレントディレクトリのみの「ファイル」でしたが、「/r」のオプションで、配下すべてのファイルが対象となります。 </p>



<p><span class="swl-inline-color has-swl-deep-01-color">「対象」にはカレントディレクトリ<span class="swl-bg-color has-swl-deep-03-background-color">配下すべて</span>の</span><span class="swl-bg-color has-swl-deep-02-background-color">ファイル名</span>などを記入します。</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="401" src="https://sugublog.org/wp-content/uploads/2021/07/image-44-1024x401.png" alt="forカレントディレクトリ" class="wp-image-3772" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-44-1024x401.png 1024w, https://sugublog.org/wp-content/uploads/2021/07/image-44-300x118.png 300w, https://sugublog.org/wp-content/uploads/2021/07/image-44-768x301.png 768w, https://sugublog.org/wp-content/uploads/2021/07/image-44.png 1051w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>図の例だとカレントディレクトリの配下に「sub_dir」ディレクトリがあります。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="411" src="https://sugublog.org/wp-content/uploads/2021/07/image-52-1024x411.png" alt="" class="wp-image-3781" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-52-1024x411.png 1024w, https://sugublog.org/wp-content/uploads/2021/07/image-52-300x120.png 300w, https://sugublog.org/wp-content/uploads/2021/07/image-52-768x308.png 768w, https://sugublog.org/wp-content/uploads/2021/07/image-52.png 1032w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p> 「sub_dir」ディレクトリの配下には「sub_sub」ディレクトリとsub1.txt、sub2.txtファイルがあります。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="401" src="https://sugublog.org/wp-content/uploads/2021/07/image-53-1024x401.png" alt="" class="wp-image-3782" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-53-1024x401.png 1024w, https://sugublog.org/wp-content/uploads/2021/07/image-53-300x118.png 300w, https://sugublog.org/wp-content/uploads/2021/07/image-53-768x301.png 768w, https://sugublog.org/wp-content/uploads/2021/07/image-53.png 1054w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p> 「sub_sub」ディレクトリ の配下には「sub_sub1.txt」ファイルがあります。</p>



<p class="is-style-big_icon_check"> bat(バッチファイル)に「/r」のオプションに変更し、保存後実行してください。  </p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-c" data-lang="C"><code>@echo off
for /r %%i in (*) do
 echo %%i
pause</code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="582" height="200" src="https://sugublog.org/wp-content/uploads/2021/07/image-51.png" alt="" class="wp-image-3780" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-51.png 582w, https://sugublog.org/wp-content/uploads/2021/07/image-51-300x103.png 300w" sizes="(max-width: 582px) 100vw, 582px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat(バッチファイル)の「/r」オプション</span></strong>では<span class="swl-marker mark_orange"><span class="swl-inline-color has-swl-deep-02-color">カレントディレクトリ配下</span>の全てのファイル</span>が、表示されました。</p>



<h2 class="wp-block-heading">batファイルforのオプション③-対象の値を決める</h2>



<p>初期値と増加と閾値を決めて繰り返す事が出来ます。よく見るパターンだと思います。基本の形は下記になります。</p>



<p>for <span class="swl-inline-color has-black-color">/l </span>%%変数1文字  in (<span class="swl-inline-color has-swl-deep-01-color">初期値,増加分,閾値</span>) do コマンド</p>



<p class="is-style-big_icon_check">bat(バッチファイル)に「/l」のオプションを下記のように記入して、保存後実行して見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-c" data-lang="C"><code>@echo off
for /l %%i in (3,3,12) do
 echo %%i
pause</code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="640" height="171" src="https://sugublog.org/wp-content/uploads/2021/07/image-54.png" alt="" class="wp-image-3784" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-54.png 640w, https://sugublog.org/wp-content/uploads/2021/07/image-54-300x80.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure>
</div>


<p>上のbatファイルの例では3から始まり、3ずつ増え、12を超えたら終了になっています。</p>



<p class="has-border -border02"><strong><span class="swl-inline-color has-swl-deep-01-color">bat(バッチファイル)の「/l」オプション</span></strong>では<span class="swl-marker mark_orange"><span class="swl-inline-color has-black-color">初期値,増加分,閾値</span>などを指定して実行</span>できます。</p>



<h2 class="wp-block-heading">batファイルforのオプション④-対象をテキストファイル内にする</h2>



<p>forの繰り返しはテキスト中身の内容を対象にします。一番ややこしいですが、様々な事が可能です。基本の形は下記です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for /f サブオプション %%i in (text1.txt) do echo %%i</code></pre></div>



<p><span class="swl-inline-color has-swl-deep-01-color">「対象」には</span><span class="swl-bg-color has-swl-deep-02-background-color">ファイル名</span>を指定します。</p>



<p>forコマンドオプション④には更に<strong>「サブオプション」</strong>があります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>forオプション④のサブオプション一覧</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>tokens　・・・左から指定した番号のものを取得する。デフォルトはスペース区切り</li>



<li>delims　・・・スペース以外を区切り文字にする</li>



<li>eol　・・・指定した値から始まる行をとばす</li>



<li>skip　・・・頭から指定した数行を無視する</li>
</ul>
</div></div>



<p class="is-style-big_icon_check">カレントディレクトリの「text1.txt」に、下記を記入して保存してください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>1 2 3 4
a1 b2 3b 4b
111 222 333 444
12.34.56.78</code></pre></div>



<h3 class="wp-block-heading"> forのサブオプション①-<span style="font-size: revert; background-color: var(--color_content_bg); color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;">tokens</span>  ・・・n番目の文字を取得する</h3>



<p class="is-style-big_icon_check">batファイルに下記を記入して、保存後実行して見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
REM 1と3番目の文字を取得する
for /f &quot;tokens=1,3&quot; %%i in (text1.txt) do echo %%i %%j
pause</code></pre></div>



<p class="is-style-icon_pen">REMはコメントアウトするコマンドです。詳しくは以下をどうぞ</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/analytics-6-300x200.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-rem-meen-and-use/">【bat基礎】remの意味と使い方を教えます！初心者向けの記事になります！</a>
						<span class="p-blogCard__excerpt">batファイルでremという文字を見かけるけど、どういった意味があるのでしょうか？ どのような時に使用するのでしょうか？使い方は？ といった疑問の声がありましたので&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="is-style-icon_pen">REM以外でコメントアウトする方法。推奨するコメントアウト方法など詳細知りたい場合は以下をどうぞ</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/PC007-8-300x182.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-commentout/">【bat基礎】バッチファイルでコメントアウトする方法！【エラー対処法有り】</a>
						<span class="p-blogCard__excerpt">batファイルでコメントアウトしたい場合の方法を記入します。 スクリプトを作成する上でコメントアウトは大事ですよね。後から見たら何をしているのかわからなくなった&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>上のbatファイルの例では「text1.txt」ファイルの１と３番目の文字を取得します。</p>



<p class="is-style-icon_info">変数を%%iと適当に決めましたが、%%iの次は%%jになります。（アルファベット順）</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="609" height="183" src="https://sugublog.org/wp-content/uploads/2021/07/image-55.png" alt="" class="wp-image-3788" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-55.png 609w, https://sugublog.org/wp-content/uploads/2021/07/image-55-300x90.png 300w" sizes="(max-width: 609px) 100vw, 609px" /></figure>
</div>


<pre class="wp-block-code"><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-black-color"><span class="swl-bg-color has-swl-deep-01-background-color">1</span></mark> 2 <span class="swl-bg-color has-swl-deep-01-background-color">3</span> 4
<span class="swl-bg-color has-swl-deep-01-background-color">a1</span> b2 <span class="swl-bg-color has-swl-deep-01-background-color">3b</span> 4b
<span class="swl-bg-color has-swl-deep-01-background-color">111</span> 222 <span class="swl-bg-color has-swl-deep-01-background-color">333</span> 444
<span class="swl-bg-color has-swl-deep-01-background-color">12.34.56.78</span></code></pre>



<p>テキストファイルのスペース区切りの1と3番目が取得出来ています。</p>



<p class="is-style-icon_info">4行目はスペースがない為、1番目のみ表示されています。（3番目がないのにエラー出ないのはエライですね）</p>



<p class="has-border -border02">takensオプションではn番目の文字を取得する事が出来ます。</p>



<h3 class="wp-block-heading"> forのサブオプション②-delims  ・・・区切り文字を決めて取得する</h3>



<p class="is-style-big_icon_check"> batファイルに青「delims」のオプションを追記して、保存後実行して見て下さい。 </p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
REM 区切り文字を,として1と3番目の文字を取得する
for /f &quot;tokens=1,3 delims=.&quot; %%i in (text1.txt) do echo %%i %%j
pause</code></pre></div>



<p> 「.」を区切り文字として、text1.txtファイルの１と３番目の文字を取得するbatファイルです。 </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="653" height="193" src="https://sugublog.org/wp-content/uploads/2021/07/image-56.png" alt="" class="wp-image-3792" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-56.png 653w, https://sugublog.org/wp-content/uploads/2021/07/image-56-300x89.png 300w" sizes="(max-width: 653px) 100vw, 653px" /></figure>
</div>


<pre class="wp-block-code"><code><span class="swl-bg-color has-swl-deep-01-background-color"><span class="has-inline-color has-white-color">1</span> 2 3 4
a1 b2 3b 4b
111 222 333 444</span>
<span class="swl-bg-color has-swl-deep-01-background-color">12</span>.34.<span class="swl-bg-color has-swl-deep-01-background-color">56</span>.78</code></pre>



<p>1から3は区切り文字「.」がない為、1番目の全てが表示されています。4行目は「.」で区切られた1と3番目を表示しています。</p>



<p class="has-border -border02">delimsサブオプションでは区切り文字を指定して取得できます。</p>



<h3 class="wp-block-heading">  forのサブオプション③-<span style="font-size: revert; background-color: var(--color_content_bg); color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;">eol</span> ・・・指定した値から始まる行をとばす</h3>



<p class="is-style-big_icon_check"> batファイルに「eol=a」のオプションを追記して、保存後実行して見て下さい。  </p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
REM 区切り文字を,として1と3番目の文字を取得するがaから始まるものは省く
for /f &quot;tokens=1,3 delims=. eol=a&quot; %%i in (text1.txt) do echo %%i %%j
pause</code></pre></div>



<p> 「.」を区切り文字として、text1.txtファイルの１と３番目の文字を取得する、<strong>かつ「a」から始まる行をとばす</strong>batファイルです。  </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="661" height="167" src="https://sugublog.org/wp-content/uploads/2021/07/image-57.png" alt="" class="wp-image-3794" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-57.png 661w, https://sugublog.org/wp-content/uploads/2021/07/image-57-300x76.png 300w" sizes="(max-width: 661px) 100vw, 661px" /></figure>
</div>


<pre class="wp-block-code"><code><span class="swl-bg-color has-swl-deep-01-background-color"><span class="has-inline-color has-white-color">1</span> 2 3 4</span>
a1 b2 3b 4b
<span class="swl-bg-color has-swl-deep-01-background-color">111 222 333 444</span>
<span class="swl-bg-color has-swl-deep-01-background-color">12</span>.34.<span class="swl-bg-color has-swl-deep-01-background-color">56</span>.78</code></pre>



<p>  「a」から始まる2行目が消えています。その他は先程の結果と同じです。 </p>



<p class="has-border -border02">eolサブオプションでは、指定した値から始まる行を対象外にできます。</p>



<h3 class="wp-block-heading">  forのサブオプション④-skip・・・ 頭から指定した数行を無視する </h3>



<p class="is-style-big_icon_check"> batファイルに青背景のオプションを追記して、保存後実行して見て下さい。 </p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
REM 区切り文字を,として1と3番目の文字を取得するがaから始まるものと頭から2行目までは省く
for /f &quot;tokens=1,3 delims=. eol=a skip=2&quot; %%i in (text1.txt) do echo %%i %%j
pause</code></pre></div>



<p> 「.」を区切り文字として、text1.txtファイルの１と３番目の文字を取得する、かつ「a」から始まる行をとばす、<strong>かつ1,2行目をとばす</strong>batファイルです。 </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="610" height="162" src="https://sugublog.org/wp-content/uploads/2021/07/image-58.png" alt="" class="wp-image-3795" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-58.png 610w, https://sugublog.org/wp-content/uploads/2021/07/image-58-300x80.png 300w" sizes="(max-width: 610px) 100vw, 610px" /></figure>
</div>


<pre class="wp-block-code"><code>1 2 3 4
a1 b2 3b 4b
<span class="swl-bg-color has-swl-deep-01-background-color">111 222 333 444</span>
<span class="swl-bg-color has-swl-deep-01-background-color">12</span>.34.<span class="swl-bg-color has-swl-deep-01-background-color">56</span>.78</code></pre>



<p>先頭の2行が消えています。後の結果は同じです。</p>



<p class="has-border -border02">skipサブオプションでは、先頭の行から指定した行数を対象外にする事が出来ます。</p>



<h2 class="wp-block-heading">batファイルforのおまけオプション：対象をコマンドの結果にする</h2>



<p><strong>バッチファイルの「for」</strong>では<span class="swl-inline-color has-swl-deep-01-color">コマンドの結果</span>を対象として、値を取得する事も可能です。構文は下記です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>for %%i in (`コマンド`) do コマンド</code></pre></div>



<p class="is-style-big_icon_check">  batファイルに下記を記入して、保存後実行して見て下さい。 </p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
dir
for /f &quot;tokens=1,4 usebackq&quot; %%i in (`dir`) do echo %%i %%j
pause</code></pre></div>



<p>上のバッチファイルの例では、まずdirコマンドで表示しています。その後、dirの結果の1,4番目を取得して見ました。（区切りはデフォルトのスペース）</p>



<p class="is-style-icon_pen">「usebackq」はコマンドの出力結果を対象にするオプションです。</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="707" height="514" src="https://sugublog.org/wp-content/uploads/2021/07/image-59.png" alt="batファイルでデフォルトとオプションで表示したものを比較した図" class="wp-image-3796" srcset="https://sugublog.org/wp-content/uploads/2021/07/image-59.png 707w, https://sugublog.org/wp-content/uploads/2021/07/image-59-300x218.png 300w" sizes="(max-width: 707px) 100vw, 707px" /></figure>
</div>


<p>図の上のブロックはdirコマンドの結果です。図の下のブロックは今回のオプションの結果でコマンド結果の1と4番目の「日時」と「ファイル名」を取得出来ています。</p>



<p class="has-border -border02"><strong>バッチファイルの「for」</strong>では<span class="swl-inline-color has-swl-deep-01-color">コマンドの結果</span>を対象として、値を取得する事も可能です。</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">まとめ：batファイルのforで繰り返しを行う</h2>



<p>batでforを使用して繰り返す基本構文は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-c" data-lang="C"><code>for オプション %%変数1文字 in (対象) do コマンド</code></pre></div>



<p><span class="swl-inline-color has-swl-deep-01-color"><strong>batファイルの「for」で使用できるオプション</strong></span>は以下の通りです。</p>



<p>デフォルト（オプション無し）はカレントディレクトリの「ファイル」とする。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>バッチファイルのオプション一覧</span></div><div class="cap_box_content">
<ol class="is-style-num_circle wp-block-list">
<li>/d　・・・対象をカレントディレクトリの「ディレクトリ」のみとする。</li>



<li>/r　・・・対象をカレントディレクトリと、その配下のサブディレクトリの「ファイル」も含む。</li>



<li>/l　・・・対象の値を決める</li>



<li>/f　・・・対象をテキストファイル内にする</li>
</ol>
</div></div>



<p>対象をカレントディレクトリにしたり、配下のファイルも含む全てにしたり、自分で指定した値などにする事が可能</p>



<p class="has-border -border01">対象をテキストファイルの中身にする事も可能で、更にオプションが必要となる</p>



<p>テキスト中身の内容を対象にします。基本の形は下記です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-c" data-lang="C"><code>for /f サブオプション %%i in (text1.txt) do echo %%i</code></pre></div>



<p>オプション④には更に<strong>「サブオプション」</strong>があります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>オプション④のサブオプション一覧</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>tokens　・・・左から指定した番号のものを取得する。デフォルトはスペース区切り</li>



<li>delims　・・・スペース以外を区切り文字にする</li>



<li>eol　・・・指定した値から始まる行をとばす</li>



<li>skip　・・・頭から指定した数行を無視する</li>
</ul>
</div></div>



<p><strong><span class="swl-inline-color has-swl-deep-01-color">batの「for」で繰り返し</span></strong>をマスターすれば、業務効率化も出来るので是非マスターして業務に取り入れて見て下さい。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/windows-bat-for-base/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
