批处理文件将文件复制到许多机器

时间:2022-02-11 16:02:00

I need to have a batch file that copies files (8) in my local directory to either a list of machines on the network (hostname or IP) or have me prompt to enter the next hostname/ip

我需要一个批处理文件,将我本地目录中的文件(8)复制到网络上的机器列表(主机名或IP)或让我提示输入下一个主机名/ ip

I'd prefer not to have to run the script over and over again to enter the next hostname/ip

我不想一遍又一遍地运行脚本来输入下一个hostname / ip

I'll also need to see the output for each system to make sure it connected correctly and copied successfully. Perhaps a "ready to process next?" after each machine?

我还需要查看每个系统的输出,以确保正确连接并成功复制。也许“准备下一步?”每台机器后?

I don't know how to have a batch file does the equivalent of STDIN, so please forgive the ?STDIN? placeholder

我不知道如何让批处理文件相当于STDIN,所以请原谅?STDIN?占位符

So far, all I have is

到目前为止,我所拥有的只是

net use S: \\?STDIN?\C$ /user:domain\myusername mypassword  

copy %~file01.txt S:\%SystemRoot%\system32 /y
copy %~file02.txt S:\%SystemRoot%\system32 /y
copy %~file03.txt S:\%SystemRoot%\system32 /y
copy %~file04.txt S:\%SystemRoot%\system32 /y
copy %~file05.txt S:\%SystemRoot%\system32 /y
copy %~file06.txt S:\%SystemRoot%\system32 /y
copy %~file07.txt S:\%SystemRoot%\system32 /y
copy %~file08.txt S:\%SystemRoot%\system32 /y

net use :s /delete

do it again

2 个解决方案

#1


You can prompt the user for input (and store it into an environment variable) with the set /p command:

您可以使用set / p命令提示用户输入(并将其存储到环境变量中):

set /p HOST=Machine?
net use S: \\%HOST%\C$ ...

You can loop by using a label and jump to it. You should offer a way to exit, though:

您可以使用标签循环并跳转到它。但是,您应该提供退出的方法:

:loop
set /p HOST=Machine? (leave blank to exit)
if [%HOST%]==[] goto :EOF
net use S: \\%HOST%\C$ ...
copy ...
...
goto loop

If you don't want to enter each host name individually you can also loop over the arguments to the batch file:

如果您不想单独输入每个主机名,您还可以循环遍历批处理文件的参数:

:loop
if [%1]==[] goto :EOF
net use S: \\%1\C$ ...
copy ...
...
shift
goto loop

You can can call it as follows, then:

您可以按如下方式调用它,然后:

my_batch machine1 machine2 192.168.12.14 ...

HTH

#2


you can use the "if ERRORLEVEL 1" to see if an error occurred in the copy which will allow you to trap for problems with the copy. Along with a "if EXIST" to check that the file is there, though, you may get some files that are zero length.

您可以使用“if ERRORLEVEL 1”来查看副本中是否发生错误,这样可以捕获副本的问题。但是,除了“if EXIST”以检查文件是否存在之外,您可能会得到一些长度为零的文件。

Though, you may want to investigate Powershell as a more capable tool for the job.

但是,您可能希望将Powershell作为一种更有能力的工具进行调查。

#1


You can prompt the user for input (and store it into an environment variable) with the set /p command:

您可以使用set / p命令提示用户输入(并将其存储到环境变量中):

set /p HOST=Machine?
net use S: \\%HOST%\C$ ...

You can loop by using a label and jump to it. You should offer a way to exit, though:

您可以使用标签循环并跳转到它。但是,您应该提供退出的方法:

:loop
set /p HOST=Machine? (leave blank to exit)
if [%HOST%]==[] goto :EOF
net use S: \\%HOST%\C$ ...
copy ...
...
goto loop

If you don't want to enter each host name individually you can also loop over the arguments to the batch file:

如果您不想单独输入每个主机名,您还可以循环遍历批处理文件的参数:

:loop
if [%1]==[] goto :EOF
net use S: \\%1\C$ ...
copy ...
...
shift
goto loop

You can can call it as follows, then:

您可以按如下方式调用它,然后:

my_batch machine1 machine2 192.168.12.14 ...

HTH

#2


you can use the "if ERRORLEVEL 1" to see if an error occurred in the copy which will allow you to trap for problems with the copy. Along with a "if EXIST" to check that the file is there, though, you may get some files that are zero length.

您可以使用“if ERRORLEVEL 1”来查看副本中是否发生错误,这样可以捕获副本的问题。但是,除了“if EXIST”以检查文件是否存在之外,您可能会得到一些长度为零的文件。

Though, you may want to investigate Powershell as a more capable tool for the job.

但是,您可能希望将Powershell作为一种更有能力的工具进行调查。