Backup multiple folders with one click

Create a new text document and name it path

The content is as follows:

D:\3dMaxWorkplace
D:\Desktop\3dMax job
D:\Desktop\interface design

Copy the following code, save as bat, and run

@echo off
setlocal enabledelayedexpansion
echo 备份到U盘:
for /f %%i in (%~dp0路径.txt) do set "pt=%%i"&echo %%i&call :getname %%i&xcopy "!pt!\*.*" "F:\备份\!fn!\" /s /h /d /c /y
echo 备份到桌面:
for /f %%i in (%~dp0路径.txt) do set "pt=%%i"&echo %%i&call :getname %%i&xcopy "!pt!\*.*" "D:\Desktop\备份\!fn!\" /s /h /d /c /y
pause
exit
:getname
set "fn=%~nx1"

Explanation of code operating mechanism:

Read multiple directories to be backed up from a text document and obtain their folder names

Create a new folder with the same name in the destination path, and then copy the files in the directory to the folder

effect:

Notes:

@echo off hide the original command entered

The startup statement for variable delay is " setlocal  enabledelayedexpansion ", and the variable should be enclosed by a pair of exclamation marks "!!" (note that the English exclamation mark should be used), otherwise there will be no variable delay effect. Simply put, you can modify the value of the variable after it is turned on.

%~dp0 The directory where the batch file is located

call

getname branch (equivalent to a function), pass in a path, get the name of the folder, set it to variable fn(folder name)

%~nx1 Get the folder name or file name (including the suffix)

to sum up:

The path of xcopy needs to have a slash at the end, otherwise the computer doesn’t know if it is a file or a folder, it will ask you

Guess you like

Origin blog.csdn.net/weixin_43673589/article/details/109191675