<?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>bat &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/category/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0/bat/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Tue, 28 Apr 2026 16:17:35 +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>bat &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【コピペでOK】バッチファイルで日付名のフォルダを作成する方法！0埋め対応の決定版</title>
		<link>https://sugublog.org/bat-hizuke-folder/</link>
					<comments>https://sugublog.org/bat-hizuke-folder/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Tue, 28 Apr 2026 16:17:25 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[未分類]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[日付]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=7027</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w-1024x572.png" class="webfeedsFeaturedVisual" /></p>毎日手動で「20260429」のようなフォルダを作っていませんか？ バッチファイル1つで、その作業は0秒になります。 しかし、ネットにあるコードをそのまま使うと、1桁の月（1月〜9月）の時にエラーが出て動かないことがあり [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w-1024x572.png" class="webfeedsFeaturedVisual" /></p>
<p>毎日手動で「20260429」のようなフォルダを作っていませんか？</p>



<p>バッチファイル1つで、その作業は0秒になります。</p>



<p>しかし、ネットにあるコードをそのまま使うと、1桁の月（1月〜9月）の時に<strong><span class="swl-inline-color has-swl-deep-01-color">エラーが出て動かないこと</span></strong>があります。この記事では、そんな「空白問題」を対策済みの、<strong>現場でそのまま使える決定版コード</strong>を現役エンジニアの筆者が紹介します。</p>


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


<p>午前9時にエラーが出る人へ初心者でも失敗しない「0埋め（1桁の月日対策）」済みのコードを紹介します。</p>



<h2 class="wp-block-heading">【結論】batで日付フォルダを作る今すぐ使える魔法のコード</h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="682" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg" alt="データベース" class="wp-image-4502" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-768x511.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
set DT=%date:/=%
set DT=%DT: =0%
set YMD=%DT:~0,8%

mkdir %YMD%</code></pre></div>



<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>実行した瞬間に「20260429」という形式のフォルダを作成します。</li>



<li><strong><span class="swl-inline-color has-swl-deep-01-color">1桁の月日でもエラーになりません</span></strong>（ここが重要！）。</li>
</ul>
</div></div>



<h2 class="wp-block-heading">なぜこの書き方なのか？（技術解説）</h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-1024x683.jpg" alt="カウンセリング" class="wp-image-4564" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p class="is-style-big_icon_batsu">そのままフォルダを作ろうとすると、「スペースのせいでフォルダ名が途切れる」「エラーで止まる」という現象が起きます。</p>



<p>そのまま切り出すと、フォルダ名に勝手にスペースが混じり、コマンドが途中で切れてエラーになります。</p>



<p><code>set DT=%DT: =0%</code> ：この一行が、厄介な「空白」をすべて「0」に置き換えてくれます。これで、1月でも10月でも安定して動くようになります。</p>



<h3 class="wp-block-heading">%date% の正体</h3>



<p>コマンドプロンプトで <code>echo %date%</code> と打つと、多くの場合以下のような結果が返ってきます。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>2026/04/29</strong></p>
</blockquote>


<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/06/time1-1-min-e1623600613499-300x190.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-date-time/">【bat】バッチで日付や時間を取得する方法！初心者向けで簡単に取得可能</a>
						<span class="p-blogCard__excerpt">バッチで日付や時間を取得できれば出来る事は多くなります。 例えば本日の日付と時間を組み合わせて、ログファイルやディレクトリを作成するなどです。 そうする事によ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>しかし、この中身はPCの設定によって以下のように変化します：</p>



<ul class="wp-block-list">
<li><code>2026/04/29</code> （標準的な日本の設定）</li>



<li><code>2026/04/29 水</code> （曜日が含まれる設定）</li>



<li><code>Wed 04/29/2026</code> （英語圏の設定）</li>
</ul>



<p>つまり、<strong><span class="swl-inline-color has-swl-deep-01-color">「1文字目から4文字目が年」という前提が崩れる可能性がある</span></strong>のが、バッチファイル作成における最大の注意点です。</p>



<h3 class="wp-block-heading">文字列の「切り出し」の仕組み</h3>



<p>バッチファイルでは、<code>%date:~開始位置,文字数%</code> という特殊な書き方で、文字列の特定の部分だけを抽出します。</p>



<h4 class="wp-block-heading">日本の標準設定（YYYY/MM/DD）の場合のインデックス</h4>



<p>文字の位置を <strong>0から数える</strong> のがポイントです。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><td><strong>文字</strong></td><td><strong>2</strong></td><td><strong>0</strong></td><td><strong>2</strong></td><td><strong>6</strong></td><td><strong>/</strong></td><td><strong>0</strong></td><td><strong>4</strong></td><td><strong>/</strong></td><td><strong>2</strong></td><td><strong>9</strong></td></tr></thead><tbody><tr><td><strong>位置</strong></td><td>0</td><td>1</td><td>2</td></tr></tbody></table></figure>



<p><strong>年 (YYYY):</strong> <code>%date:~0,4%</code> （0番目から4文字） → <strong>2026</strong></p>



<p><strong>月 (MM):</strong> <code>%date:~5,2%</code> （5番目から2文字） → <strong>04</strong></p>



<p><strong>日 (DD):</strong> <code>%date:~8,2%</code> （8番目から2文字） → <strong>29</strong></p>



<p>これを組み合わせて <code>set YMD=%date:~0,4%%date:~5,2%%date:~8,2%</code> と書くことで、スラッシュを取り除いた <code>20260429</code> という文字列が作れます。</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/money002-e1626536268834-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-string-extraction/">【bat】バッチで文字列を切り出して抽出する方法！変数を使用します！</a>
						<span class="p-blogCard__excerpt">batファイルで文字列を切り出して抽出して使用したいです！ 文字を切り出して自由に変更したいです！何かいい方法はないでしょうか？ そういった疑問に現役エンジニアの&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">なぜ「置換」が必要なのか？</h3>



<p>前述の構成案で <code>set DT=%date:/=%</code> という処理を入れましたが、これには重要な理由があります。</p>



<h4 class="wp-block-heading">スラッシュ <code>/</code> を消去する理由</h4>



<p><code>mkdir</code> コマンドでフォルダを作る際、名前に <code>/</code>（スラッシュ）が含まれていると、Windowsはそれを「フォルダの区切り」と認識してしまいます。</p>



<ul class="wp-block-list">
<li><code>mkdir 2026/04/29</code> と実行すると、「2026」というフォルダの中に「04」フォルダ、その中に「29」フォルダを作ろうとしてしまいます。</li>
</ul>



<h3 class="wp-block-heading">空白を「0」に置換する理由</h3>



<p>これが最も厄介な点です。環境によっては、1桁の月が <code>4</code>（スペース＋4）のように、<strong>10の位が空白</strong>になることがあります。</p>



<p><code>set DT=%DT: =0%</code> と書くことで、「もし空白があったら0に置き換える」という処理を行い、常に <code>04</code> という2桁の状態を保てるようにしています。</p>



<p class="is-style-icon_announce">実は <code>%time%</code>（現在時刻）も同様で、午前9時以前だと時間の先頭に「空白」が入ります（例: <code>9:15:30</code>）。日付フォルダに時刻も入れたい場合は、時刻に対しても同様の「空白→0置換」を行うのがプロの書き方です。</p>



<h2 class="wp-block-heading">【実務応用】さらに便利なカスタマイズ</h2>



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



<h3 class="wp-block-heading">日付だけじゃなくて、時間も含める</h3>



<p>「日付だけじゃなくて、時間も入れたい」「特定の場所に入れたい」という方向けのカスタマイズ集です。</p>



<p>バックアップを1日に何度も取る場合に便利です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>set TM=%time::=%
set TM=%TM: =0%
mkdir %YMD%_%TM:~0,6%</code></pre></div>



<p>結果：<code>20260429_101530</code> のようなフォルダができます。</p>



<p>日付と同じように、時刻も <code>%time%</code> という変数で取得できます。しかし、そのままフォルダ名に使おうとすると<strong>100%エラー</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">
<p><strong>「:（コロン）」と「.（ドット）」が含まれている:</strong> Windowsのフォルダ名には、コロンを使うことができません。</p>



<p><strong>午前中の「空白」問題:</strong> 実は、午前9時などの1桁の時間帯は <code>9:15:30</code> のように<strong>先頭に半角スペース</strong>が入ります。これが原因でバッチが動かないケースが非常に多いのです。</p>
</div></div>



<h3 class="wp-block-heading">【決定版】日付＋時刻のフォルダ作成コード</h3>



<p>このコードは、不要な記号を取り除き、さらに「午前中の空白」を「0」に置き換える対策を施した、最も安全な書き方です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off

:: 1. 日付の取得と整形（/ を取り除く）
set DT=%date:/=%
set DT=%DT: =0%
set YMD=%DT:~0,8%

:: 2. 時刻の取得と整形（: と . を取り除き、空白を0に置換）
set TM=%time::=%
set TM=%TM:.=%
set TM=%TM: =0%

:: 3. フォルダ名の作成（YYYYMMDD_HHMMSS）
:: ※ミリ秒まで入れると長いため、頭から6文字（時分秒）を切り出し
set TARGET_NAME=%YMD%_%TM:~0,6%

:: 4. フォルダ作成
mkdir %TARGET_NAME%

echo フォルダ「%TARGET_NAME%」を作成しました。
pause</code></pre></div>



<h3 class="wp-block-heading">コード解説</h3>



<h4 class="wp-block-heading">① 時刻から記号を消す (<code>%time::=%</code>, <code>%time:.=%</code>)</h4>



<p>バッチファイルには「特定の文字を消す（置換する）」機能があります。</p>



<ul class="wp-block-list">
<li><code>%time::=%</code> ：すべての <code>:</code> を空文字に置き換えます。</li>



<li><code>%time:.=%</code> ：すべての <code>.</code> を空文字に置き換えます。 これで、<code>10:15:30.45</code> が <code>10153045</code> というただの数字の羅列になります。</li>
</ul>



<h4 class="wp-block-heading">② 魔の「午前9時問題」を解決する (<code>set TM=%TM: =0%</code>)</h4>



<p>ここが<strong>プロのこだわりポイント</strong>です。 午前9時に実行した場合、標準では <code>91530</code> となり、先頭にスペースが残ります。このまま <code>mkdir</code> を使うと、「コマンドの区切り」と誤認されてエラーになります。 この一行を入れることで、<code>091530</code> となり、常に安定したフォルダ名になります。</p>



<h4 class="wp-block-heading">③ 必要な部分だけ切り出す (<code>%TM:~0,6%</code>)</h4>



