2016-07-06 54 views
0

我想從當前路徑中獲取一個文件夾的名稱,並將其傳遞給批處理腳本中的命令。我現在的路徑是:如何從批處理腳本中的當前路徑中獲取更高級別文件夾的名稱?

C:\TopDir\NextDir\AppDir\GetThisName\src\main\resources\batchScripts\ 

,我希望得到的文件夾GetThisName的名字並把它傳遞到一個命令。目前,我可以用這個腳本獲得文件夾的名稱:

::Save the current path 
set mypath=%~dp0 

::get the name of the folder five levels up 
FOR %%V IN ("%~dp0..\..\..\..\") DO set shortPath=%%~dpV 
cd %shortPath% 
for %%* in (.) do set FolderName=%%~nx*= 

::Go back to original location 
cd %mypath% 

::Pass FolderName to command 
java .... -o %FolderName% ... 

但我想知道,是否有一個更優雅的方式來獲得該文件夾的名稱,而無需移動文件夾?

回答

1

這麼近......

FOR %%V IN ("%~dp0..\..\..\..") DO set "foldername=%%~nxV" 
echo %foldername% 

注:省略最後\

+0

謝謝!這工作完美! – jencoston

相關問題