2012-11-27 22 views
1

我正在編寫一個dos腳本,我想獲取已知文件的路徑,以便可以在腳本內使用該路徑來更改爲該目錄以使用指定的文件並輸出將文件記錄到同一個目錄。從提取命令的DOS提取目錄

該腳本將一些目錄添加到路徑中,並更改爲所需的目錄以使用同一目錄中的輸入文件執行命令。該命令會生成許多保存在同一目錄中的文件。

這裏是我迄今爲止

@ECHO OFF 

:: Check argument count 
set argC=0 
for %%x in (%*) do Set /A argC+=1 

IF %argC% LSS 3 (echo WARNING must include the parent Directory, the filename and the timestep for the simulation) 

:: Assign meaningfull names to the input arguments 
set parentDirectory=%1 
set filename=%2 
set scenarioTimestep=%3 

:: Check validaty of the input arguments 
:: TODO: implement check for directory, filename exists, and possibly limits to the timestep 
IF "%parentDirectory%"=="" (
    set parentDirectory=P:Parent\Directory 
) 
IF "%filename%"=="" (
    set filename=ship2.xmf 
) 
IF "%scenarioTimestep%"=="" (
    set scenarioTimestep=0.1 
) 

echo parent Directory: %parentDirectory% 
echo filename: %filename% 
echo timestep: %scenarioTimestep% 

set MSTNFYURI=file:mst.log 
set MSTNFYLEVEL=debug 
set MSTNFYFLUSH=1 

set XSFNFYURI=file:xsf.log 
set XSFNFYLEVEL=debug 
set XSFNFYFLUSH=1 

set parentNFYURI=file:parent.log 
set parentNFYLEVEL=debug 
set parentNFYFLUSH=1 

:: Add the parent directories to the path 
set PATH=%parentDirectory%\bin\;%parentDirectory%\bin\ext\;%parentDirectory%\bin\gtkmm\;%parentDirectory%\bin\osg\;%PATH% 

:: Change to the target directoy 
set tagetDirectory=%parentDirectory%\examples\testing_inputs 
cd %tagetDirectory% 

echo command will be: ft -c %filename% -T %scenarioTimestep% 
::ft -c %filename% -T %scenarioTimestep% 

@ECHO ON 

我希望能夠做的是,而不是使用硬編碼的目錄路徑例子\ testing_inputs爲targetDirectoy什麼,我希望能夠以搜索提供文件名並將目錄更改爲該路徑。

我知道我可以使用 「DIR FILENAME.EXT/S」

DOS顯示的信息ouptut

Volume in drive C is OS 
Volume Serial Number is XXXX-XXXX 

Directory of C:\Users\Me\parent\examples\testing_input 

15/11/2012 02:51 PM <size> filename 
... 
... 

我如何解壓目錄形式此信息要在腳本中使用?另外,如果有多個相同名稱的文件,我如何根據文件的時間戳選擇路徑?

回答

2
for /f %%F in ('dir /B /S /A:-D filename.ext ') do set file_path=%%F 
pushd %file_path%\.. 
dir_path=%CD% 
popd 
echo %file_path% 
echo %dir_path% 

這是你在找什麼?

編輯:檢查dbenham的評論。

+1

+1,這應該可行,但爲什麼不在你的DO子句中設置「dir_path = %%〜dpF」'?另外,我想你想要最後一行讀'echo%dir_path%' – dbenham

+0

因爲我不夠聰明:-) – npocmaka