<p>時刻変数はミリ秒まで含まれていて少し長すぎるため、最初の6文字（時・分・秒）だけを切り出しています。</p>



<h3 class="wp-block-heading">実務での活用例</h3>



<p><strong>ログファイルの整理:</strong> 実行するたびに新しいフォルダを作って、そこに実行結果を保存する。</p>



<p><strong>上書き禁止のバックアップ:</strong> 日付だけのフォルダだと、1日に2回実行したときに上書きされてしまいます。時刻まで含めれば「20260429_101530」「20260429_143000」のように別々に保存できます。</p>



<h3 class="wp-block-heading">特定のディレクトリに作成する</h3>



<p>デスクトップや共有サーバーに直接作りたい場合です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>mkdir &quot;C:\Users\YourName\Desktop\Backup_%YMD%&quot;</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/11/HDD_1107-2-1-300x126.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-create-folder/">【bat基礎】バッチでフォルダ作成する方法！一括で作成する方法も伝授</a>
						<span class="p-blogCard__excerpt">batファイルでフォルダを作成するやり方をお伝えします。 業務などで同じ構成のフォルダを複数作成するときは、batファイルでフォルダを作成しましょう！ 同じツリーを&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">まとめ</h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="572" src="https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w-1024x572.png" alt="batで日付フォルダを作成する方法" class="wp-image-7030" srcset="https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w-1024x572.png 1024w, https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w-300x167.png 300w, https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w-768x429.png 768w, https://sugublog.org/wp-content/uploads/2026/04/Gemini_Generated_Image_mp3w5cmp3w5cmp3w.png 1376w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">batで日付フォルダを作成する方法</figcaption></figure>



<p>プロの現場では、ログ保存のためにこのコードを必ず忍ばせています</p>



<p>日付取得はバッチファイル自動化の第一歩。</p>



<p>一度作っておけば、将来的にログ管理やバックアップにも応用可能です。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-hizuke-folder/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【脱・cmd】バッチファイルからPowerShellへ！</title>
		<link>https://sugublog.org/powershell-beginner-guide/</link>
					<comments>https://sugublog.org/powershell-beginner-guide/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sun, 29 Mar 2026 15:17:29 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[プログラミング]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=7017</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal-1024x572.png" class="webfeedsFeaturedVisual" /></p>エンジニアのための入門・書き換えガイド プログラムを実践してきた筆者が、基本構文から実務で使える便利コマンドまで徹底解説します。 なぜバッチファイルからPowerShellなのか 「あぁ、また文字化けか……」 Windo [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal-1024x572.png" class="webfeedsFeaturedVisual" /></p>
<p><strong>エンジニアのための入門・書き換えガイド</strong></p>



<p>プログラムを実践してきた筆者が、基本構文から実務で使える便利コマンドまで徹底解説します。</p>


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


<h2 class="wp-block-heading">なぜバッチファイルからPowerShellなのか</h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="853" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-6-1024x853.jpg" alt="教師" class="wp-image-4809" srcset="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-6-1024x853.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-6-300x250.jpg 300w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-6-768x640.jpg 768w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-6.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>「あぁ、また文字化けか……」</p>



<p>Windowsエンジニアなら誰もが一度は、メモ帳で書いたバッチファイル（.bat）を叩いた瞬間に真っ黒な画面で暴れる「謎の記号」に頭を抱えたことがあるはずです。</p>



<p class="is-style-big_icon_hatena">ファイル名に日付を入れてバックアップを取りたいだけなのに、%date:~0,4%%date:~5,2%… といった、まるで呪文のような文字列をググりながら必死に打ち込んだ経験はありませんか？</p>



<p>確かに、長年連れ添ったバッチファイルは手軽です。<br>しかし、現場のDX化が進み、クラウドやJSON、複雑なAPI連携が当たり前になった令和の今、「コマンドプロンプト（bat）」という武器だけでは、正直戦えなくなってきています。</p>



<p>「batで無理やり書けばなんとかなる」<br>その職人気質なこだわりが、実はあなたの貴重な残業時間を奪っているかもしれません。</p>



<p class="is-style-big_icon_point">そこでおすすめしたいのが、Windowsの標準シェルでありながら、圧倒的なパワーを秘めた<strong>「PowerShell」</strong>です。</p>



<p>「難しそう」「青い画面はとっつきにくい」<br>そう思って避けてきたあなたへ。</p>



<p>この記事では、IT現場の「泥臭い自動化」を支えてきたエンジニアの視点で、<strong>バッチファイルで消耗していたあの日々を過去にする「PowerShellの最短攻略法」</strong>を解説します。</p>



<p>この記事を読み終える頃には、あなたは「.bat」を右クリックして編集するのをやめ、自信を持って「.ps1」を作り始めているはずです。</p>



<ul class="wp-block-list">
<li><strong>なぜ今、PowerShellなのか？</strong>
<ul class="wp-block-list">
<li>Windows 10/11の標準シェルがPowerShellへ移行している現状。</li>



<li>bat（コマンドプロンプト）では難しかった「複雑な条件分岐」や「JSON/XML操作」が容易になるメリット。</li>
</ul>
</li>



<li><strong>この記事で学べること</strong>
<ul class="wp-block-list">
<li>PowerShellの起動と実行ポリシーの設定。</li>



<li>batファイルとの書き方の違い（比較表）。</li>
</ul>
</li>
</ul>



<h2 class="wp-block-heading"><strong>&nbsp;準備：実行ポリシーの設定（最初につまずくポイント）</strong></h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/06/shutterstock_1433996435-min-1024x683.jpg" alt="エラー" class="wp-image-2885"/></figure>



<p>新しくスクリプトを学ぶ時に全く別の箇所でつまずき挫折することはよくあります。なのでまずはPowershellで最初につまずくポイントを整理しておきます。</p>



<div class="swell-block-capbox cap_box"><div class="cap_box_ttl"><span>Powershellで最初につまずくポイント</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>PowerShellはデフォルトではスクリプト（.ps1）が実行できない仕様。</li>



<li>Set-ExecutionPolicy コマンドの紹介。RemoteSigned への変更手順をコマンド例とともに解説。</li>
</ul>
</div></div>



<h3 class="wp-block-heading">PowerShellはデフォルトではスクリプト（.ps1）が実行できない仕様</h3>



<p>PowerShellの学習を始めた人が、真っ先にぶつかる「高い壁」。 それが、<strong><span class="swl-marker mark_orange"><span class="swl-inline-color has-swl-deep-01-color">「スクリプトが実行できません」という無慈悲な赤文字のエラー</span></span></strong>です。</p>



<p>「せっかく勉強しようと思ったのに、最初から拒否されるのか……」と、ここで挫折してしまうのは本当にもったいない！</p>



<p>実はこれ、PowerShellが「悪意のあるプログラムからあなたのPCを守るため」にあえてかけている「強力なセーフティ（安全装置）」なのです。バッチファイル（.bat）にはなかった、現代的なセキュリティの考え方ですね。</p>



<p>この安全装置を、開発に支障がない程度に「少しだけ緩める」のが、最初の一歩になります。</p>



<h3 class="wp-block-heading">実行ポリシーを「RemoteSigned」へ変更する</h3>



<p>実務で最もバランスが良い設定が <strong><code>RemoteSigned</code></strong> です。 「自分で書いたスクリプトは動かすけど、ネットから拾ってきた怪しいやつは署名がないと動かさないよ」という、まさにエンジニアのための設定です。</p>



<p>設定手順はたったの2ステップ。</p>



<ol start="1" class="wp-block-list">
<li><strong>PowerShellを「管理者として実行」で起動する</strong> （ここを忘れると、設定変更自体が拒否されます！）</li>



<li><strong>魔法のコマンドを打ち込む</strong></li>
</ol>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>Set-ExecutionPolicy RemoteSigned</code></pre></div>



<p>コマンドを打つと、「実行ポリシーを変更しますか？」と聞かれますので、迷わず <strong><code>Y</code></strong>（または <code>A</code>）を押してエンター。これで、あなたのPCは「PowerShellを受け入れる準備」が整いました。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>【プロのワンポイントアドバイス】</strong> 「セキュリティを下げるのは怖い……」と思うかもしれませんが、ご安心を。この設定は、あくまで「スクリプトファイル（.ps1）を直接叩けるようにする」ためのもの。まずはここを突破しないと、PowerShellの真価を味わうことはできません。</p>
</blockquote>



<h2 class="wp-block-heading"><strong>【比較で覚える】bat vs PowerShell 基本構文</strong></h2>



<p>「コマンドプロンプト（bat）で培った知識は、もうゴミ箱行きなのか？」 いいえ、そんなことはありません。</p>



<p>PowerShellはバッチファイルの進化系です。多くのコマンド（<code>dir</code> や <code>cd</code>、<code>copy</code> など）が「エイリアス（別名）」として引き継がれているため、今までの操作感のまま、より強力な書き方ができるようになります。</p>



<p>まずは、最もよく使う「基本の型」を比較表でサクッと確認してみましょう。</p>



<p class="has-text-align-center is-style-balloon_box">bat vs PowerShell 基本構文</p>



<figure class="wp-block-table is-style-regular"><table><thead><tr><th><strong>項目</strong></th><th><strong>バッチファイル (.bat)</strong></th><th><strong>PowerShell (.ps1)</strong></th><th>ポイント</th></tr></thead><tbody><tr><th><strong>変数</strong></th><td>set VAR=value</td><td>$var = &#8220;value&#8221;</td><td>PSは頭に <code>$</code> を付けるだけ！</td></tr><tr><th><strong>出力</strong></th><td>echo Hello</td><td>Write-Host &#8220;Hello&#8221;</td><td><code>echo</code> でも動くが正式にはこれ</td></tr><tr><th><strong>コメント</strong></th><td>rem または ::</td><td>#</td><td>プログラミング言語に近い <code>#</code></td></tr><tr><th><strong>引数</strong></th><td>%1, %2</td><td>$args[0], $args[1]</td><td>0番目から始まる配列形式</td></tr><tr><th>日付の取得</th><td><code>%date%</code> (要整形)</td><td>Get-Date</td><td>オブジェクトとして高機能</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">実際のコードで見比べる「変数の扱い」</h3>



<p>バッチファイルで一番イライラするのは、変数を「%」で囲んだり、代入の時にスペースを空けてはいけなかったりする独特のルールですよね。PowerShellなら、もっと直感的に書けます。</p>



<h4 class="wp-block-heading">バッチファイルの場合</h4>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
set NAME=SuguBlog
echo Hello, %NAME%!
pause</code></pre></div>



