简体   繁体   English

在批处理脚本中逐行删除

[英]Deleting line after line in a batch script

I am writing a Batch-script which copies a file to multiple servers. 我正在写一个批处理脚本,它将文件复制到多个服务器。 It takes the servername from a .txt file as a variable and uses it to connect to the server. 它从.txt文件中获取服务器名称作为变量,然后使用它来连接服务器。
After a name was turned into a variable, I want to remove this entry from the file (and save it to another file) so that the Script takes the next available server name when running again. 将名称转换为变量后,我想从文件中删除该条目(并将其保存到另一个文件),以便脚本在再次运行时采用下一个可用的服务器名称。

So far, I have written this: 到目前为止,我已经写了这个:

@echo off
:start
set /p servername=<server.txt
findstr /v "%servername%" server.txt > serverdone.txt

rem (Part of the script that copies the file, this is already working)

GOTO start

The script is able to take the first line of server.txt and puts it in the %servername% variable as supposed, however, the findstr line does not seem to work. 该脚本能够采用server.txt的第一行并将其按预期方式放入%servername%变量中,但是, findstr行似乎不起作用。 The serverdone.txt file stays empty, and the script just keeps using the first server in the server.txt file. serverdone.txt文件保持为空,脚本仅继续使用server.txt文件中的第一台服务器。 I used this question as a guide: Delete certain lines in a txt file via a batch file . 我以这个问题为指导: 通过批处理文件删除txt文件中的某些行

Why do you not use a for /F loop to read the file server.txt line by line and do the copying stuff within that loop? 为什么不使用for /F循环逐行读取文件server.txt并在该循环中进行复制呢? I think this could perfectly work for you. 我认为这可能完全适合您。 Take a look at the following example: 看下面的例子:

@echo off

rem This iterates through all lines of `server.txt`:
for /F "usebackq delims=" %%S in ("server.txt") do (

    rem Call copying sub-routine for each item:
    call :SUB_COPY %%S

)
exit /B

:SUB_COPY
rem (do your copying stuff here, using `%1`)
exit /B

The copying stuff could also be placed within the for /F loop directly (using %%S instead of %1 ). 复制的东西也可以直接放在for /F循环中(使用%%S代替%1 )。


In case the copying stuff could fail and you want server.txt to contain all server names for which it failed and serverdone.txt to contain those for which it succeeded, you could do this: 如果复制的东西可能失败并且您希望server.txt包含失败的所有服务器名称,并serverdone.txt包含成功的服务器名称,则可以执行以下操作:

@echo off

rem This iterates through all lines of `server.txt`;
rem `type` ensures the file to be read completely
rem before the any further activities are pursued;
rem `break` does nothing and is used to empty files:
for /F "delims=" %%S in ('
    type "server.txt" ^& ^
        ^> "server.txt" break ^
        ^> "serverdone.txt" break
') do (

    rem Call copying sub-routine for each item:
    call :SUB_COPY %%S
    if ErrorLevel 1 (
        rem (`ErrorLevel` is `>=1`, so failure)
        >> "server.txt" echo %%S
    ) else (
        rem (`ErrorLevel` is `0`, so success)
        >> "serverdone.txt" echo %%S
    )

)
exit /B

:SUB_COPY
rem (do your copying stuff here, using `%1`;
rem  in case of errors, set `ErrorLevel`)
exit /B 0 & rem (success: `ErrorLevel` is `0`)
exit /B 1 & rem (failure: `ErrorLevel` is `1`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM