【问题标题】:Grab and Split Content of a File and Use it as a variable in a batch script抓取和拆分文件的内容并将其用作批处理脚本中的变量
【发布时间】:2020-02-28 22:55:18
【问题描述】:


我正在尝试从文件中获取服务器列表并同时获取根驱动器。
这是文件的内容:

serverlist.config

WKS_LIST                           install  WKS1 WKS2 \
                                            WKS3 WKS4 WKS5 \
                                            WKS6

WKS_ROOT_PATHS                       install    C:\temp D:\temp \
                                                C:\temp C:\temp C:\temp \
                                                C:\temp


SERVER_LIST                       install   NODE0 NODE1 \
                                            APP1 APP2 APP3 \
                                            APP4 APP5 APP6 \
                                            APP7

SERVER_ROOT_PATHS              install          I:\temp E:\temp I:\temp \
                                            I:\temp I:\temp I:\temp \
                                            I:\temp I:\temp I:\temp \
                                            I:\temp

TEST_LIST                       install     TEST1 TEST2

TEST_ROOT_PATHS                 install     I:\temp I:\temp

我希望批处理脚本查看 serverlist.config 并将变量设置为如下所示:

set serverlist=NODE0 NODE1 APP1 APP2 APP3 APP4 APP5 APP6 APP7
set rootpath=I E I I I I I I I I

FOR %%a in (%serverlist%) do robocopy \\%%a\%rootpath%$\temp\log I:\temp\TEST-LOG\%%a *mask1* *mask2* /S /copyall /maxage:20200227 /xd *mask3* >> I:\temp\output.log

您能否提供有关如何抓取和拆分 serverlist.config 文件以将它们放入上述批处理脚本中的变量的帮助?
如果您需要任何东西,请告诉我。

【问题讨论】:

  • 我建议您不要考虑尝试解析除非常基本的文本文件之外的任何内容的批处理文件。有第三方帮助实用程序可以帮助您,但我建议您首先寻求适当的脚本语言的帮助。
  • 我只能在工作站/服务器上安装什么,所以我只能使用基本的批处理脚本。
  • 您的操作系统、Windows Script Host、(VBScript, JScript) 和 PowerShell 中已经内置了更高级的脚本语言。

标签: windows batch-file command-line


【解决方案1】:

这是非常不灵活的,但有效:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set config=serverlist.config

:a
set x=2
set y=2
for /f "tokens=1*" %%x in (%config%) do (
if "%%x" equ "SERVER_LIST" set x=0
if "%%x" equ "SERVER_ROOT_PATHS" (set x=1 & set y=0)
if "%%x" equ "TEST_LIST" goto :b
if !x! equ 0 echo %%x %%y>>serverlist.txt 
if !y! equ 0 echo %%x %%y>>rootpath.txt
)

:b
for /f "delims=\" %%x in (serverlist.txt) do set "SERVER_LIST=!SERVER_LIST!%%x"
for /f "tokens=3*" %%x in ("%SERVER_LIST%") do set "serverlist=%%x %%y"

:c
for /f "tokens=*" %%x in (rootpath.txt) do (
set "paths=%%x"
for %%a in (: \ temp) do set "paths=!paths:%%a=!"
set "ROOT_PATH=!ROOT_PATH!!paths!"
)
for /f "tokens=3*" %%x in ("%ROOT_PATH%") do set "rootpath=%%x %%y"

:d
del serverlist.txt
del rootpath.txt
FOR %%a in (%serverlist%) do robocopy \\%%a\%rootpath%$\temp\log I:\temp\TEST-LOG\%%a *mask1* *mask2* /S /copyall /maxage:20200227 /xd *mask3* >> I:\temp\output.log
ENDLOCAL

基本上它抓取以下内容:

  1. SERVER_LISTSERVER_ROOT_PATHS
  2. SERVER_ROOT_PATHSTEST_LIST

【讨论】:

  • 感谢@HackingAddict0302 如果没有TEST_LIST 和TEST_ROOT_PATHS,它会如何工作?有没有办法退出并仍然使脚本工作?
【解决方案2】:
@echo off
setlocal enabledelayedexpansion

set "server_list=serverlist.config"

rem Set variables with leading underscores from the server list i.e. _SERVER_LIST=...
set "continuation="

for /f "tokens=*" %%A in (!server_list!) do @(
    set "line=%%~A"

    if "!line:~-1!" == "\" (
        set "continuation=y"
        if defined last_line (
            set "line=!last_line! !line:~,-1!"
        ) else (
            set "line=!line:~,-1!"
        )
    ) else if defined continuation (
        set "continuation="
        set "line=!last_line! !line!"
        for /f "tokens=1,2,* delims= " %%B in ("!line!") do set "_%%~B=%%~D"
        set "line="
    ) else (
        for /f "tokens=1,2,* delims= " %%B in ("!line!") do set "_%%~B=%%~D"
        set "line="
    )

    set "last_line=!line!"
)

rem Set _SERVER_ROOT_DRIVES from _SERVER_ROOT_PATHS
set "_SERVER_ROOT_DRIVES="

for %%A in (!_SERVER_ROOT_PATHS!) do (
    set "drive=%%~dA"

    if defined drive (
        set "drive=!drive:~,1!"
        if defined _SERVER_ROOT_DRIVES (
            set "_SERVER_ROOT_DRIVES=!_SERVER_ROOT_DRIVES! !drive!"
        ) else (
            set "_SERVER_ROOT_DRIVES=!drive!"
        )
    )
)

rem Run robocopy (Remove echo and carets if looks OK to actually run robocopy)
for %%A in (%_SERVER_LIST%) do (
    echo robocopy \\%%A\!_SERVER_ROOT_DRIVES!$\temp\log I:\temp\TEST-LOG\%%A *mask1* *mask2* /S /copyall /maxage:20200227 /xd *mask3* ^>^> I:\temp\output.log
)

robocopy 命令的echo 输出与问题中代码的输出相匹配。

代码将读取服务器列表并使用SERVER_LIST 之类的关键字,并将前导下划线作为变量名使用。如果检测到行尾继续符 \,则变量名称的值 set 将是同一行中的值,并将包括下一行中的值。

驱动器字母从_SERVER_ROOT_PATHS 中提取,并设置为一个名为_SERVER_ROOT_DRIVES 的新变量。

robocopy 命令将回显到控制台以查看是否正常。

可以使用set _查看脚本中前导下划线的变量名和值,方便调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2018-05-13
    相关资源
    最近更新 更多