<?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>ファイル存在チェック &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%AD%98%E5%9C%A8%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Sat, 24 Feb 2024 16:20:20 +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>ファイル存在チェック &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【bat基礎】ファイル存在チェックする方法!existsを使用します！</title>
		<link>https://sugublog.org/bat-file-exist-check/</link>
					<comments>https://sugublog.org/bat-file-exist-check/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sun, 26 Nov 2023 13:34:38 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[ファイル存在チェック]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5999</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-9-1024x427.jpg" class="webfeedsFeaturedVisual" /></p>batを使用していてファイル存在チェックしたい場合のやり方をご紹介します。existsを使用します。 もしファイルが存在したら〇〇、存在しなければ作成するといった場合の処理になります。 こういった条件で場合分け出来るか出 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-9-1024x427.jpg" class="webfeedsFeaturedVisual" /></p>
<p>batを使用していて<strong><span class="swl-inline-color has-swl-deep-01-color">ファイル存在チェック</span></strong>したい場合のやり方をご紹介します。existsを使用します。</p>



<p>もしファイルが存在したら〇〇、存在しなければ作成するといった場合の処理になります。</p>



<p>こういった条件で場合分け出来るか出来ないかで、<span class="swl-marker mark_orange"><strong>batファイルのレベル</strong>が変わってきます。</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>

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


<p class="is-style-icon_announce">batファイルの「if」とオプションの機能を使用して行えます。</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のexistsでファイル存在チェックする方法の基本</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><span class="swl-marker mark_orange"><strong>bat</strong>で</span><strong><span class="swl-marker mark_orange"><span class="swl-inline-color has-swl-deep-01-color">existsを使用して</span></span></strong><span class="swl-marker mark_orange"><strong><span class="swl-inline-color has-swl-deep-01-color">ファイルの存在をチェックする方法</span></strong>を以下に示します。</span></p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>if [not] exists ファイル名 (
コマンド )</code></pre></div>



<p>存在しない場合は<span class="swl-bg-color has-swl-deep-01-background-color">「not」</span>を付けるだけです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>if not exists ファイル名 (
コマンド )</code></pre></div>



<p>elseをつけて条件に合わない場合の処理も追加しても良いです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>if exist ファイル名 (
存在する時のコマンド
)else (
その他の時のコマンド
)</code></pre></div>



<p>その他の「if」の使い方は以下の記事でまとめています。</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/PC223-min-scaled.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-if/">【bat】if文を詳しく説明します。複数条件や数値、文字列の比較まで</a>
						<span class="p-blogCard__excerpt">batファイルで条件分岐したい時には「if」構文を使用します。今回はifを使い方をまとめます。 batファイルに限らずプログラミングの条件分岐で処理を変える事は、業務を&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">実践でファイル存在チェックする</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="traning" 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>では<span class="swl-marker mark_orange"><strong>実践</strong>で<strong><span class="swl-inline-color has-swl-deep-01-color">ファイル存在チェック</span></strong>をします。</span></p>



<p>下のコードのbatファイルと同じフォルダに「ファイル1.txt」のファイルを作成します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
if exist ファイル1.txt (
 echo ある
 )else (
  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>



<p class="is-style-big_icon_caution">ここで日本語が文字化けしておかしな文字になった人もいるかと思います。</p>



<p>そういった場合の対処法は、ファイルを名前を付けて保存して文字コードを「ANSI」にして上書き保存する事です。</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/kaizen1-2-min-scaled.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-garbled-japanese-characters/">【bat】バッチファイルで日本語が文字化けする原因と解決策</a>
						<span class="p-blogCard__excerpt">バッチファイルを使用していて日本語が文字化けする時はないでしょうか？ その原因と解決策を現役エンジニアの筆者が教えます。一瞬で簡単にできるので試してください。&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">ファイル存在チェックでなければ作成するbat</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-marker mark_orange">実際<strong>実務で使用されるbatファイル</strong>をご紹介します。</span></p>



<p>ファイル存在チェックしてなければ作成するbatファイルです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
if not exist ファイル1.txt (
 echo ないので作成します
 echo &gt; ファイル1.txt
 )</code></pre></div>



<p>「not exist」でファイルが無ければの条件にしています。</p>



<p>コマンドとしてファイル作成しています。今回は「echo」コマンドを使用してファイル作成しています。</p>



<p class="is-style-icon_pen">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/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>


<p>同じフォルダの「ファイル1.txt」があれば削除してから、このbatファイルを実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>ないので作成します
続行するには何かキーを押してください . . .</code></pre></div>



<p>想定通り「ファイル1.txt」が作成されたと思います。</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>


<h2 class="wp-block-heading">batでファイルを扱う関連記事</h2>



<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/11/Server_20211109-19-300x148.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-filename-get/">【bat基礎】ファイル名を取得する様々な方法！簡単に取得可能です</a>
						<span class="p-blogCard__excerpt">batファイルでファイル名を取得する方法が分かりません。実行しているファイルやカレントディレクトリにあるファイル名を取得したい！ batファイルでファイル名を取得す&#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/11/programing_shool1121-4-300x180.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-move/">【bat基礎】ファイル移動するやり方！ファイル名変更なども行えます！</a>
						<span class="p-blogCard__excerpt">batファイルでファイル移動する場合のやり方を今回行います。 また移動先に同じファイル名があった時の対処法なども、現役エンジニアの筆者が一緒に伝えます。 moveコマ&#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/12/結婚1209-2-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/bat-file-delete/">【bat】ファイルを削除する方法！エラー対処法あり</a>
						<span class="p-blogCard__excerpt">様々なスクリプトでファイルを削除する方法が違うので、batでファイルを削除する方法を教えて欲しいです。 またファイル削除する前に確認を入れたいです。 問答無用で削&#8230;</span>					</div>
				</div>
			</div>
		</div>]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-file-exist-check/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
