%~dp0 とは何ですか? また、どのように機能しますか? 質問する

%~dp0 とは何ですか? また、どのように機能しますか? 質問する

非常に便利で%~dp0、バッチ ファイルの移植性を高めるためによく使用しています。

しかし、ラベル自体は私には非常に不可解に思えます... は何を~していますか?dpドライブとパスを意味しますか? は0%0ファイル名を含むバッチ ファイルへのパスを参照していますか?

それとも単に奇妙なラベルなのでしょうか?

また、それが文書化された機能なのか、それとも廃止される可能性のある機能なのかも知りたいです。

ベストアンサー1

通話

for /?

コマンドラインでは、この構文に関するヘルプが表示されます (FOR の外部でも使用できますが、これはヘルプが見つかる場所にすぎません)。

さらに、FOR 変数参照の置換が強化されました。次のオプションの構文を使用できるようになりました。

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

修飾子を組み合わせることで複合的な結果を得ることができます。

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

上記の例では、%I と PATH は他の有効な値に置き換えることができます。%~ 構文は有効な FOR 変数名で終了します。%I のように大文字の変数名を選択すると、読みやすくなり、大文字と小文字を区別しない修飾子との混乱を避けることができます。

f「フル パス名」、dドライブ文字、pパスなど、使用できる文字はさまざまで、組み合わせることもできます。%~は各シーケンスの始まりであり、数字はIそれがパラメータで機能することを示します%I( は%0、想定どおり、バッチ ファイルの完全な名前です)。

おすすめ記事