<h4 class="wp-block-heading">PowerShellの場合</h4>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code># 変数に代入（スペースがあってもOK！）
$name = &quot;SuguBlog&quot;

# 文字列の中でそのまま展開できる
Write-Host &quot;Hello, ${name}!&quot;

# 画面を止めたい時は Read-Host
Read-Host &quot;Enterキーを押して終了してください...&quot;</code></pre></div>



<h3 class="wp-block-heading">なぜPowerShellの方が「楽」なのか？</h3>



<p>バッチファイルの場合、変数を連結したり計算したりするたびに <code>set /a</code> や複雑なエスケープ処理が必要でした。</p>



<p>しかし、<span class="swl-inline-color has-swl-deep-01-color"><span class="swl-marker mark_orange">PowerShellは「エンジニアが直感的に書きたいように書ける」ように設計されています。</span></span> 例えば、文字列の中で <code>$name</code> と書くだけで中身を表示してくれる「変数展開」のスマートさ。これだけでも、書き換えのストレスが半分以下になるはずです。</p>



<h2 class="wp-block-heading has-text-align-left">PowerShellの最大の特徴「オブジェクト」を理解する</h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="944" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-1024x944.jpg" alt="パソコン教室" class="wp-image-4804" srcset="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-1024x944.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-300x277.jpg 300w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-768x708.jpg 768w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<div class="swell-block-capbox cap_box"><div class="cap_box_ttl"><span>見出し</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>テキストではなく「モノ（オブジェクト）」としてデータを扱う考え方の解説。</li>



<li><strong>パイプライン（|）の強力さ</strong>
<ul class="wp-block-list">
<li>例：Get-ChildItem | Where-Object { $_.Length -gt 1MB }</li>



<li>（1MB以上のファイルを抽出する、といった直感的な操作例）</li>
</ul>
</li>
</ul>
</div></div>



<ul class="wp-block-list">
<li></li>
</ul>



<p>バッチファイルやLinuxのシェルを使っていると、コマンドの実行結果はただの「文字（テキスト）」として返ってきます。 そのため、特定のデータを取り出すには、複雑な <code>findstr</code> や <code>awk</code> を駆使して、文字を切り刻む必要がありました。</p>



<p class="is-style-icon_good">しかし、PowerShellは違います。 <strong>返ってくるのは文字ではなく、データが詰まった「オブジェクト（情報の塊）」</strong>なのです。</p>



<h3 class="wp-block-heading">「文字」ではなく「中身」で会話する</h3>



<p>例えば、ファイルのサイズを知りたいとき。 バッチファイルでは <code>dir</code> の結果から「何文字目から何文字目がサイズで……」とパズルをしていました。</p>



<h3 class="wp-block-heading">魔法の杖「パイプライン（|）」の真骨頂</h3>



<p class="is-style-icon_good">PowerShellなら、ファイルという「オブジェクト」に「ねぇ、サイズ（Length）だけ教えて？」と聞くだけで終わります。</p>



<p>この「オブジェクト」の真価を発揮するのが、おなじみの縦棒 <strong><code>|</code>（パイプライン）</strong> です。 PowerShellのパイプは、ただ結果を渡すだけではありません。「データの塊を、次のコマンドへ丸ごと放り投げる」役割を持っています。</p>



<h4 class="wp-block-heading">1MB以上のファイルだけを「スグ」に見つける例</h4>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>直感的なフィルタリング</p>
</blockquote>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code># カレントディレクトリのファイル一覧を取得して、
# その中から「サイズが1MBより大きいもの」だけを選び出す
Get-ChildItem | Where-Object { $_.Length -gt 1MB }</code></pre></div>



<p>これ、バッチファイルで書こうとしたら何行必要か想像してみてください。 PowerShellなら、たった1行。「何を取得して（Get）」「どう分けるか（Where）」を繋げるだけで、複雑なロジックが完成します。</p>



<h3 class="wp-block-heading">「$_」という、ちょっと不思議な記号</h3>



<p>コードの中に出てきた <code>$_</code> という記号。最初は「何これ？」と思うかもしれませんが、これは「今、パイプラインを通っているそのデータ」を指す代名詞です。</p>



<p>「目の前のコイツの、サイズ（Length）を見て！」 そんな風にPCに指示を出しているイメージですね。</p>



<h2 class="wp-block-heading"><strong>実務で「スグ」使える逆引きレシピ 3選</strong></h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="770" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-2-1024x770.jpg" alt="パソコン教室" class="wp-image-4805" srcset="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-2-1024x770.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-2-300x226.jpg 300w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-2-768x577.jpg 768w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<div class="swell-block-capbox cap_box"><div class="cap_box_ttl"><span>見出し</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li><strong>① ファイルのバックアップ（日付付与）</strong>
<ul class="wp-block-list">
<li>Get-Date フォーマットを使ったファイル名生成。</li>
</ul>
</li>



<li><strong>② 特定の文字列を含むファイルの検索と抽出</strong>
<ul class="wp-block-list">
<li>Select-String の使い方。</li>
</ul>
</li>



<li><strong>③ CSVファイルの読み込みと加工</strong>
<ul class="wp-block-list">
<li>Import-Csv でデータをループ処理する方法。</li>
</ul>
</li>
</ul>
</div></div>



<ul class="wp-block-list">
<li></li>
</ul>



<p>「理屈はわかった。で、実際どう使うの？」 そんな声にお応えして、今日からあなたの現場で<strong><span class="swl-inline-color has-swl-deep-01-color">即戦力になる3つの鉄板レシピ</span></strong>を用意しました。バッチファイルでは苦労していたあの作業が、驚くほどスマートに片付きます。</p>



<h3 class="wp-block-heading">① ファイルのバックアップ（日付付与）</h3>



<p>ログファイルや設定ファイルを保存する際、「ファイル名_20240329.bak」のように日付を入れたいケースは多いですよね。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>日付付きコピーの決定版</p>
</blockquote>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code># 今日の日付を「yyyyMMdd」形式で取得
$date = Get-Date -Format &quot;yyyyMMdd&quot;

# 元ファイルを日付付きでコピー
Copy-Item &quot;C:\logs\app.log&quot; &quot;C:\backup\app_$date.log&quot;</code></pre></div>



<p class="is-style-icon_good"><strong>【ここがスゴイ！】</strong> バッチファイルのように <code>%date:~0,4%</code> といった複雑な切り出しは不要。<code>-Format</code> を指定するだけで、あなたが欲しい形の日付が手に入ります。</p>



<h3 class="wp-block-heading">② 特定の文字列を含むファイルを一括検索</h3>



<p>「あの設定値、どのファイルに書いたっけ？」 そんな時は <code>Select-String</code> の出番です。Linuxの <code>grep</code> に近い感覚で使えます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code># 「Error」という文字が含まれる .txt ファイルをサブフォルダまで検索
Get-ChildItem -Recurse *.txt | Select-String &quot;Error&quot;</code></pre></div>



<p class="is-style-icon_good"><strong>【ここがスゴイ！】</strong> <code>-Recurse</code> をつけるだけで、サブフォルダまで根こそぎ探してくれます。結果には「行番号」も表示されるので、修正作業が爆速になります。</p>



<h3 class="wp-block-heading">③ CSVファイルの読み込みと加工</h3>



<p>Excelで管理しているリストを元に、フォルダを一括作成したり設定を変更したりする場合、PowerShellは最強のツールになります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code># CSVを読み込んで、1行ずつ処理する
Import-Csv &quot;C:\data\user_list.csv&quot; | ForEach-Object {
    # 「UserName」列の値を使ってフォルダを作成
    New-Item -Path &quot;C:\Users\Desktop&quot; -Name $_.UserName -ItemType Directory
}</code></pre></div>



<p class="is-style-icon_good"><strong>【ここがスゴイ！】</strong> <code>Import-Csv</code> を使うと、CSVのヘッダー名をそのまま変数として扱えます。文字割りの処理を自分で書く必要はもうありません。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">【＋α】さらに一歩先へ：Active Directory（AD）連携</h3>



<p>もしあなたが社内インフラや情シスの担当者なら、PowerShellは「知っておかなければならない武器」に変わります。</p>



<p>例えば、**「期限切れ間近のユーザーアカウントを抽出する」**といった作業。 GUI（マウス操作）で一人ずつ確認していたら日が暮れますが、PowerShell（Active Directoryモジュール）を使えば一瞬です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code># 30日以内にパスワードが切れるユーザーを一覧表示
Search-ADAccount -PasswordExpiring -TimeSpan 30.00:00:00 | Select-Object Name, SamAccountName</code></pre></div>



<p>こうした「AD連携」や「サーバー構築の自動化」こそが、PowerShellの本領発揮。 まずは日々のファイル操作から慣れていき、徐々にこうした高度な自動化に挑戦していきましょう！</p>



<h2 class="wp-block-heading">まとめ：小さな一歩が「エンジニアの自由」を作る</h2>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="572" src="https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal-1024x572.png" alt="バッチファイル（.bat）からPowerShell（.ps1）への移行をイメージした比較画像。左側には古いMS-DOS風の画面と手作業のファイル、右側にはPowerShellのコード（Get-DateやWhere-Object）と自動化されたデータパイプラインが描かれ、「バッチで消耗していませんか？PowerShellで『自由』を手に入れよう！」というキャッチコピーが入ったスグブログのアイキャッチ画像" class="wp-image-7022" srcset="https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal-1024x572.png 1024w, https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal-300x167.png 300w, https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal-768x429.png 768w, https://sugublog.org/wp-content/uploads/2026/03/Gemini_Generated_Image_4falkt4falkt4fal.png 1376w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">バッチファイルの限界を感じていませんか？PowerShellなら、たった1行のコードで複雑な自動化をスマートに実現できます。</figcaption></figure>



<div class="swell-block-capbox cap_box"><div class="cap_box_ttl"><span>見出し</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>PowerShellは最初は難しく感じるが、一度覚えると自動化の幅が劇的に広がる。</li>



<li>まずは「今のbatを1つだけPowerShellに書き換えてみる」ことから始めよう。</li>
</ul>
</div></div>



<p>最後まで読んでいただき、ありがとうございます！</p>



<p>ここまで読んでくださったあなたは、もう薄々感じているはずです。 「バッチファイルで頑張っていたあの時間は、もっと短縮できたんだ」と。</p>



<p>もちろん、長年使い古した <code>.bat</code> をすべて捨てる必要はありません。 でも、もし明日「ファイル名を一括で変えたい」「ログから特定のエラーを探したい」という作業が発生したら、グッとこらえて <strong>PowerShell（.ps1）</strong> を開いてみてください。</p>



