web-dev-qa-db-ja.com

Windowsバッチファイルで%〜d0とはどういう意味ですか?

次の変数を定義したバッチファイルを見ています。

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0
  • %~d0または%~p0は実際にはどういう意味ですか?
  • 現在のディレクトリ、ドライブ、スクリプトに対するパラメータなどの一連の既知の値はありますか?
  • 私が使用できる他の類似のショートカットはありますか?
320
Chris Smith

マジック変数%nには、ファイルの呼び出しに使用される引数が含まれます。%0は、batファイル自体へのパスです、%1は2番目以降の最初の引数、%2は2番目以降の引数です。

引数は多くの場合ファイルパスであるため、パスの一部を抽出するための追加の構文があります。 ~dはドライブ、~pはパス(ドライブなし)、~nはファイル名です。 ~dpがドライブ+パスになるように、これらを組み合わせることができます。

したがって、%~dp0はバットで非常に役立ちます。実行中のバットファイルが存在するフォルダーです。

ファイルに関する他の種類のメタ情報も取得できます。~tはタイムスタンプ、~zはサイズです。

すべてのコマンドラインコマンドのリファレンスについては、 here を参照してください。チルダマジックコードは for で説明されています。

496
JacquesB

それらは強化された変数置換です。それらはバッチファイルで使用される%N変数を修正します。あなたがWindowsでバッチプログラミングに興味があるなら、かなり役に立ちます。

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

上記を見つけるには FOR /? を実行してください。

137
efotinis

はい、あなたが使用できる他のショートカットがあります。あなたのコマンドでは、〜d0は0番目の引数のドライブ文字を意味します。

~ expands the given variable
d gets the drive letter only
0 is the argument you are referencing

0番目の引数はスクリプトパスなので、パスのドライブ名を取得します。次のショートカットも使えます。

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 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    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

これは、CALL /?を実行したときにコマンドプロンプトで直接見つけることもできます。またはFOR /?

47
Clewaks

Fromバッチファイルでのファイル名解析とその他の慣用句 - Realの使い方

スクリプトがあるパス(ドライブなし):〜p0

スクリプトがあるドライブ:〜d0

9
William Keller

もう1つのヒントは、カレントディレクトリを別のドライブに設定することです。最初に%~d0、次にcd %~dp0です。これでディレクトリがバッチファイルのドライブに変更され、次にそのフォルダに変更されます。

#oneLinerLoversの場合、cd /d %~dp0はドライブとディレクトリの両方を変更します。

これが誰かに役立つことを願っています。

8

%~d0は引数0(スクリプト名)のドライブ文字、%~p0はパスを表します。

6
Armin Ronacher

気をつけなければならないことがいくつかあります。

ダブルクリックした場合、バッチファイル%0は引用符で囲まれます。たとえば、このファイルをc:\test.batとして保存したとします。

@echo %0
@pause

それをダブルクリックすると、新しいコマンドプロンプトが出力されます。

"C:\test.bat"

しかし、あなたが最初にコマンドプロンプトを開き、そのコマンドプロンプトから直接それを呼び出すと、%0はあなたがタイプされたを参照するでしょう。 test.batと入力した場合Enter引用符を入力しなかったので、%0の出力には引用符が付きません。

c:\>test.bat
test.bat

testと入力した場合Enter拡張子を入力しなかったので、%0の出力も拡張子を持ちません。

c:\>test
test

tEsTと同じEnter

c:\>tEsT
tEsT

"test"と入力した場合Enter%0の出力は(あなたがそれらをタイプしたので)引用符を持っていますが拡張子はありません:

c:\>"test"
"test"

最後に、"C:\test.bat"と入力すると、出力はダブルクリックした場合とまったく同じになります。

c:\>"C:\test.bat"
"C:\test.bat"

%0が他のフォルダからスクリプトを呼び出すことができるため、これらがすべての可能な値であるとは限らないことに注意してください。

c:\some_folder>/../teST.bAt
/../teST.bAt

%~0の出力は単なる%~0の出力から引用符(もしあれば)を引いたものであるため、上記の例はすべて%0にも影響します。

4
Pacerier

このコードは〜tilda文字の使い方を説明していますが、これは私にとって最も分かりにくいものでした。これを理解すれば、理解しやすくなります。

@ECHO off
SET "PATH=%~dp0;%PATH%"
ECHO %PATH%
ECHO.
CALL :testargs "these are days" "when the brave endure"
GOTO :pauseit
:testargs
SET ARGS=%~1;%~2;%1;%2
ECHO %ARGS%
ECHO.
exit /B 0
:pauseit
pause
2
djangofan

現在いるファイルまたはディレクトリの現在の場所が表示されます。バッチファイルがデスクトップディレクトリにある場合、 "%〜dp0"はデスクトップディレクトリを表示します。現在のディレクトリを現在のファイル名で表示したい場合は、「%〜dp0%〜n0%〜x0」と入力します。

0
zask