2009-08-26 9 views
10

Piszę skrypt budujący, a jeśli katalog, w którym użytkownik buduje skrypt, zawiera spacje, wszystko się rozpada. Aby to obejść, pomyślałem o użyciu nazw plików 8.3, aby drive:\Documents and setttings\whatever stał się drive:\Docume~1\whatever. Aktualny katalog można znaleźć, wysyłając zapytanie do zmiennej środowiskowej% CD%.Jak przekształcić katalog roboczy w skróconą nazwę pliku 8.3 za pomocą polecenia partia?

Jak przekształcić% CD% w ścieżkę krótkiego pliku?

Odpowiedz

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

Edit: nie zapomnij użyć %% jeśli używasz go w pliku wsadowym. jak ten

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

na moim komputerze:

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

Inne opcje:

Ponadto, podstawienie o referencje zmienna została wzmocniona. Możesz teraz użyć następującej opcjonalnej składni:

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

szukałem tego sam - byłbyś zaskoczony, jak trudno googling dla "dla" i wiersza polecenia jest. Ucieknij% s na %%, jeśli jest w pliku wsadowym. – Will

+1

Otrzymuję to jako komunikat o błędzie: cd ~ sf było nieoczekiwane w tym momencie. – Geo

+0

okrzyki będą. Ciągle o tym zapominam. –

-3

Lepiej: użyj znaków cudzysłowu (") wokół wszystkich ścieżek.

+0

Trudno to wyjaśnić, ale nie jest to możliwe. – Geo

+1

Nie odpowiada na pytanie OP, nie wyjaśnia, dlaczego jest to lepsze w sytuacjach, w których działa i nie zawsze działa. –

Powiązane problemy