Method of automatically backing up files to U disk

Paste the following code into the text file to save, change the suffix to bat to run

:start
@echo off
::若检测到U盘插入,则自动拷贝文件到U盘
::pt为自动备份的目录路径
setlocal enabledelayedexpansion
set "pt=D:\Desktop\自动备份"
:check
echo 正在检测U盘
for %%i in (d e f g h i j k ) do (
fsutil fsinfo drivetype %%i: | findstr "可移动" >nul 2>nul && set upan=%%i:
)
if "%upan%"=="" (
echo 未检测到U盘
ping -n 2 127.0.1>nul
cls
goto :check
)
echo 检测到U盘
echo 本次复制的文件:
if exist "%upan%\备份" (
::copy "!pt!\*.*" "%upan%\备份\" /v
xcopy "!pt!\*.*" "%upan%\备份\" /s /h /d /c /y
goto :1
) else (
md "%upan%\备份"
::copy "!pt!\*.*" "%upan%\备份\"
xcopy "!pt!\*.*" "%upan%\备份\" /s /h /d /c /y
)
goto :2
:1
echo 已成功将目录文件备份至U盘
ping -n 2 127.0.1>nul
pause>nul
goto :start
:2
echo 已成功将目录文件备份至U盘
ping -n 2 127.0.1>nul
pause>nul
goto :start

Xcopy's extended commands:

/c Ignore errors and continue copying other files

/d Copy new files, the ones that have been copied will not be copied again; if there is a new version file, it will overwrite the old version file

/s Copy non-empty directories and subdirectories. If you omit /s, xcopy will work in a directory

/h Copy files with hidden and system file attributes (by default, xcopy does not copy hidden or system files)

/y Do not prompt to overwrite files

/z Resume uploading

Create a folder:

md "path"

echo display string

pause>nul pause, press any key to continue

:start tag, used with goto statement

goto :start jump to the mark

running result:

Original path:

U Disk:

Tips:

If there are garbled characters, select ANSI for encoding

For U disk, storing a large number of small files is a headache

Can be compressed and then backed up

@echo off

::pt is the folder path to be compressed
set pt=D:\3dMaxWorkplace
:: Need to add environment variable path C:\Program Files\WinRAR;
WinRAR a -r D:\Desktop\automatic backup\"%pt:~3 %" %pt%

::D:\Desktop\Auto Backup\"%pt:~3%" is the storage path of the compressed package, the compressed package is named
pause after the original folder

effect:

If you need to limit the file types to be copied, for example, I only want mp4

Just replace the * sign representing the suffix with mp4

xcopy "!pt!\*.mp4" "%upan%\备份\" /s /h /d /c /y

The difference between copy and xcopy

copy can only copy the specified file, not the specified folder and its subdirectories

set pt="D:\Desktop\测试"
copy /v /y /z "%pt%\*.*" "F:\备份"
pause

Test folder:

Backup folder:

It can be seen that copy can only copy files under the path, not subdirectories and their files

However, copy can display the copy progress bar, but xcopy cannot

The fly in the ointment is that batch processing cannot eject the USB flash drive because there is no USB interface in the command line era.

Guess you like

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