ドライブ文字をUNCパスまたは逆置換パスに変換するバッチです。しかしそれは動作しますが動作しません。使用の
例:script.cmd echo Z: Y: W:
@echo off
:: u is a variable containing all arguments of the current command line
set u=%*
:: enabledelayedexpansion: exclamation marks behave like percentage signs and enable
:: setting variables inside a loop
setlocal enabledelayedexpansion
:: parsing result of command subst
:: format: I: => C:\foo\bar
:: variable %G will contain I: and variable H will contain C:\foo\bar
for /f "tokens=1* delims==> " %%G IN ('subst') do (
set drive=%%G
:: removing extra space
set drive=!drive:~0,2!
:: expanding H to a short path in order not to break the resulting command line
set subst=%%~sfH
:: replacing command line.
call set u=%%u:!drive!=!subst!%%
)
:: parsing result of command net use | findstr \\ ; this command is not easily tokenized because not always well-formatted
:: testing whether token 2 is a drive letter or a network path.
for /f "tokens=1,2,3 delims= " %%G IN ('net use ^| findstr \\') do (
set tok2=%%H
if "!tok2:~0,2!" == "\\" (
set drive=%%G
set subst=%%H
) else (
set drive=%%H
set subst=%%I
)
:: replacing command line.
call set u=%%u:!drive!=!subst!%%
)
call !u!
ああ、はい、CMDの道を行くことは、私が最初に拒否されたソリューションです。私は実際にそのトリックを行うWin32 APIを見つけることを試みていました。明らかに、あなたのソリューションはバッチ/スクリプト環境で同じことをしようとする人々のために働くはずです。あなたのアイデアに感謝します。 CMDのトリックを発見する機会がありました。 –
このスクリプトは素晴らしいです。ただ1つのバグです。置換ドライブパスのスペースをサポートしていません。修正するには、最初のforループを以下から変更してください: ...トークン= 1,2 ... 〜 ...トークン= 1 * ... –
@MrBungle:ありがとうございます!私は 'tokens = 1 *'について知りませんでした。調査します。 '1,2 *'ではないと確信していますか? – Benoit