Location>code7788 >text

Use Bat to copy files in a remote shared directory under Windows

Popularity:129 ℃/2025-01-23 16:48:49

@echo off
::By dressed in white

:: TIME:2025/1 

::version:1.0 

:: Check if the configuration file exists

if exist configuration.txt (

set /p Remote_dir=<configuration.txt

echo reads remote download directory information from the configuration file

) else (

echo The configuration file has not been read. Please press Enter to enter the default update plan:

:choose

set /p choice=

if /i "%choice%"=="1" (

set Remote_dir=\\remote directory 1

) else if /i "%choice%"=="2" (

set Remote_dir=\\remote directory 2

echo Please enter 1 or 2 to select the default remote share path according to actual needs:

echo 1.Beijing area (BJ)

echo 2.Shanghai area (SH)

goto choose

)

)

Remote download directory information read by echo: %Remote_dir%

:write

echo The currently logged in user is: %USERNAME%

set /p DIR_NAME="Please confirm that the above user name is correct and press Enter:"

 

:: Check whether there is a personal directory of the customer user under users on the C drive

set "TARGET_DIR=C:\Users\%USERNAME%"

if not exist "%TARGET_DIR%" (

echo Directory not found: %TARGET_DIR%

goto write

)

:: Enter the target directory and check whether the target directory exists

set "US_DIR=%TARGET_DIR%\XXX"

if not exist "%US_DIR%" (

echo did not check the XXX directory, creating it...

mkdir "%US_DIR%"

echo %US_DIR% directory is created successfully.

)

:: Enter the US_DIR directory and check whether the directory already exists

set "target_DIR="

for /d %%i in ("%US_DIR%\directory name*") do (

if not defined target_DIR (

set "target_DIR=%%i"

echo finds the target directory: %%i

)

)

:: Delete the old version directory if a directory starting with the target directory name exists

if defined target_DIR (

rmdir /s /q "%target_DIR%"

echo deletes the old version successfully.

)

:: Copy the entire folder from the shared directory of the remote machine to the US directory

echo starts downloading the new version to the local, please wait!

xcopy /e /i /y "%Remote_dir%" "%US_DIR%\target directory"

if errorlevel 1 (

echo The remote directory cannot be accessed. Please exit and re-run and select other options to copy again. If it still fails, please contact the administrator!

goto end

)else (

The new version of echo is downloaded!

)

:end

pause