【问题标题】:How to set a string from a txt to a variable with cmd如何使用 cmd 将 txt 中的字符串设置为变量
【发布时间】:2011-05-15 21:21:29
【问题描述】:

我有一个指向特定路径的 txt 文件,其中包含一行或多行,就像 以下:

Directory of c:\folderA\folderB
Directory of c:\folderC\folderD

这些是程序 example.exe 的路径。我想自动找到 example.exe 在我的计算机中存在的路径并从 .cmd 脚本运行它。到目前为止,我已成功将存在路径记录到 txt。如何设置只有路径的变量 第一行?

最终结果应该是var_path=c:\folderA\folderB

【问题讨论】:

    标签: string variables path cmd text-files


    【解决方案1】:

    一个快速而肮脏的解决方案是:

    @echo off
    Call :getFirstDirectory
    ::do your stuff with %var_path%. For example:
    @echo %var_path%
    
    exit /b
    
    :getFirstDirectory
    for /f "tokens=3" %%a in (dirs.txt) do (
    set var_path=%%a
    exit /b
    )
    

    假设dirs.txt 是包含以下内容的文件:

    Directory of c:\folderA\folderB
    Directory of c:\folderC\folderD
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      相关资源
      最近更新 更多