<p>最初は、実行ポリシーのエラーに首を傾げるかもしれません。 パイプラインの書き方に戸惑うかもしれません。</p>



<p class="is-style-big_icon_point">しかし、その「最初の一歩」を乗り越えた先には、<strong>面倒なルーチンワークを1行のコマンドで終わらせ、涼しい顔で定時に帰宅する「自由」が待っています。</strong></p>



<h3 class="wp-block-heading">次のステップへのヒント</h3>



<ul class="wp-block-list">
<li><strong>まずは「1行」書いてみる:</strong> <code>Get-ChildItem</code>（dirの代わり）から始めてみましょう。</li>



<li><strong>わからないことは「スグ」検索:</strong> PowerShellのコミュニティは広大です。困ったらこの記事を読み返してください。</li>
</ul>



<p>ITエンジニアの世界は、新しい武器を手に入れた者勝ちです。 今日、あなたが手に入れた「PowerShell」という強力な剣を、ぜひ現場で振り回してみてくださいね。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>これからも『スグブログ』では、エンジニアの皆さんが「スグ」に現場で使える技術ネタを発信していきます。</p>



<p>次は、Active Directoryの自動操作や、さらに高度なスクリプト術でお会いしましょう。 それでは、また！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/powershell-beginner-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチでログの出力方法をご紹介！実行結果などを出力する</title>
		<link>https://sugublog.org/bat-log/</link>
					<comments>https://sugublog.org/bat-log/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sun, 25 Feb 2024 14:21:42 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat log]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6423</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/moving_0926-2-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>bat（バッチ）ファイルで実行結果をログに出力しませんか？ 日時処理などでこけていた場合に、どこで失敗したか確認する時にログ出力は必須です。 また日時や時間を使用してログファイル名を変えたいと思った事はないでしょうか？  [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/moving_0926-2-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat（バッチ）ファイル</span></strong>で実行結果を<strong>ログ</strong>に<strong>出力</strong>しませんか？</p>



<p>日時処理などでこけていた場合に、どこで失敗したか確認する時に<strong>ログ出力は必須</strong>です。</p>



<p>また日時や時間を使用してログファイル名を変えたいと思った事はないでしょうか？</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_002-e1624111291945.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_002-e1624111291945.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>そんなエンジニアの悩みに、現役エンジニアの筆者が答えていきます。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>

<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">bat（バッチ）でログの出力方法</h2>


<div class="wp-block-image is-style-photo_frame">
<figure class="aligncenter size-large"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-7-1024x1024.jpg" alt="パソコン教室" class="wp-image-4810"/></figure>
</div>


<h3 class="wp-block-heading">bat（バッチ）でログの出力方法の基本</h3>



<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat（バッチ）でログの出力方法</span></strong>はリダイレクトを使用します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="log出力.bat"><code>echo 処理STEP1完了しました！ &gt;&gt; log.log</code></pre></div>



<p>「&gt;」は上書き保存し「&gt;&gt;」は追記になります。</p>



<p>これでbatファイルと同階層にlog.logファイルが出力されます。</p>



<p>logファイルの中身を確認してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="log.logの中身"><code>処理STEP1完了しました！</code></pre></div>



<p class="has-border -border02">batファイルでログの出力ができています。</p>



<h3 class="wp-block-heading">ログに日付と時間を記入する</h3>



<p>今度は<span class="swl-marker mark_orange">ログの内容に日付と時間を入れてみます。</span></p>



<p>batでもともと使用できる変数にdateやtimeというものがあり、それらを使用すれば簡単に日付と時間を取得する事が可能です。</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/06/time1-1-min-e1623600613499-300x190.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-date-time/">【bat】バッチで日付や時間を取得する方法！初心者向けで簡単に取得可能</a>
						<span class="p-blogCard__excerpt">バッチで日付や時間を取得できれば出来る事は多くなります。 例えば本日の日付と時間を組み合わせて、ログファイルやディレクトリを作成するなどです。 そうする事によ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>batで変数を展開する時は前後に「%」をつけます。dateなら「%date%」となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="log出力.bat" data-line="2"><code>echo %date% %time% 処理STEP1完了しました！ &gt;&gt; log.log</code></pre></div>



<p>logファイルの中身を確認してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="log.logの中身"><code>処理STEP1完了しました！ 
2024/02/25 15:08:46.56 処理STEP1完了しました！</code></pre></div>



<p class="has-border -border02">先程のファイル内容に、日付と時間付きログが追加されているのがわかります。</p>



<p class="is-style-icon_info">このようにbatの処理ステップごとに日付と時間を付けてログ出力すれば、どこで処理が失敗したか、何時に処理が行われているかがわかり、エラーの原因究明に一役かうことがあるかもしれません。</p>



<h3 class="wp-block-heading">ログのパスが絶対パスにて書かれている場合の注意点</h3>



<p>ログ出力のパスは絶対パスで指定する事が決まっている環境もあると思います。そんな時にbatファイル流用しようとすればパスもいちいち修正しなければいけません。</p>



<p>そうならない為にもbatのパスを変数で指定しておきます。「%~dp0\」で取得することが可能です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="log出力.bat" data-line="2"><code>echo %date% %time% 処理STEP1完了しました！ &gt;&gt; %~dp0\log.log</code></pre></div>



<p>このbatファイルを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="log.logの中身"><code>処理STEP1完了しました！ 
2024/02/25 15:08:46.56 処理STEP1完了しました！
2024/02/25 15:23:12.49 STEP1完了しました！ </code></pre></div>



<p class="has-border -border01">絶対パスでも同じ様に追記されています。</p>



<h2 class="wp-block-heading">bat（バッチ）でログファイル名を日付や時間にして出力する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="664" src="https://sugublog.org/wp-content/uploads/2021/11/HDD_1107-3-1-1024x664.jpg" alt="ログ出力" class="wp-image-4363" srcset="https://sugublog.org/wp-content/uploads/2021/11/HDD_1107-3-1-1024x664.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/HDD_1107-3-1-300x195.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/HDD_1107-3-1-768x498.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/HDD_1107-3-1.jpg 1199w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p class="is-style-big_icon_check">今度は日付や時間で<strong>ログファイル名</strong>を変更したいと思いませんか？</p>



<p>そうすれば日付や時間ごとにログファイルを分ける事ができて、後から見やすくなります。</p>



<p>先程と同じように日付と時間を変数にて取得して、ログ出力してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>echo %date% %time% STEP1完了しました！ &gt;&gt; %~dp0\%date%%time%log.log</code></pre></div>



<p>このbatを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>ファイル名、ディレクトリ名、またはボリューム ラベルの構文が間違っています。</code></pre></div>



<p class="is-style-big_icon_batsu">エラーがでてログ出力できていません。</p>



<h3 class="wp-block-heading">ログファイル名を日付や時間にするとエラーが起こる原因と対処法</h3>



<p>batでログファイル名を日付や時間にするとエラーが起こる原因は、<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" data-file="log.logの中身"><code>処理STEP1完了しました！ 
2024/02/25 15:08:46.56 処理STEP1完了しました！
2024/02/25 15:23:12.49 STEP1完了しました！ </code></pre></div>



<p class="is-style-big_icon_caution">dateで取得した日付の「/」が使用できません。timeでは「:」が使用できません。</p>



<p>ですので%date%と%time%で取得した、使用できない文字列を修正する必要があります。</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>



<p>修正したbatファイルが以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>set time1=%time::=%
echo %date% %time% STEP1完了しました！ &gt;&gt; %~dp0\%date:/=%-%time1:~0,6%.log</code></pre></div>



<p>時間の「:」を削除して変数「time1」にいれました。（set time1=%time::=%）</p>



<p>time1の最初の文字から6文字目までを取得するのが（%time1:~0,6%）</p>



<p>日付の「/」を削除するコマンドが（%date:/=%）</p>



<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/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>ではこの修正したbatファイルを実行して、ログ出力を行います。</p>



<p class="has-border -border02">batと同階層に「日付-時間6桁.log」ファイルが作成されて、中身に「日付 時間 STEP1完了しました！」とログ出力できています。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_002-e1624111291945.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_002-e1624111291945.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>batでログ出力する時に使用してみてください。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-log/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】typeコマンドの様々な使い方！nulを併用するやり方もご紹介</title>
		<link>https://sugublog.org/bat-type-command-nul/</link>
					<comments>https://sugublog.org/bat-type-command-nul/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sun, 25 Feb 2024 05:13:21 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat type]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6415</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/analytics-7-1024x454.jpg" class="webfeedsFeaturedVisual" /></p>batでtypeコマンドの使い方を知っていますでしょうか？ ファイルの中身やbatファイルの中身などを表示する事ができます。 その他にnulと併用してファイル作成することも可能です。 この記事を読む事でbatのtypeコ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/analytics-7-1024x454.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">typeコマンド</span></strong>の使い方を知っていますでしょうか？</p>



<p>ファイルの中身やbatファイルの中身などを表示する事ができます。</p>



<p>その他にnulと併用してファイル作成することも可能です。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_002-e1624111291945.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_002-e1624111291945.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>そんなbatのtypeコマンドについて、現役エンジニアの筆者がまとめました。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>

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


<p>この記事を読む事で<span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>の<strong><span class="swl-inline-color has-swl-deep-01-color">typeコマンド</span></strong>について、完全に理解する事ができます。</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でtypeコマンドの使い方</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="436" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-2-1024x436.jpg" alt="batのtypeコマンド" class="wp-image-4501" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-2-1024x436.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-2-300x128.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-2-768x327.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>の<strong><span class="swl-inline-color has-swl-deep-01-color">typeコマンド</span></strong>の使い方の基礎は、ファイルなどの中身を表示する事です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>type ファイル名</code></pre></div>



<p>このコマンドでファイルの内容を表示します。</p>



<p class="is-style-icon_info">ではbatファイルの同じ階層に下の図のような環境があるとします。</p>


<div class="wp-block-image is-style-border">
<figure class="aligncenter size-full"><img decoding="async" width="858" height="640" src="https://sugublog.org/wp-content/uploads/2024/02/bat_type_1.png" alt="batのtypeコマンド" class="wp-image-6421" srcset="https://sugublog.org/wp-content/uploads/2024/02/bat_type_1.png 858w, https://sugublog.org/wp-content/uploads/2024/02/bat_type_1-300x224.png 300w, https://sugublog.org/wp-content/uploads/2024/02/bat_type_1-768x573.png 768w" sizes="(max-width: 858px) 100vw, 858px" /></figure>
</div>


