Bat code of migrating picture directory

Time:2020-4-30
Copy codeThe code is as follows:
@echo off
setlocal enabledelayedexpansion
: enable variable delay, add custom variable name
set sourpic=e:\pic
:: define picture source
set despic=e:\img
:: define image storage address
set pictype=*.jpg
:: define picture type
set log=e:\log.txt
: define log information, which will be regenerated every time

REM processing start logging
Echo image from:% sourpic% >% log%
Echo upload directory:% despic% > >% log%
Echo picture type:% pictype% > >% log%
Echo start time:% date%% time% > >% log%

: after traversing the folders, traverse the corresponding folders one by one, copy pictures, log records and delete pictures at the same time

for /d %%i in (%sourpic%\*) do (
:: traverse the root folder to get the name of the subfolder
set folder= %%~ni
Echo operate on! Folder
Echo to! Folder!:% date%% time% > >% log%
REM starts to judge the directory of the server
Call :serverfolder %despic% !folder! %log%
REM starts traversing picture files in this folder
Call :copyfile %%~fi !folder!
)
Echo% date%% time% — operation completed > >% log%
pause
exit

REM manages the directory of the target server
:serverfolder
if exist %1\%2 (
Directory exists on echo server side:% 1 \% 2
) else (
Echo started creating Directory:% 1 \% 2
md %1\%2
if %ERRORLEVEL% ==0 (
Echo server created% 1 \% 2 successfully! >>%3
)else (
Echo server failed to create% 1 \% 2! >>%3
)
)
goto:eof

REM processing files for subfolders
:copyfile
for /r %1 %%i in (%pictype%) do (
Echo start copying:%% I
echo %date%%time%–%%i>>%log%
copy %%i %despic%\%2\%%~ni%%~xi /y
if %ERRORLEVEL% ==0 (
Echo copy succeeded > >% log%
REM starts the deletion
del %%i /q
if %ERRORLEVEL% ==0 (
Echo deleted successfully > >% log%
)else (
Echo deletion failed > >% log%
)
)else (
Echo copy failed > >% log%
)
)
goto:eof