【问题标题】:Batch script to store certain output details in a Variable将某些输出详细信息存储在变量中的批处理脚本
【发布时间】:2016-04-20 12:39:17
【问题描述】:

有人可以帮我编写这个批处理脚本吗?

我需要运行以下命令 svn info 并获取变量中的某些详细信息。

Path: .
Working Copy Root Path: C:\Users\jslevin\Desktop\SQL
URL: https://ofss220383.in.oracle.com:18080/svn/SVN_DEMO/branches/FCUBS_TEST/Soft/AM/SQL
Relative URL: ^/branches/FCUBS_TEST/Soft/AM/SQL
Repository Root: https://ofss220383.in.oracle.com:18080/svn/SVN_DEMO
Repository UUID: 866b0b85-a196-4771-a359-d37e344426b2
Revision: 47
Node Kind: directory
Schedule: normal
Last Changed Author: john.levin@oracle.com
Last Changed Rev: 47
Last Changed Date: 2016-04-11 18:01:56 +0530 (Mon, 11 Apr 2016)

Var1 = SVN_DEMO (This variable should hold the Repository Root: line and should get the Last word i.e., SVN_DEMO)

Var2 = FCUBS_TEST (Should grep URL: and hold the word after branches alone i.e., FCUBS_TEST)

我正在我的脚本中创建一些临时文件。在脚本结束时,我需要找到任何可用的文件并删除它。

【问题讨论】:

  • 批处理脚本在哪里,您需要帮助吗?
  • @Stephan,我使用 awk、grep 在 shell 中实现我的目标,但想知道如何批量使用 findstr 和 grep 来实现我的结果。任何想法表示赞赏。

标签: windows batch-file batch-processing


【解决方案1】:

使用 unix-tools 可能更容易,但使用纯批处理没有太大挑战(保持简单,无需优化):

@echo off
REM get the two important lines into variables:
for /f "tokens=*" %%a in ('find "Repository Root" file.txt') do set var1=%%a
for /f "tokens=*" %%a in ('find "Relative URL" file.txt') do set var2=%%a

:loop
REM var1 - remove from the beginning until (including) the first '/':
set var1=%var1:*/=%
REM if there is another '/' then do it again:
if "%var1%" neq "%var1:/=%" goto :loop
REM kind of "brute force", isn't it?

REM var2: remove from the beginning until (including) 'branches/':
set var2=%var2:*branches/=%
REM take the first token by '/' as delimiter:
for /f "delims=/" %%a in ("%var2%") do set var2=%%a

set var 

【讨论】:

  • 谢谢斯蒂芬。你的想法让我修改并得到我所期望的结果。所以这是我的代码.................................................. .for /f "tokens=4 delims=//" %%i in ('findstr /ilc:"Repository Root" "svninfo"') do (set var1=%%i) for /f "tokens=3 delims=^/ " %%j in ('findstr /ilc:"Relative URL" "svninfo"') do (set var2=%%j) echo %var1% 1>&2 echo %var2% 1>&2 exit 1
  • 小心:tokens= 会给出错误的结果,如果你的路径有另一个深度。这就是我没有使用tokens 的原因。并且只取了“存储库中的最后一个”和“'分支'旁边”。
  • @Stephen,我需要获取的字符长度将始终保持稳定。所以我想这对我来说不会有麻烦..在这种情况下它也会行为不端吗?顺便说一句,非常感谢您的支持。
猜你喜欢
  • 2022-01-21
  • 2017-12-11
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多