2009-08-26 5 views
10

私はビルドスクリプトを書いていますが、ユーザーがスクリプトをビルドするディレクトリにスペースが含まれていると、すべてが崩壊します。その周りを回って、私はがdrive:\Docume~1\whateverになるように8.3ファイル名を使うことを考えました。現在のディレクトリは、環境変数%CD%を照会すると見つかります。バッチを使用して作業ディレクトリを8.3短いファイル名に変換するにはどうすればよいですか?

%CD%を短いファイルパスに変換するにはどうすればよいですか?

答えて

19
for %f in ("%cd%") do @echo %~sf 

編集:はバッチファイルでそれを使用している場合は%%を使用することを忘れないでください。私のマシンでこの

for %%f in ("%cd%") do @echo %%~sf 

よう

C:\Users\preet>cd "\Program Files" 
C:\Program Files>for %f in ("%cd%") do @echo %~sf 
C:\PROGRA~1 

その他のオプション:また

は、変数の参照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 

The modifiers can be combined to get compound results: 

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

はこれを自分で探していました。「for」とコマンドプロンプトがどのようにしてグーグルグーグルであるかは驚くでしょう。バッチファイルの場合は、%sを%%にエスケープします。 – Will

+1

これはエラーメッセージとして表示されます。cd〜sfは今回は予期せぬものでした。 – Geo

+0

の歓声です。私はこれを忘れています。 –

-3

すべてのパスで引用符(")を使用してください。

+0

それは説明するのは難しいのですが、これはオプションではありません。 – Geo

+1

OPの質問に答えることはできませんが、なぜこれがうまくいっているかを説明しておらず、いつもうまくいくとは限りません。 –

関連する問題