2014-11-24 58 views
0

標題有點含糊,我不知道如何說出它。批量啓動路徑此文件的文件名稱

我該怎麼做。 運行文件的開始路徑\文件名。

有點像啓動路徑\%0這是有點什麼,我正在嘗試做,但如果我這樣做,它會拿出像波紋管的東西。

複製C:\ Users \用戶名\桌面\文件夾\ C:\ Users \用戶名\桌面\ batch.bat

我只想C:\ Users \用戶名\桌面\文件夾\ batch.bat batch.bat是文件名,如果你重命名批處理文件,這個必須改變。

對不起,如果我解釋得很糟糕。

回答

0

你可以用一些for詭計做到這一點:

@setlocal enableextensions enabledelayedexpansion 
@echo off 
echo %0 
for %%f in (%0) do set basename=%%~nf 
echo Will run \arbitrary-path\%basename% 
endlocal 

%%~nf給你%%f變量的名稱,沒有路徑或擴展。如果你喜歡的東西運行:

C:\Users\Pax\qq.cmd 

,你會看到類似這樣的:

c:\Users\Pax\qq.cmd 
Will run \arbitrary-path\qq 

你可以看到所有的其他有用的擴展,如果你鍵入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 
+0

謝謝。 此代碼: '@迴響 @setlocal ENABLEEXTENSIONS enabledelayedexpansion 爲%% F IN(%0)做設定基名= %%〜NF 組UsPr =%USERPROFILE% 拷貝%0 「%UsPr%」 開始「%UsPr%\%basename%.bat」' 如果這是在禁用CMD的計算機上使用的,當第二個批處理文件使用start命令打開時會出錯說cmd被禁用,我怎麼能解決這個問題?還有,如何才能停止打開自己無限的時間直到你的電腦崩潰/死機? – Neb9 2014-11-24 06:52:23

+0

@ Neb9,如果'cmd'被禁用,所有投注都關閉。你無法確定地運行任何命令腳本。而且,如果您想停止無限迴歸,只需添加一個檢查以確保'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' – paxdiablo 2014-11-24 07:06:39

+0

對不起,我沒有經驗的批次,我將如何添加你描述的支票?如果禁用了cmd,批處理將生效,但不能使用start命令從批處理文件啓動任何批處理文件。 – Neb9 2014-11-24 07:26:03

相關問題