<p class="is-style-icon_announce">type.batには以下のコードを記入します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="type.bat"><code>@echo off
type type_text.txt
type type_bat.bat
pause</code></pre></div>



<p>この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="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="type.batの結果"><code>type_textの内容@echo off
type_bat続行するには何かキーを押してください . . .</code></pre></div>



<p class="has-border -border02">テキストファイルとバッチファイルの内容が表示されています。</p>



<p class="is-style-icon_bad">改行がされてなく綺麗に表示されていないので、修正します。</p>



<p>batファイルで改行をいれるには「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/11/script_1114-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-kaigyou/">【bat】バッチで改行する方法！echoで改行する方法も教えます！</a>
						<span class="p-blogCard__excerpt">bat（バッチ）ファイルを作成する時に、コマンドが長くなって読みにくい事はないでしょうか？ またechoなどで表示した文字などを、改行して綺麗に表示したい時はありま&#8230;</span>					</div>
				</div>
			</div>
		</div>


<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="type.bat"><code>@echo off
type type_text.txt
echo.
type type_bat.bat
echo.
pause</code></pre></div>



<p>修正したbatファイルでを実行します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>type_textの内容
@echo off
type_bat
続行するには何かキーを押してください . . .</code></pre></div>



<p class="has-border -border02">テキストファイルの内容とバッチファイルの内容が<span class="swl-bg-color has-swl-deep-02-background-color">綺麗に表示</span>されました。</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">typeコマンドとnulでファイルを作成するやり方</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="717" src="https://sugublog.org/wp-content/uploads/2021/10/uninstall_20211010-1-1024x717.jpg" alt="batのtypeコマンド" class="wp-image-4270" srcset="https://sugublog.org/wp-content/uploads/2021/10/uninstall_20211010-1-1024x717.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/10/uninstall_20211010-1-300x210.jpg 300w, https://sugublog.org/wp-content/uploads/2021/10/uninstall_20211010-1-768x538.jpg 768w, https://sugublog.org/wp-content/uploads/2021/10/uninstall_20211010-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">typeコマンド</span></strong>はファイルの中身を表示するほかに、<span class="swl-inline-color has-swl-deep-01-color">ファイルを作成する事も可能</span>です。</span>基礎構文は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>type nul &gt; 新規ファイル名</code></pre></div>



<p>nulとは何もない事を意味します。＞はリダイレクトでファイルに追記したり、新規ファイルを作成する事ができます。</p>



<p>つまりこのbatは内容がない新規ファイルを作成するという意味です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="type-new-file-create.bat"><code>@echo off
type nul &gt; new_file_text.txt
pause</code></pre></div>



<p>上のbatを実行すると同じ階層に「new_file_text.txt」というファイルを新規作成する事が可能です。</p>



<p class="is-style-icon_book">batでtype以外で新規ファイル作成する方法は以下の記事て纏めています。</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/DB0804-3-300x210.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-file-create/">【bat基礎】バッチでファイルを簡単に作成する方法！全３種類ご紹介</a>
						<span class="p-blogCard__excerpt">batファイルでファイルを作成する方法をお伝えします。 ファイルを何個も作成する場合に、batで行えば楽に業務を遂行する事が可能です。 batでファイルを簡単に作成する&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">まとめ：batでtypeコマンドの使い方</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/script_0809-9-1024x576.jpg" alt="スクリプト" class="wp-image-3866" srcset="https://sugublog.org/wp-content/uploads/2021/08/script_0809-9-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-9-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-9-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-9.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">typeコマンド</span></strong>を使用して出来る事をまとめます。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batでtypeを使用して出来る事2選</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>ファイルの中身を表示できる</li>



<li>新規ファイルを作成できる</li>
</ul>
</div></div>



<p><strong><span class="swl-inline-color has-swl-deep-02-color">ファイルの中身を表示するbatファイル</span></strong>の構文</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>type ファイル名</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>type nul &gt; 新規ファイル名</code></pre></div>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="red"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://sugublog.org/wp-content/uploads/2021/03/dog004-150x150.jpeg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/03/dog004-150x150.jpeg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">筆者</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>今回は<strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>の<strong><span class="swl-inline-color has-swl-deep-01-color">typeコマンド</span></strong>で出来る事をまとめました。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-type-command-nul/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチで改行する方法！echoで改行する方法も教えます！</title>
		<link>https://sugublog.org/bat-kaigyou/</link>
					<comments>https://sugublog.org/bat-kaigyou/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sat, 24 Feb 2024 15:53:33 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat 改行]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6409</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/script_1114-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>bat（バッチ）ファイルを作成する時に、コマンドが長くなって読みにくい事はないでしょうか？ またechoなどで表示した文字などを、改行して綺麗に表示したい時はありませんか？ そんなbatファイルの改行について、現役エンジ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/script_1114-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat（バッチ）ファイル</span></strong>を作成する時に、コマンドが長くなって読みにくい事はないでしょうか？</p>



<p>またechoなどで表示した文字などを、改行して綺麗に表示したい時はありませんか？</p>



<p>そんな<span class="swl-bg-color has-swl-deep-01-background-color">batファイルの改行</span>について、現役エンジニアの筆者が解説します。</p>


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


<p class="is-style-big_icon_point">この記事を読めば長くて汚いbatファイルを綺麗にすることが可能です。</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（バッチ）で改行する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="666" src="https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1-1024x666.jpg" alt="code" class="wp-image-4186" srcset="https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1-1024x666.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1-300x195.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1-768x499.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/code_0923-1-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">batで改行するやり方</span></strong>は「＾」をつけることです。</span></p>



<p class="is-style-icon_info">windowsの日本語のキーボードでいう「へ」のところを押せば「＾」が入力できます。</p>



<p>では例を出して改行してみます。</p>



<p>したのようなbatファイルがあるとします。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>set moji=mojiretsu
if /i %moji% == mojiretsu (
echo 文字列はあっています。
)</code></pre></div>



<p>コマンドを改行して下のように修正します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>s^
e^
t^
 moji=mojiretsu
if /i %moji% == mojiretsu (
echo 文字列はあっています。
)</code></pre></div>



<p>このbatファイルを実行するとどちらも同じ結果となります。</p>



<h2 class="wp-block-heading">batのechoで改行する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-3-1024x683.jpg" alt="スクリプト" class="wp-image-3871" srcset="https://sugublog.org/wp-content/uploads/2021/08/script_0809-3-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-3-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-3-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-3.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>echoで空白で改行して綺麗に見せたい場合は、「echo.」など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/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>


