忍者ブログ
プログラミングとか日常とかの覚書っぽいなにか
[7] [6] [5] [4] [3] [2] [1]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

コマンドプロンプトでのファイル操作やエクスプローラのファイル検索をするときに、ファイルを一括で指定できるワイルドカード。Windows APIのFindFirstFile関数でも同じことが可能ですが、どうやらこのフィルタ機能を過信するのも良くないようです。


アスタリスク「*」やクエスチョンマーク「?」はワイルドカードと呼ばれ、「*」を0文字以上の任意の文字、「?」を任意の1文字を表す文字として、複数のファイルを指定するときのフィルタとして使用することができます。
例えば、エクスプローラのファイル検索で

ab*.txt

と入れれば、ファイル名が「ab」で始まり、拡張子が「txt」のファイルを検索することができますし、コマンドプロンプトで

del proc??.log

と打てば、現在のディレクトリのにあるファイルのう、ファイル名が「proc」で始まり、ファイル名の長さが6文字+拡張子「log」のファイルをすべて削除することができるというわけです。

FindFirstFile関数も同様の指定でファイル列挙ができるので、ソフト開発の場でも非常によく使われるのですが、1つ忘れられがちなのが、これらのワイルドカードによるフィルタ指定が、一般的な「長い形式のファイル名」だけでなく、同時に「短い形式のファイル名」もチェックの対象となるという事実。

Windowsでは、256文字までのファイル名をつけることができるのですが、そのような「長い形式のファイル名」に対応していない古いソフトウェアでも動作するように、ファイル名8文字+拡張子3文字を超える長さのファイル名を持つすべてのファイルは、通常のファイル名のほかに「短い形式のファイル名」が付けられています。
これは俗に「8.3形式」とも呼ばれていて、その名が示すとおりに「ファイル名8文字+拡張子3文字」となります。

例えば、

test200090621.txt

という名前のファイルを作ったとき、このファイルにはこの名前とは別に

TEST20~1.TXT

という短い形式の名前が付けられます。Windows XPのエクスプローラでは短いファイル名を見ることができないようですが、コマンドプロンプトでディレクトリを移動して

dir /x

というように「/x」オプション付きのdirコマンドで調べることができます。

短い形式のファイル名の命名規則はちょっと複雑で、最初の4つまでは単に「~」の後ろの数字が増えるだけですが、それ以降は別の形になります。試しに順に作ってみたところ

test200090621.txt → TEST20~1.TXT
test200090622.txt → TEST20~2.TXT
test200090623.txt → TEST20~3.TXT
test200090624.txt → TEST20~4.TXT
test200090625.txt → TE6848~1.TXT
test200090626.txt → TE6C48~1.TXT

という感じになりました。
さて、ここで、最初の文字が「t」で、ファイル名の最後が「1.txt」のファイルを削除したいとすると、コマンドプロンプトなら

del t*1.txt

とすればよいはず。普通ならば、これで最初の「test200090621.txt」だけが削除されるだろうと思ってしまうのですが、実際には

test200090621.txt
test200090625.txt
test200090626.txt

の3つが削除されてしまうという結果に。つまり短いファイル名である

TE6848~1.TXT
TE6C48~1.TXT

が「t*1.txt」というフィルタに引っかかってしまって、削除されてしまったというわけです。

バッチファイルなどではワイルドカード指定をすることは非常に多いですし、プログラム中でもFindFirstFileで特定のフィルタ文字を指定することはよくあることなのですが、それが必ずしも目的のファイルだけを取ってきてくれるとは限らないということのようです。ワイルドカード指定で抽出された中から、さらに自前のフィルタ処理を行うことが必要ということを肝に銘じておいたほうがよさそうです。

長くなりましたが今回はこの辺で。

拍手

PR

コメント
bracelet replica van cleef alhambra
cartierlovejesduas Que idéia é essa de briga? Até parece que não sabem que vocês são um casal exemplar. Tudo na maior paz e com muito amor! Adoro vocês!
bracelet replica van cleef alhambra http://www.buyjewelrywebsite.org/tag/fake-van-cleef-arpels-bracelet
【2017/06/09 01:37】 NAME[bracelet replica van cleef alhambra] WEBLINK[URL] EDIT[]
replica gold love bangle
cartierlovejesduas Cheers gents, full house after 26 minutes – unlikely to put a better tip up all season
replica gold love bangle http://www.latestbracelet.com/
【2017/06/09 01:37】 NAME[replica gold love bangle] WEBLINK[URL] EDIT[]
datejust rolex oyster perpetual
cartierlovejesduas Right on. I can’t go back to windows anymore after purchasing my chromebook. Everything feels so integrated and simple. It used to take me hours on end to re-install all my programs for Windows, not gonna waste anymore of my time with a Windows Machine. Chromebook has alternatives that are just as good IF not better than Microsoft’s flagship programs.
datejust rolex oyster perpetual http://www.cheapwatchshop.cn/
【2017/06/09 01:38】 NAME[datejust rolex oyster perpetual] WEBLINK[URL] EDIT[]
replica the oyster perpetual datejust
cartierlovejesduas I feel threatened by you, Brett. According to YOUR dictate, this should be the last thing you read, as you are a “born child”. What you said doesn’t make sense. There are literally millions of people who would give their last cent to adopt a child, one of those you say the government should help terminate. It has nothing to do with a woman’s body-she made her choice when decided to be intimate. If her life is truly endangered, then abortion is legitimate. If it’s to take care of an “inconvenience”, let the child live and make someone else happy. Our country is too selfish and self-centered. The government is helping the lazy along by giving them too many things to keep them fettered. Your eyes will be opened, and probably when it’s too late.
replica the oyster perpetual datejust http://www.movement-watch.cn/
【2017/06/09 01:38】 NAME[replica the oyster perpetual datejust] WEBLINK[URL] EDIT[]
bracelets imitation cartier love
cartierlovejesduas 51. It also means that it’s “Your Fault” when you recognise your stepmother slamming the cardoor (without watchting of course) and that she gets annoyed because she doesn’t believe you.
bracelets imitation cartier love http://www.bestcbangs.in/
【2017/06/09 01:38】 NAME[bracelets imitation cartier love] WEBLINK[URL] EDIT[]
replica rolex oyster perpetual datejust
cartierlovejesduas Brookfield Power made a little less money with their hydroelectric plants in 2007 than 2006 due to low water levels caused by drought. The upside to hydro is your fuel – water – is free. The downside is it might not rain for six months. Maybe that was the problem with Algonquin.
replica rolex oyster perpetual datejust http://www.movement-watch.com/tag/fake-rolex-datejust-oyster-perpetual
【2017/06/09 01:38】 NAME[replica rolex oyster perpetual datejust] WEBLINK[URL] EDIT[]


コメントフォーム
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード
  Vodafone絵文字 i-mode絵文字 Ezweb絵文字


トラックバック
この記事にトラックバックする:


忍者ブログ [PR]
プロフィール
HN:
はむぱい
職業:
ソフト作ったりしてる人
Twitter
最新CM
[06/09 replica rolex oyster perpetual datejust]
[06/09 bracelets imitation cartier love]
[06/09 replica the oyster perpetual datejust]
[06/09 datejust rolex oyster perpetual]
[06/09 replica gold love bangle]
カレンダー
02 2024/03 04
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
ブログ内検索
あ~いい漢字