【问题标题】:DOS extract directory from find commandDOS 从 find 命令中提取目录
【发布时间】:2012-11-27 05:16:32
【问题描述】:

我正在编写一个 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

我想要做的不是使用硬编码的目录路径examples\testing_inputs for targetDirectoy,我希望能够搜索提供的文件名并将目录更改为该路径。

我知道我可以使用 "目录文件名.ext /s"

DOS 输出

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
...
...

如何从该信息中提取目录以在脚本中使用?另外,如果有多个同名文件,如何根据文件的时间戳选择路径?

【问题讨论】:

    标签: directory find dos


    【解决方案1】:
    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,应该可以,但为什么不能在 DO 子句中使用set "dir_path=%%~dpF"?另外,我认为您希望最后一行阅读echo %dir_path%
    • 因为我不够聪明:-)
    猜你喜欢
    • 2013-05-04
    • 2019-06-14
    • 2015-04-14
    • 2012-05-17
    • 2023-04-02
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    相关资源
    最近更新 更多