为什么此备份批处理文件将隐藏的系统文件夹创建为目的地?

时间:2019-05-11 04:30:43

标签: batch-file xcopy

我从这里获得了此批处理文件,并且运行良好。我唯一遇到的问题是要创建的文件夹(甚至已经使用的文件夹也已使用)变成了隐藏的系统文件夹。

@echo off
REM get start time
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

REM set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
set "logtimestamp=%YYYY%.%MM%.%DD% %HH%:%Min%:%Sec%"

REM actual copy
set source=%1
set destination=%2

REM create the exclusion list
set exclusion=%3
set exclusion=%exclusion:"=%
(for %%i in (%exclusion%) do echo %%i) > exclusion.txt

REM set the file name for the logging data
set log=log-%fullstamp%.txt

REM start the backup process
echo // started backup at %logtimestamp% > %log%
echo // from %~f1 to %~f2\ >> %log%

echo ---- >> %log%
xcopy %source% %destination% /S /E /C /D /R /Y /V /I /EXCLUDE:exclusion.txt >> %log%
echo ---- >> %log%
del /f exclusion.txt

REM get finish time
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "logtimestamp=%YYYY%.%MM%.%DD% %HH%:%Min%:%Sec%"

echo // finished backup at %logtimestamp% >> %log%

REM move the logging
if not exist "%destination%\.backup_log" mkdir %destination%\.backup_log
move %log% %destination%\.backup_log

1 个答案:

答案 0 :(得分:0)

这是您的脚本示例,进行了一些更改,最重要的是将保存日志文件的目录名称从.backup_log更改为backup_logs

@Echo Off
SetLocal DisableDelayedExpansion

Rem Actual copy
Set "source=%~1"
Set "destination=%~2"
Set "logdir=%~2\backup_logs"

Rem Create the exclusion list
(For %%A In (%~3)Do Echo(%%A)>"exclusion.txt"

Rem Ensure log directory exists
If Not Exist "%logdir%\" MD "%logdir%"

Rem Get start time
Call :Stamp

Rem Set the file name for the logging data
Set "log=log-%fs%.txt"

Rem Start the backup process
(   Echo // Started backup at %ls%
    Echo // from %~1 to %~2
    Echo ----
    XCopy "%source%" "%destination%" /E /C /D /R /Y /V /I /EXCLUDE:"exclusion.txt"
    Echo ----

    Rem Get finish time
    Call :Stamp
    SetLocal EnableDelayedExpansion
    Echo // Finished backup at !ls!
    EndLocal)>"%logdir%\%log%"
Del "exclusion.txt"
EndLocal
GoTo :EOF

:Stamp
Rem Get date and time stamps
Set "fs="
For /F "Tokens=1-6Delims=/: " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined fs (Set "fs=%%A-%%B-%%C_%%D-%%E-%%F"
    Set "ls=%%A.%%B.%%C %%D:%%E:%%F")
Exit /B

您可能会使用类似于以下内容的命令从命令提示符处运行脚本:

C:\Users\Dale>"My Scripts\backup.cmd" Documents D:\Backups\MyDocs ".lnk ~$ \temp\"