header_if_exist

【Cmd指令教學】IF EXIST檢查檔案/資料夾是否存在


Deprecated: Return type of TagFilterNodeIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/naomindc/public_html/stylengineer/wp-content/plugins/easy-table-of-contents/includes/vendor/ultimate-web-scraper/tag_filter.php on line 1149

Deprecated: Return type of TagFilterNodeIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/naomindc/public_html/stylengineer/wp-content/plugins/easy-table-of-contents/includes/vendor/ultimate-web-scraper/tag_filter.php on line 1159

Deprecated: Return type of TagFilterNodeIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/naomindc/public_html/stylengineer/wp-content/plugins/easy-table-of-contents/includes/vendor/ultimate-web-scraper/tag_filter.php on line 1154

Deprecated: Return type of TagFilterNodeIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/naomindc/public_html/stylengineer/wp-content/plugins/easy-table-of-contents/includes/vendor/ultimate-web-scraper/tag_filter.php on line 1144

Deprecated: Return type of TagFilterNodeIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/naomindc/public_html/stylengineer/wp-content/plugins/easy-table-of-contents/includes/vendor/ultimate-web-scraper/tag_filter.php on line 1139

Deprecated: Return type of TagFilterNodeIterator::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/naomindc/public_html/stylengineer/wp-content/plugins/easy-table-of-contents/includes/vendor/ultimate-web-scraper/tag_filter.php on line 1164

語法:IF EXIST 檔案路徑 (Action 1) ELSE (Action 2)

程式碼範例:

::Step1:定義變數(資料夾or檔案路徑)set MydirPath=”D:\BLOG\MyDir”set MyfilePath=”D:\BLOG\MyFile.txt”set NotExistFilePath=”D:\BLOG\NotExistFile.txt”
::Step2:由路徑檢查資料夾or檔案是否存在IF EXIST %MydirPath% (echo dir”MyDir” exist) ELSE (echo dir”MyDir” not exist)IF EXIST %MyfilePath% (echo file”MyFile” exist) ELSE (echo file”MyFile” not exist)IF EXIST %NotExistFilePath% (echo file”NonExistFile” exist) ELSE (echo file”NonExistFile” not exist)pause

!注意事項:

  • ::(statement) 表示該行為註解
  • 定義變數格式:set name=value,等號前後不能有空格,錯誤示範:set MydirPath = “D:\BLOG\MyDir”
  • IF EXIST要搭配使用,不能單獨使用EXIST
  • 最後要加上pause指令,否則cmd執行完畢會自動跳出,無法觀察output

Test Case:
D:/BLOG路徑下存在 MyDir & MyFile.txt,不存在NotExistFile.txt