2017-10-17 64 views

回答

0

下面是該任務的註釋批次代碼:

@echo off 
rem Is the batch file not called with an argument which 
rem is expected to be the name of a file with full path? 
if "%~1" == "" goto DemoCode 

rem This code demonstrates how to get path to last but one folder 
rem with accessing the file system and therefore working only if 
rem the specified file respectively its folders really exist. 

for %%I in ("%~dp1..\..\") do set "FilePath=%%~dpI" 

rem Remove the backslash at end. 
set "FilePath=%FilePath:~0,-1%" 
goto DisplayResult 


rem The demo code below demonstrates how to remove file name 
rem and last two folders in path from a file name string with 
rem complete path without accessing the file system at all. 

:DemoCode 
rem Get path of file ending with a backslash. 
for /F "delims=" %%I in ("C:\Test\Test01\Test02\Test03\Test04\Test05\Test06\Test.txt") do set "FilePath=%%~dpI" 

rem Remove the backslash at end. 
set "FilePath=%FilePath:~0,-1%" 

rem Get path to last folder of original file name with full path. 
for /F "delims=" %%I in ("%FilePath%") do set "FilePath=%%~dpI" 

rem Remove the backslash at end. 
set "FilePath=%FilePath:~0,-1%" 

rem Get path to last but one folder of original file name with full path. 
for /F "delims=" %%I in ("%FilePath%") do set "FilePath=%%~dpI" 

rem Remove the backslash at end a last time. 
set "FilePath=%FilePath:~0,-1%" 

:DisplayResult 
echo/ 
echo File path is: %FilePath% 
echo/ 
set "FilePath=" 
pause 

有兩個例子作爲註釋說明。

爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。

  • call /? ...解釋%~1(第一個參數字符串周圍去掉雙引號)和%~dp1(驅動器和第一個參數的路徑)。
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
相關問題