有没有办法将这段代码从 Pull 功能变成 Push 功能?

问题描述 投票:0回答:1

我们使用此代码来更新工作中的计算机,但您必须在每台计算机上一台一台地启动批处理文件。然后,计算机连接到交换机,然后交换机连接到不同房间中的服务器,没有任何计算机或服务器连接到互联网。服务器保存需要复制到计算机的所有文件。 它是几年前制作的,我没有任何批处理文件或编码的经验。

@echo off
mode con COLS=80 LINES=40
title Copy Documents from LocalData and AFC Shares.

set UPDATESERVER=192.168.1.50
set DeviceName=H:
set LocalData=P:\Phoenix_Data\LocalData
set LocalAfc=P:\Phoenix_Data\AFC
set LocalDataShare=\\%UPDATESERVER%\_LocalData
set AfcShare=\\%UPDATESERVER%\_AFC
if [%1] equ [afc] (
    call:copysharetolocal %DeviceName% %AfcShare% %LocalAfc%
)
if [1%] equ [localdata] (
    call:copysharetolocal %DeviceName% %LocalDataShare% %LocalData%
)
if [%1] equ [] (
    call:copysharetolocal %DeviceName% %AfcShare% %LocalAfc%
    call:copysharetolocal %DeviceName% %LocalDataShare% %LocalData%
)
exit
:copysharetolocal
cls
color 0f
net use %~1 /delete > nul 2>&1
echo Connecting to %~2 share.
timeout /t 5 /nobreak > nul
net use %~1 %~2
if %errorlevel% neq 0 (
    color cf
    echo Unable to CONNECT to %~2 share!
    pause
    exit /b %errorlevel%
)
if not exist %DeviceName%\ (
    color cf
    echo Unable to ACCESS %~2 share!
    pause
    exit /b %errorlevel%
) else (
    color 2f
    echo Connected to %~2 Share!
)

if exist "%~3\" (
    rmdir /s /q "%~3"
)
if exist "%~3\" (
    color cf
    echo Error cleaning Local Directories!
    pause
    exit /b %errorlevel%
) else (
    color 2f
    echo Successfully cleaned Local Directories!
)
echo Copying %~2 to Local Directories!
xcopy "%~1\*" "%~3" /s /y /i

if %errorlevel% neq 0 (
    color cf
    echo Error copying from %~2 share!
    pause
    exit /b %errorlevel%
) else (
    color 2f
    echo Successfully copied from %~2 share!
)
:: Remove mapping.
net use %~1 /delete > nul 2>&1
pause
exit /b %errorlevel%

我正在尝试找到一种方法让批处理文件将文件夹的内容从服务器推送到计算机。

batch-file ethernet switching
1个回答
0
投票

在与批处理文件相同的目录中创建一个名为computers.txt 的文本文件。该文件中的每一行都应包含需要更新的计算机的名称。修改批处理文件以读取此文件并迭代每台计算机名称,为每台计算机执行更新过程。

此脚本将从computers.txt 文件中读取每台计算机的名称,连接到服务器共享,将内容复制到每台计算机上的指定目录,然后断开与服务器的连接。确保将 %LocalAfc% 替换为 AFC 文件的适当服务器共享路径。

这是修改后的批处理文件:

@echo off
mode con COLS=80 LINES=40
title Copy Documents from LocalData and AFC Shares.

set UPDATESERVER=192.168.1.50
set DeviceName=H:
set LocalData=P:\Phoenix_Data\LocalData
set LocalAfc=P:\Phoenix_Data\AFC
set LocalDataShare=\\%UPDATESERVER%\_LocalData
set AfcShare=\\%UPDATESERVER%\_AFC

REM Read computer names from computers.txt and loop through each one
for /f %%c in (computers.txt) do (
    echo Updating %%c...
    call :updatecomputer %%c
)

exit

:updatecomputer
cls
color 0f
set ComputerName=%1
set DeviceName=%ComputerName%
net use %DeviceName% /delete > nul 2>&1
echo Connecting to %LocalAfc% share.
timeout /t 5 /nobreak > nul
net use %DeviceName% %AfcShare%
if %errorlevel% neq 0 (
    color cf
    echo Unable to CONNECT to %AfcShare% share on %ComputerName%!
    pause
    exit /b %errorlevel%
)
if not exist %DeviceName%\ (
    color cf
    echo Unable to ACCESS %AfcShare% share on %ComputerName%!
    pause
    exit /b %errorlevel%
) else (
    color 2f
    echo Connected to %AfcShare% Share on %ComputerName%!
)

if exist "%LocalData%\" (
    rmdir /s /q "%LocalData%"
)
if exist "%LocalData%\" (
    color cf
    echo Error cleaning Local Data Directory on %ComputerName%!
    pause
    exit /b %errorlevel%
) else (
    color 2f
    echo Successfully cleaned Local Data Directory on %ComputerName%!
)
echo Copying %AfcShare% to Local Data Directory on %ComputerName%!
xcopy "%DeviceName%\*" "%LocalData%" /s /y /i

if %errorlevel% neq 0 (
    color cf
    echo Error copying from %AfcShare% share on %ComputerName%!
    pause
    exit /b %errorlevel%
) else (
    color 2f
    echo Successfully copied from %AfcShare% share to Local Data Directory on %ComputerName%!
)
:: Remove mapping.
net use %DeviceName% /delete > nul 2>&1
pause
exit /b %errorlevel%
© www.soinside.com 2019 - 2024. All rights reserved.