如何使用ECHO批量设置变量

时间:2015-09-08 02:34:54

标签: windows batch-file

我一直在试图找出一种方法来使用ECHO命令来批量设置变量值。

我知道您可以滥用FOR命令将命令输出设置为变量。 例如:

for /F "delims=" %%A in ('ipconfig ^| findstr /i IPv4') do (set TestVar=%%A)

但是,我无法弄清楚如何使用ECHO命令作为源命令,“set”将用作变量的输入。

有人知道是否有办法实现这个目标?

以下是帮助澄清我的目标的更多信息。

  1. 这样做的目的是让我可以通过findstr和其他命令过滤命令的结果,以去掉我在脚本中无法使用的字符。然后我将这个剥离的输出分配给变量。

  2. 我正在创建一个批处理脚本,用于将插入特定系统的任何USB驱动器的内容与系统上的源文件夹同步。我试图让它“智能”,因为它会自动过滤掉任何网络驱动器等。网络驱动器是通过GPO分配的,所以我不想依赖手动更新从文本文件中提取的驱动器列表,因为忘记更新此类文件可能会导致网络驱动器被覆盖。

  3. 我已经能够通过回显“temp.txt”文件然后从这个temp.txt文件中提取脚本来实现这个目标。

  4. 另一个问题是我正在记录每个“同步”的结果。我不希望它创建错误的成功日志,所以我编译脚本的方式是失败的同步(失败的robocopy)不会进入日志命令,这是对文本文件的回应(假设失败是一种robocopy根本不运行的类型,如果运行robocopy,它仍会记录。)

  5. 这是我的脚本,如果它有助于澄清事情,以及我允许出现的一个配置文件的副本。

  6. SCRIPT:

    REM This script relies on the "SyncConfig.txt" file to be present in the same directory as this script in order to run successfully.
    REM The "SyncConfig.txt" file specifies which drives are eligible to have their contents synced with the Rockwell VM drive.
    echo off
    cls
    color c
    echo THIS SCRIPT WILL EFFECTIVELY WIPE ANY DRIVE THAT IS PRESENT
    echo WHICH IS NOT SPECIFICALLY EXCLUDED IN THE "SyncConfig.txt"
    echo FILE!  Press any key to continue or click the "X" to cancel.
    pause
    color a
    set SourceDrive=F:\
    setlocal ENABLEDELAYEDEXPANSION
    for /F "skip=6 tokens=*" %%A in (SyncConfig.txt) do (
        echo %%A| findstr /i /v "Not Included" > temp.txt
        for /F %%B in (temp.txt) do (
            robocopy /MIR !SourceDrive! %%B:\
            echo %Date% > %%B:\LastSynced.txt
            for /F %%C in (temp.txt) do (
                dir %%C:| findstr /i "Volume" | findstr /i /v "Serial" >> temp.txt
                )
            for /F "skip=1 tokens=6,7,8,9,10 delims= " %%C in (temp.txt) do (
                echo ------------------- >> SyncLog.log
                echo %DATE% %TIME% >> SyncLog.log
                echo sync performed for: %%C %%D %%E %%F %%G >> SyncLog.log
                )
            )
        )
    del temp.txt
    endlocal
    

    “CONFIG FILE” - SyncConfig.txt:

    Lines which include the text "Not Included" anywhere in the line will be    ignored. This is being used     |Not Included
    to prevent specific drives (such as mapped network shares) from being overwritten by the sync script.       |Not Included
                                                            |Not Included
                                                            |Not Included
    DRIVES:                                                     |Not Included
    -----------                                                 |Not Included
    A
    B
    C Not Included
    D Not Included
    E Not Included
    F Not Included
    G
    H
    I
    J
    K
    L Not Included
    M
    N
    O Not Included
    P
    Q
    R
    S Not Included
    T Not Included
    U Not Included
    V
    W
    X
    Y
    Z
    

0 个答案:

没有答案