<p>改行できる文字と出来ない文字があるので、以下にまとめます。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>改行できる文字</th><th>改行できない文字</th></tr></thead><tbody style="--tbody-th-color--bg:var(--color_deep04);--tbody-th-color--txt:var(--swl-text_color--white)"><tr><td>echo.</td><td>echo@</td></tr><tr><td>echo:</td><td>echo-</td></tr><tr><td>echo;</td><td>echo{</td></tr><tr><td>echo]</td><td>echo}</td></tr><tr><td>echo[</td><td></td></tr><tr><td>echo\</td><td></td></tr><tr><td>echo/</td><td></td></tr><tr><td>echo,</td><td></td></tr></tbody></table><figcaption class="wp-element-caption">echoで改行できる、できない文字一覧表</figcaption></figure>



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



<p>実際に下のようなbatファイルを作成します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
echo ☆☆☆☆☆☆
echo.
echo:
echo;
echo]
echo[
echo\
echo/
echo,
echo ☆☆☆☆☆☆
pause </code></pre></div>



<p>この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="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>☆☆☆☆☆☆








☆☆☆☆☆☆
続行するには何かキーを押してください . . .</code></pre></div>



<p>８行分だけ改行されています。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-kaigyou/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチでwait処理を秒数指定する！ミリ秒単位で指定可能</title>
		<link>https://sugublog.org/bat-wait/</link>
					<comments>https://sugublog.org/bat-wait/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sat, 24 Feb 2024 06:46:47 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat wait]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6406</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/12/dog202112129-1-1024x766.jpg" class="webfeedsFeaturedVisual" /></p>そういったエンジニアの悩みに現役エンジニアの筆者が答えます。 bat(バッチ)でwait処理したい人はこの記事を読んでください。 batでwait処理を秒数指定する batでwait処理を秒数指定する方法をまとめます。  [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/12/dog202112129-1-1024x766.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(バッチ)でwait処理行いたい人</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>bat(バッチ)でwait処理を行い秒数指定したいです。</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>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat(バッチ)でwait処理したい人</span></strong>はこの記事を読んでください。</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でwait処理を秒数指定する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/12/dog202112129-2-1024x683.jpg" alt="wait" class="wp-image-4830" srcset="https://sugublog.org/wp-content/uploads/2021/12/dog202112129-2-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/12/dog202112129-2-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/12/dog202112129-2-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/12/dog202112129-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">batでwait処理を秒数指定する方法</span></strong>をまとめます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>timeout /t 秒数</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/10/カスタマイズ1013-1-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-base-timeout/">【bat基礎】Timeoutの使い方！効かない時の対処法などご紹介</a>
						<span class="p-blogCard__excerpt">時間指定して経過後、続きからbatファイルを動かしたい場合は「Timeout」コマンドを使用します。 batファイルで一時停止させたい場合「Pause」コマンドを使用しますが、&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>これで指定した秒数だけwaitする事ができます。</p>



<p>では３秒間waitした後に処理を続けるbatを作成します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>timeout /t 3</code></pre></div>



<p>この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="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>3 秒待っています。続行するには何かキーを押してください .</code></pre></div>



<p>秒数がカウントされて、その後に処理が再開されます。</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/03/dog004-150x150.jpeg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/03/dog004-150x150.jpeg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">機嫌のいいワンコ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>秒数を数えてくれるので、どのくらい待てばよいのか、本当に動いているのかわかるので安心です。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h2 class="wp-block-heading">wait処理でカウントを表示させない方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="632" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-3-1024x632.jpg" alt="wait" class="wp-image-4560" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-3-1024x632.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-3-300x185.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-3-768x474.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-3.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>カウントさせたくない方法は、先程のコマンドを「>nul」にリダイレクトしましょう。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>timeout /t 3 &gt;nul</code></pre></div>



<p class="is-style-icon_announce">nulはゴミ箱のようなものです。>nullを付けることでメッセージを非表示にする事が可能です。</p>



<p>batを実行してみると、カウントが表示されずに３秒後に後の処理が再開されました。</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">wait処理をその他の方法で行う</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/10/moving1006-2-1024x683.jpg" alt="wait" class="wp-image-4235" srcset="https://sugublog.org/wp-content/uploads/2021/10/moving1006-2-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/10/moving1006-2-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/10/moving1006-2-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/10/moving1006-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>batの上位互換にpowershellというものがあります。</p>



<p>bat上でもpowershellが動きます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>powershell sleep 3</code></pre></div>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>powershell start-sleep -seconds 3</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/2022/01/vitamins_0117-3-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-sleep/">【bat】バッチでsleepコマンドを使用して数秒待つ！失敗する人必見</a>
						<span class="p-blogCard__excerpt">batでsleepコマンドを使用して、ミリ秒単位で処理を止めたい時もあると思います。 今回はsleepコマンドを使用して、batの処理を数秒止めて再開させたいと思います。 ま&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="has-border -border01">このようなコマンドでもbatでwait処理をする事が可能です。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-wait/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチで現在のカレントディレクトリに移動する方法</title>
		<link>https://sugublog.org/bat-current-directory/</link>
					<comments>https://sugublog.org/bat-current-directory/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Fri, 23 Feb 2024 18:53:05 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat カレントディレクトリ]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6394</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/junp2_0907-2-1024x633.jpg" class="webfeedsFeaturedVisual" /></p>batファイルで現在のカレントディレクトリに移動したい時にどうしていますか？ 絶対パスで指定する人もいますが、batファイルを移動した時などはパスを修正しなくてはいけません。 という事で今回はbatで現在のカレントディレ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/junp2_0907-2-1024x633.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">batファイル</span></strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">現在のカレントディレクトリ</span></strong>に移動したい時にどうしていますか？</p>



<p>絶対パスで指定する人もいますが、<span class="swl-marker mark_yellow">batファイルを移動した時などはパスを修正しなくてはいけません。</span></p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_001.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_001.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>どんな環境でも修正なしで実行できるbatファイルが好ましいです。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p class="has-border -border01">という事で今回はbatで現在のカレントディレクトリに移動する方法を、現役エンジニアの筆者がお伝えします。</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">bat(バッチ)で現在のカレントディレクトリを取得する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="503" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-13-1024x503.jpg" alt="取得する" class="wp-image-4420" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-13-1024x503.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-13-300x148.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-13-768x378.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-13.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>まずは<strong><span class="swl-inline-color has-swl-deep-01-color">現在の</span></strong><span class="swl-inline-color has-swl-deep-01-color"><strong>カレントディレクトリを取得する方法</strong></span>をお伝えします。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>%~dp0</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/2022/01/sweets_20220105-3-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-dp0/">【bat】バッチでdp0の使い方！その他のオプションも合わせてご紹介！</a>
						<span class="p-blogCard__excerpt">batファイルで「dp0」の意味と使い方を、現役エンジニアの筆者が教えます！ batでdp0とよくわからない記号が出てきましたがどのように使用するのかわかりません？ batフ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>これでbatファイルが存在するパスを取得する事が可能です。</p>



<p>実際に下の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="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
echo %~dp0
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>


<p>batファイルのカレントディレクトリが表示されています。</p>


<div class="wp-block-image is-style-border">
<figure class="aligncenter size-full"><img decoding="async" width="781" height="258" src="https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ1.png" alt="batでカレントディレクトリに移動する" class="wp-image-6401" srcset="https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ1.png 781w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ1-300x99.png 300w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ1-768x254.png 768w" sizes="(max-width: 781px) 100vw, 781px" /></figure>
</div>


<h2 class="wp-block-heading">bat(バッチ)で現在のカレントディレクトリに移動する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/09/pitapa-1-1024x683.jpg" alt="カレントディレクトリに移動する" class="wp-image-4116" srcset="https://sugublog.org/wp-content/uploads/2021/09/pitapa-1-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/pitapa-1-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/pitapa-1-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/pitapa-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">現在のカレントディレクトリに移動するbatファイル</span></strong>を試します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="current_directory.bat"><code>@echo off
cd /d C:\
dir

cd /d %~dp0
dir

pause</code></pre></div>



<p class="is-style-icon_pen">ディレクトリを移動するコマンドは「cd」です。「dir」コマンドはカレントディレクトリ配下のファイル一覧を表示できます。</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="wp-block-list">
<li>Cディレクトリ配下に移動してファイル一覧を表示します。</li>



<li>その後にカレントディレクトリに移動してファイル一覧を表示します。</li>
</ol>
</div></div>



<p>このbatを実行します。</p>


<div class="wp-block-image is-style-border">
<figure class="aligncenter size-full"><img decoding="async" width="1173" height="828" src="https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ2.png" alt="batでカレントディレクトリに移動する" class="wp-image-6402" srcset="https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ2.png 1173w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ2-300x212.png 300w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ2-1024x723.png 1024w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ2-768x542.png 768w" sizes="(max-width: 1173px) 100vw, 1173px" /></figure>
</div>


<p>Cドライブ配下のファイル一覧が表示されています。その後に<span class="swl-marker mark_orange"><strong>カレントディレクトリ</strong>に移動してファイル一覧が表示されています。</span></p>



<p class="is-style-big_icon_check">このbatファイルを別のディレクトリに移動して実行してみます。</p>


<div class="wp-block-image is-style-border">
<figure class="aligncenter size-full"><img decoding="async" width="1252" height="892" src="https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ3.png" alt="batでカレントディレクトリに移動する" class="wp-image-6403" srcset="https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ3.png 1252w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ3-300x214.png 300w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ3-1024x730.png 1024w, https://sugublog.org/wp-content/uploads/2024/02/bat_カレントディレクトリ3-768x547.png 768w" sizes="(max-width: 1252px) 100vw, 1252px" /></figure>
</div>


<p><span class="swl-marker mark_orange">batファイルの修正なしで<strong>現在のカレントディレクトリ</strong>に移動してファイル一覧が表示されています。</span></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(バッチ)で現在のカレントディレクトリ以外を取得する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-6-1024x683.jpg" alt="カレントディレクトリ以外を取得する" class="wp-image-4563" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-6-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-6-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-6-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-6.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>今回は<strong>現在のカレントディレクトリ</strong>を取得しましたが、他の要素も取得可能です。</p>



<p>その一覧をまとめました。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>オプション</th><th>意味</th></tr></thead><tbody><tr><td>%~d0</td><td>バッチファイルのドライブ名</td></tr><tr><td>%~p0</td><td>バッチファイルのパス</td></tr><tr><td>%~dp0</td><td>バッチファイルのドライブ名＋パス</td></tr><tr><td>%~f0</td><td>バッチファイル名を含めたパス</td></tr><tr><td>%~n0</td><td>バッチファイル名</td></tr><tr><td>%~x0</td><td>バッチファイルの識別子</td></tr><tr><td>%~nx0</td><td>バッチファイル名（識別子含む）</td></tr><tr><td>%~s0</td><td>バッチファイル名を含めたパス（省略形）</td></tr><tr><td>%~a0</td><td>バッチファイルの属性</td></tr><tr><td>%~t0</td><td>バッチファイルの更新日時</td></tr><tr><td>%~z0</td><td>バッチファイルの容量（バイト）</td></tr></tbody></table><figcaption class="wp-element-caption">その他の項目を取得するオプション一覧</figcaption></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-current-directory/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチでexeを実行する方法を教えます！引数など使用あり</title>
		<link>https://sugublog.org/bat-exe/</link>
					<comments>https://sugublog.org/bat-exe/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Fri, 23 Feb 2024 17:25:36 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat exe]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6387</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg" class="webfeedsFeaturedVisual" /></p>batの中でexeファイルを実行したいと思った事はないでしょうか？ batの途中でexeファイルを実行して、何らかの処理をしてから続きのbat処理を行えると業務の幅が広がります。 でも難しそうですよね。でも大丈夫です。  [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg" class="webfeedsFeaturedVisual" /></p>
<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>の中で<strong><span class="swl-inline-color has-swl-deep-01-color">exeファイル</span></strong>を<strong>実行したい</strong></span>と思った事はないでしょうか？</p>



<p>batの途中でexeファイルを実行して、何らかの処理をしてから続きのbat処理を行えると業務の幅が広がります。</p>



<p>でも難しそうですよね。でも大丈夫です。</p>



<p>現役エンジニアの筆者がbatでexeファイルの実行の仕方を丁寧に解説します。</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">batでexeを実行する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/08/DB0804-5-1024x683.jpg" alt="exeを実行" class="wp-image-3844" srcset="https://sugublog.org/wp-content/uploads/2021/08/DB0804-5-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-5-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-5-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-fz u-fz-l">batの中でexeファイルを起動する方法</span></span></strong>をまとめます。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batの中でexeファイルを起動する方法2選</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>callコマンドでexeを起動する</li>



<li>startコマンドでexeを起動する</li>
</ul>
</div></div>



<h2 class="wp-block-heading">callコマンドでexeを起動する</h2>



<p><strong>callコマンド</strong>はbatファイルの中でラベルをつけたところに移動する事が出来たり、外部のファイルを呼び出したりするコマンドですが、<strong><span class="swl-inline-color has-swl-deep-01-color">exe</span>も起動する事</strong>ができます。</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/code_0923-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/bat-call/">【bat】バッチでcallの使い方！gotoやstartとの違いについて</a>
						<span class="p-blogCard__excerpt">batでcallの使い方について教えます。 似たコマンドの「goto」や「start」がありますが、どのように使い分けたら良いのですか？ そう言った疑問にも現役エンジニアの筆&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>callでexeを起動する時の基礎構文をお伝えします。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>call &quot;exeファイルの絶対パス&quot;</code></pre></div>



<p>これでexeを起動する事ができます。</p>



<h2 class="wp-block-heading">startコマンドでexeを起動する</h2>



<p>次は<strong>startコマンド</strong>です。batでstartコマンドはフォルダを開いたり、他のアプリケーションを起動できるので<span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">exe</span></strong>を指定して起動できます。</span></p>



<p>startでexeを起動する時の基礎構文をお伝えします。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>start &quot;&quot; &quot;exeファイルの絶対パス&quot;</code></pre></div>



<p>startの基礎構文はstartの後に「ウィンドウタイトル」が来て、その次に対象を書くので最初の&#8221;&#8221;はウィンドウタイトルを空白にして実行するという意味です。</p>



<p>また環境変数でパスが通っているのであれば、exeファイルを指定するだけでも可能です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>start exeファイル名</code></pre></div>



<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でexeを起動してみる</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="614" src="https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-4-1024x614.jpg" alt="training" class="wp-image-4544" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-4-1024x614.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-4-300x180.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-4-768x461.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-4.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>実際に<span class="swl-bg-color has-swl-deep-02-background-color">batファイルを使用してexeを起動</span>してみます。</p>



<h3 class="wp-block-heading">batのcallコマンドでexeを起動してみる</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
call &quot;C:\Program Files\Google\Chrome\Application\chrome.exe&quot;</code></pre></div>



<p class="is-style-dent_box">「@echo off」について詳しく知りたい方は以下の記事へどうぞ</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-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>


<p>このbatを実行するとgoogle chromeのブラウザが立ち上がります。</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>


<h3 class="wp-block-heading">batのstartコマンドでexeを起動してみる</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
start &quot;&quot; &quot;C:\Program Files\Google\Chrome\Application\chrome.exe&quot;</code></pre></div>



<p class="has-border -border01">このbatを実行してもgoogle chromeのブラウザが立ち上がります。</p>



<p>環境変数が通っているので下のコマンドでも立ち上がります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
start &quot;&quot; &quot;chrome.exe&quot;</code></pre></div>



<h2 class="wp-block-heading">実践：batでexeを引数も使用して起動してみる</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="544" src="https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-6-1024x544.jpg" alt="引数を使用してexe実行" class="wp-image-4583" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-6-1024x544.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-6-300x159.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-6-768x408.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-6.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>今度は<span class="swl-bg-color has-swl-deep-02-background-color">exeを引数を用いて起動</span>してみます。</p>



<h3 class="wp-block-heading">引数指定してcallコマンドでexeを起動</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>call &quot;C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE&quot; test.xlsx</code></pre></div>



<p>excelのexeファイルを絶対パスで指定して、その後にファイル名を引数として実行します。</p>



<p class="has-border -border01">するとtest.xlsxファイルがexcelで開きました。</p>



<h3 class="wp-block-heading">引数指定してstartコマンドでexeを起動</h3>



<p>同じ事をstartでもやってみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>start &quot;&quot; &quot;C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE&quot; 35_php.xlsx</code></pre></div>



<p class="has-border -border01">同じ様にtest.xlsxファイルがexcelで開きました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-exe/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチでsleepコマンドを使用して数秒待つ！失敗する人必見</title>
		<link>https://sugublog.org/bat-sleep/</link>
					<comments>https://sugublog.org/bat-sleep/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Fri, 23 Feb 2024 02:52:06 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat sleep]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6377</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2022/01/vitamins_0117-3-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>batでsleepコマンドを使用して、ミリ秒単位で処理を止めたい時もあると思います。 今回はsleepコマンドを使用して、batの処理を数秒止めて再開させたいと思います。 またsleepコマンドを使用しても想定通りの動き [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2022/01/vitamins_0117-3-1024x683.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">sleep</span></strong>コマンドを使用して、<strong>ミリ秒単位で処理を止めたい</strong>時もあると思います。</p>



<p>今回はsleepコマンドを使用して、batの処理を数秒止めて再開させたいと思います。</p>



<p>また<span class="swl-marker mark_yellow">sleepコマンドを使用しても想定通りの動きをしない場合の対処法</span>も教えます。</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">bat(バッチ)でsleepコマンドのやり方</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/11/programing_school1123-3-1024x576.jpg" alt="batを教えます" class="wp-image-4580" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-3-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-3-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-3-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-3.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>での<strong><span class="swl-inline-color has-swl-deep-01-color">sleep</span></strong>コマンドは処理を<strong>決めた秒数だけ一時停止</strong>する効果があります。</span></p>



<p>基礎構文を下に教示します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>sleep 秒数</code></pre></div>



<p>これを入れるだけでbat処理を秒数だけ一時停止する事が可能です。</p>



<h2 class="wp-block-heading">bat(バッチ)のsleepコマンドと同じ動きをするコマンド</h2>



<p class="is-style-big_icon_caution">しかしsleepコマンドはwindowsの環境などで動かない場合が存在します。</p>



<p>そういった場合には「timeout」コマンドを使用すれば同じ効果が得られます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>timeout /t 秒数</code></pre></div>



<p>timeoutコマンドのその他のオプションなどは以下の記事でまとめています。</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/カスタマイズ1013-1-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-base-timeout/">【bat基礎】Timeoutの使い方！効かない時の対処法などご紹介</a>
						<span class="p-blogCard__excerpt">時間指定して経過後、続きからbatファイルを動かしたい場合は「Timeout」コマンドを使用します。 batファイルで一時停止させたい場合「Pause」コマンドを使用しますが、&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">bat(バッチ)でsleepコマンドが動かない時の対処法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-1024x683.jpg" alt="エラー対処法" class="wp-image-4564" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-7.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p class="is-style-big_icon_caution">しかしsleepコマンドもtimeoutコマンドもwindowsの環境などで動かない場合が存在します。</p>



<p>そんな時の対処法を教えます。</p>



<p>「powershell sleep」コマンドを使用すれば可能です。</p>



<p class="is-style-icon_book">powershellとはwindowsのbatの拡張版のようなものです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>powershell sleep 秒数</code></pre></div>



<p>これなら確実に動きますので、「powershell sleep」のコマンドを推奨します。</p>



<h2 class="wp-block-heading">bat(バッチ)のsleepでミリ秒を指定する方法</h2>



<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>の<strong><span class="swl-inline-color has-swl-deep-01-color">sleep</span></strong>で<strong>ミリ秒を指定</strong>したい時</span>もあると思います。</p>



<p>そんな時は以下の様に記述してください。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>powershell sleep 0.001</code></pre></div>



<p>これでミリ秒単位でbat処理を一時停止する事が可能です。</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のsleepで処理を止める</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-1-1024x683.jpg" alt="training" class="wp-image-4541" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-1-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-1-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-1-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>では実際にbatのsleepを使用してみます。</p>



<h3 class="wp-block-heading">sleepを使用したbatファイル説明</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off

echo ①この行が最初の行です!
powershell sleep 5
echo ②この行が最後の行です！

pause</code></pre></div>



<p class="is-style-icon_pen">「@echo off」はbatファイルの1行目に記述するおまじないだと思ってください。</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-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>


<p>最初の文字列を表示してから<span class="swl-marker mark_orange">「powershell speep」コマンドで5秒待ってから最終行の文字列を表示します。</span></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/bat-base-echo/">【bat基礎】バッチファイルのechoで文字列表示する！エラー対処法あり</a>
						<span class="p-blogCard__excerpt">batファイルで「echo」の使い方を今回は行います。 「echo」コマンドは指定の文字列を標準出力に表示する事が出来るコマンドです。 ただ文字列を表示するだけでなく、フ&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="is-style-icon_pen">最後のpauseコマンドは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/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>


<h3 class="wp-block-heading">sleepを使用したbatファイルを実行した結果</h3>



<p>ではこの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="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>①この行が最初の行です!</code></pre></div>



<p>最初の文字列が表示されます。</p>



<p>5秒間待ちが発生します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>①この行が最初の行です!
②この行が最後の行です！
続行するには何かキーを押してください . . .</code></pre></div>



<p>5秒後に最後の文字列が表示されました。</p>



<h2 class="wp-block-heading">まとめ：bat(バッチ)でsleepコマンドのやり方</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-1-1024x683.jpg" alt="batを教えます" class="wp-image-4578" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-1-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-1-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-1-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_school1123-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">bat</span></strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">sleep</span></strong>を使用すれば<strong>処理を指定した時間だけ止める</strong>事ができる。</span></p>



<p>sleepのコマンドは以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>sleep 秒数</code></pre></div>



<p>その他のコマンドで<span class="swl-bg-color has-swl-deep-02-background-color">sleepと同じ効果のあるコマンド</span>はtimeoutで構文は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>timeout /t 秒数</code></pre></div>



<p>環境によっては<span class="swl-marker mark_yellow">sleepもtimeoutも動かない時があるので</span>、powershellコマンドを使用するのが推奨です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>powershell sleep 秒数</code></pre></div>



<p>秒数の箇所はミリ秒単位でも指定可能。1ミリ秒を指定する時のやり方です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>powershell sleep 0.001</code></pre></div>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-sleep/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【bat】バッチでcallの使い方！gotoやstartとの違いについて</title>
		<link>https://sugublog.org/bat-call/</link>
					<comments>https://sugublog.org/bat-call/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Thu, 22 Feb 2024 16:18:48 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[bat call]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=6357</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/code_0923-4-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>batでcallの使い方について教えます。 そう言った疑問にも現役エンジニアの筆者が答えていきます。 batでcallの使い方を学んで、「goto」や「start」などの使い分けが出来れば、自由自在なbatファイルを設計 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/09/code_0923-4-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>
<p>batでcallの使い方について教えます。</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でcallの使い方に疑問を持っているワンコ</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>似たコマンドの「goto」や「start」がありますが、どのように使い分けたら良いのですか？</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>


<p class="is-style-big_icon_check"><strong>bat</strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">call</span></strong>の使い方を学んで、「goto」や「start」などの使い分けが出来れば、自由自在なbatファイルを設計する事ができます。</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でのcallの基本の使い方</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/script_0809-8-1024x576.jpg" alt="call" class="wp-image-3865" srcset="https://sugublog.org/wp-content/uploads/2021/08/script_0809-8-1024x576.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-8-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-8-768x432.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/script_0809-8.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-marker mark_orange">batでのcallの基本の使い方</span></strong>としては以下の2点になります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batでのcallの基本の使い方</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>ラベルに飛ぶ</li>



<li>外部ファイルを呼び出す</li>
</ul>
</div></div>



<h3 class="wp-block-heading">callでラベルまで飛ぶ</h3>



<p><strong><span class="swl-inline-color has-swl-deep-01-color">callでラベルまで飛ぶ</span></strong>事が可能です。</p>



<p class="is-style-icon_book">ラベルとはbatファイルの中につける印のようなものです。ラベルの名前は自由につける事ができます。</p>



<p>ラベルを作成してそこまで飛びます。<span class="swl-marker mark_orange">そこから最後まで行くと「call」の後からまた処理が始まります。</span></p>



<h3 class="wp-block-heading">callで外部ファイルを呼び出す</h3>



<p><strong><span class="swl-inline-color has-swl-deep-02-color">callで外部ファイルを呼び出す</span></strong>事が可能です。</p>



<p>外部のファイルを表示したり実行することもできます。</p>



<p>外部テキストファイルを開いたとして、<span class="swl-marker mark_blue">閉じると「call」の後からまた処理が始まります。</span></p>



<p><span class="swl-marker mark_blue">他のbatファイルを開いたとしたら、そのbatファイルの処理が終われば「call」の後からまた処理が始まります。</span></p>



<h2 class="wp-block-heading">実践でcallを使用してみる</h2>


<div class="wp-block-image is-style-border">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="512" src="https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-2-1024x512.jpg" alt="training" class="wp-image-4542" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-2-1024x512.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-2-300x150.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-2-768x384.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_shool1121-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">callでラベルまで飛ぶか実践する</h3>



<p>では実際に<span class="swl-marker mark_orange">batファイルで<strong><span class="swl-inline-color has-swl-deep-01-color">callコマンド</span></strong>を試してみます。</span>まずはラベルまで飛ぶ動きの確認です。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>callでラベルまで飛ぶ動き</span></div><div class="cap_box_content">
<ol class="wp-block-list">
<li>ラベルをつけてそこに飛ぶ事を確認</li>



<li>batの最後まで行く事を確認</li>



<li>callの後から処理が再開される事を確認</li>
</ol>
</div></div>



<p>下のbatファイルを記述して実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call_1.bat"><code>@echo off

echo ①この行が最初の行です!

REM lastのラベルまで飛びます
call :last

echo まずこの行が表示されなければ成功

:last
echo ②この行が最後の行です！

pause</code></pre></div>



<h3 class="wp-block-heading">callでラベルまで飛ぶbatの説明</h3>



<p class="is-style-icon_pen">「@echo off」は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/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>


<p>ラベルをつけるには「:」の後に自分の好きな名前をつけます。ここでは「:last」というラベルをつけています。</p>



<p>「call :last」コマンドでラベルまで飛ぶ事ができます。</p>



<p class="is-style-icon_pen">最後の「pause」は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/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>では実際に実行してみます。</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>


<h3 class="wp-block-heading">callでラベルまで飛ぶbatの実行結果説明</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call_1.batの結果.log"><code>①この行が最初の行です!
②この行が最後の行です！
まずこの行が表示されなければ成功
②この行が最後の行です！
続行するには何かキーを押してください . . .</code></pre></div>



<p>まず初めのechoの文字列が表示されます。</p>



<p>ラベルまで飛んだので２つ目のechoの文字列は表示されず、最後の文字列が表示されています。</p>



<p><span class="swl-marker mark_orange"><strong>bat</strong>の最後まで行ったので<strong>call</strong>の後から処理が開始され</span>、２つ目の文字列が表示されます。</p>



<p>また最後の文字列が表示されて、batが終わっています。</p>



<p class="has-border -border01">違和感のある動きですが、想定通りの動きになりました。</p>



<h3 class="wp-block-heading">callで外部ファイルを呼び出してみる</h3>



<p>次に<span class="swl-marker mark_blue">callで外部ファイルを呼び出してみましょう。</span></p>



<p>まずは下のbatファイルを作成します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call_2.bat"><code>@echo off

echo ①この行が最初の行です!

REM 外部ファイルを呼び出します
call another_file.txt

call another_file.bat

echo ②この行が最後の行です！

pause</code></pre></div>



<p>このbatファイルと同じ階層に「anather_file.txt」と「anather_file.bat」を作成します。内容は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="anather_file.txt"><code>this is another text file!</code></pre></div>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="another_file.bat"><code>@echo off
echo this is another bat file!</code></pre></div>



<h3 class="wp-block-heading">callで外部ファイルを呼び出すbatの説明</h3>



<p>まずは最初の文字を表示します。</p>



<p>その後に<span class="swl-marker mark_blue">callで外部のテキストファイルを呼び出しています。</span></p>



<p>その後にまた<span class="swl-marker mark_blue">callで外部batファイルを呼び出しています。</span></p>



<p>そして最後の文字列を表示して終了です。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_001.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_001.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>ではこのbatを実行してみます。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h3 class="wp-block-heading">callで外部ファイルを呼び出すbatの実行結果説明</h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call_2.batの実行結果前半"><code>①この行が最初の行です!</code></pre></div>



<p>まずは最初の文字列が表示されて、外部テキストファイルが開きました。</p>



<p>開いたテキストを閉じます。<span class="swl-marker mark_blue">callで外部テキストファイルを呼び出した後から処理が継続</span>して、次の外部batファイルのechoコマンドが実行されて、<span class="swl-marker mark_blue">このプロンプト上に文字列が表示</span>されています。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call_2.batの実行結果中盤"><code>this is another bat file!</code></pre></div>



<p><span class="swl-marker mark_green">外部batファイルが終わりまで行った</span>ので、<span class="swl-marker mark_blue">呼び出したcallの後から処理が継続</span>して最後の文字列が表示されました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call_2.batの実行結果後半"><code>②この行が最後の行です！
続行するには何かキーを押してください . . .</code></pre></div>



<p class="has-border -border01">想定通りの動きになりました。</p>



<h2 class="wp-block-heading">callとgotoの違いについて</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="766" src="https://sugublog.org/wp-content/uploads/2021/09/jump0907-3-1024x766.jpg" alt="callとgoto" class="wp-image-4148" srcset="https://sugublog.org/wp-content/uploads/2021/09/jump0907-3-1024x766.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/jump0907-3-300x225.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/jump0907-3-768x575.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/jump0907-3.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>

<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="red"><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ファイルでcallとgotoの違いがわからないエンジニアワンコ</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>batファイルでラベルに移動するコマンドで「goto」があるけど「call」とどう違うの？</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h3 class="wp-block-heading">callとgotoの違いについての説明</h3>



<p>では<strong><span class="swl-inline-color has-swl-deep-01-color">callとgotoの違い</span></strong>は何なのでしょうか？</p>



<p>先程の<span class="swl-marker mark_blue">「call」はbatの終わりまで行くとcallの下からまた処理が始まります</span>が、<span class="swl-marker mark_yellow">「goto」はそのまま終わる</span>ところが違いです。</p>



<p>先程のbatファイルの「call」を「goto」に変えて実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call→goto_1.bat"><code>@echo off

echo ①この行が最初の行です!

REM lastのラベルまで飛びます
goto :last

echo まずこの行が表示されなければ成功

:last
echo ②この行が最後の行です！

pause</code></pre></div>



<h3 class="wp-block-heading">callをgotoに修正して実行した結果</h3>



<p><span class="swl-bg-color has-swl-deep-04-background-color">callをgotoに修正</span>して実行した結果です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>①この行が最初の行です!
②この行が最後の行です！
続行するには何かキーを押してください . . .</code></pre></div>



<p>初めの文字列が表示されたあと、<span class="swl-marker mark_orange">ラベルに飛び最後の文字列が表示されて終了</span>しています。</p>



<p class="has-border -border02">「call」と「goto」の違いは飛んだあと戻ってくるか来ないかです。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_002-e1624111291945.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_002-e1624111291945.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>１つのbatの中であれば「call」よりも「goto」の方が使いやすいですね。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h2 class="wp-block-heading">callとstartの違いについて</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-7-1024x683.jpg" alt="callとstart" class="wp-image-4093" srcset="https://sugublog.org/wp-content/uploads/2021/08/school_08-7-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/school_08-7-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/school_08-7-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/school_08-7.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>

<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="yellow"><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ファイルでcallとstartの違いがわからないエンジニアワンコ</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>batで外部ファイルを呼び出すコマンドで「start」があるけど「call」とどう違うの？</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h3 class="wp-block-heading">callとstartの違いについての説明</h3>



<p>では<strong><span class="swl-inline-color has-swl-deep-01-color">callとstartの違い</span></strong>は何なのでしょうか？</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>callとstartの違い</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>外部ファイルの実行終了を待たない</li>



<li>外部batファイルは別ウィンドウで開く</li>
</ul>
</div></div>



<p>先程の<span class="swl-marker mark_blue">「call」は外部ファイルが閉じられたり、外部batが終了するまで待ち、そのあとcallの下から処理が再開</span>しますが、<span class="swl-marker mark_yellow">「start」は外部の終了を待たない事</span>が違いです。</p>



<p>また<span class="swl-marker mark_blue">外部batファイルは別ウィンドウを開いて実行</span>します。</p>



<p>先程のbatファイルの「call」を「start」に変えて実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain" data-file="bat-call→start_1.bat"><code>@echo off

echo ①この行が最初の行です!

REM 外部ファイルを呼び出します
start another_file.txt

start another_file.bat

echo ②この行が最後の行です！

pause</code></pre></div>



<h3 class="wp-block-heading">callをstartに修正して実行した結果</h3>



<p><span class="swl-bg-color has-swl-deep-02-background-color">callをstartに修正して実行</span>した結果です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>①この行が最初の行です!
②この行が最後の行です！
続行するには何かキーを押してください . . .</code></pre></div>



<p>外部テキストファイルが開かれて、外部batファイルも別ウィンドウで開かれています。</p>



<p>初めの文字列が表示されたあと、外部ファイルが閉じるのを待たずして、一気に最後の文字列が表示されて終了しています。</p>



<p class="has-border -border02">「call」と「start」の違いは外部ファイルの処理を待つか待たないか。また外部batを別ウィンドウで表示するかしないかになります。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-right" 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/mine_white_002-e1624111291945.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/mine_white_002-e1624111291945.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">管理人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>外部ファイルの終了を待つcallが使いやすいです。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<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でcallの使い方！gotoやstartとの違いについて</h2>


<div class="wp-block-image is-style-photo_frame">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="503" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-5-1024x503.jpg" alt="サーバー" class="wp-image-4412" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-5-1024x503.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-5-300x148.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-5-768x378.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-fz u-fz-l">batでのcallの基本の使い方</span></span></strong>としては以下の2点になります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batでのcallの基本の使い方</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>ラベルに飛ぶ</li>



<li>外部ファイルを呼び出す</li>
</ul>
</div></div>



<p>gotoとの違いはラベルに飛んだ後callの下から処理が再開する。</p>



<p class="has-border -border01">callはラベルに飛ぶなどそのbatで完結する場合にはふさわしくない。「goto」コマンドを使用しましょう！</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/11/Server_20211109-10-300x153.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-goto/">【bat】バッチファイルでgotoを使い、好きな位置まで移動する方法</a>
						<span class="p-blogCard__excerpt">batファイルで好きな位置まで移動できると色々と便利です。 そんな事出来ないと思っていませんか？ 「goto」コマンドを使用すれば以外と簡単に可能だったので、ここに覚&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>callで外部ファイルを呼び出すときはstartと違い、外部ファイルが閉じたり処理が終了するのを待つので扱いやすい</p>



<p class="has-border -border02">外部ファイルを呼び出すときは「call」を使用しましょう！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-call/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
