如何:获取批处理脚本以记录多个robocopies

时间:2018-06-12 21:39:37

标签: batch-file cmd

对于我的生活,我无法弄清楚如何从我的脚本中获取任何形式的输出到日志文件中。我试图使用" / log +:"来记录ROBOCOPY。我没有得到任何输出以这种方式记录。所以现在我想我可以使用重定向">"或">>"。这也不适合我,我还没有找到适合我的解决方案。任何帮助表示赞赏。

@Echo OFF
setlocal EnableExtensions EnableDelayedExpansion
REM Backup Script from Windows 7 to Windows 10
REM Made By Adam Avila
REM Date: 6/7/2018
Set Destination="C:\Users\%Username%\BackupS2018"
Set LogFile="%Destination%\Log"

REM This part of the script sets the variables
Set Desktop=C:\Users\%Username%\Desktop\
Set Favorites=C:\Users\%Username%\Favorites\
Set ChromeBookmarks="C:\Users\%Username%\AppData\Local\Google\Chrome\User Data\Default"
Set FirefoxPath=C:\Users\%Username%\AppData\Roaming\Mozilla\Firefox\profiles\
Set UProof=C:\Users\%Username%\AppData\Roaming\Microsoft\Uproof\
Set Templates=C:\Users\%Username%\AppData\Roaming\Microsoft\Templates\
Set SSH=C:\Users\%Username%\AppData\Roaming\SSH\
Set Skype=C:\Users\%Username%\AppData\Roaming\Skype\
Set FileZilla=C:\Users\%Username%\AppData\Roaming\FileZilla\
Set Box=C:\Users\%Username%\AppData\Roaming\Box\
Set Snagit=C:\Users\%Username%\AppData\Local\TechSmith\SnagIt\
Set Cisco=C:\Users\%Username%\AppData\Local\Cisco\
Set StickyNotes="C:\Users\%Username%\AppData\Roaming\Microsoft\Sticky Notes"
Set NewStickyNotes=C:\Users\%Username%\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\

REM This part of the script restores data.
If Exist %Destination% (
If Exist %Destination%\Desktop (ROBOCOPY %Destination%\Desktop %Desktop% /S)
If Exist %Destination%\Favorites (ROBOCOPY %Destination%\Favorites %Favorites% /S)
If Exist %Destination%\ChromeBookmarks (ROBOCOPY %Destination%\ChromeBookmarks %ChromeBookmarks% /S)
If Exist %Destination%\UProof (ROBOCOPY %Destination%\UProof %UProof% /S)
If Exist %Destination%\Templates (ROBOCOPY %Destination%\Templates %Templates% /S)
If Exist %Destination%\SSH (ROBOCOPY %Destination%\SSH %SSH% /S)
If Exist %Destination%\Skype (ROBOCOPY %Destination%\Skype %Skype% /S)
If Exist %Destination%\FileZilla (ROBOCOPY %Destination%\FileZilla %FileZilla% /S)
If Exist %Destination%\Cisco (ROBOCOPY %Destination%\Cisco %Cisco% /S)
If Exist %Destination%\Box (ROBOCOPY %Destination%\Box %Box% /S)
If Exist %Destination%\Snagit (ROBOCOPY %Destination%\Snagit %Snagit% /S)
If Exist %Destination%\StickyNotes (
taskkill /im Microsoft.Notes.exe /t /f
ROBOCOPY %Destination%\StickyNotes %NewStickyNotes% StickyNotes.snt /S)
Rename %NewStickyNotes%\StickyNotes.snt ThresholdNotes.snt
)

) > "%LogFile%\RestoreLog.txt" Else (

REM This part of the script backups data.
If Exist %Desktop% (ROBOCOPY %Desktop% %Destination%\Desktop /S)
If Exist %Favorites% (ROBOCOPY %Favorites% %Destination%\Favorites /S)
If Exist %ChromeBookmarks%\Bookmarks (ROBOCOPY %ChromeBookmarks% %Destination%\ChromeBookmarks Bookmarks /S)
If Exist %UProof% (ROBOCOPY %UProof% %Destination%\UProof /S)
If Exist %Templates% (ROBOCOPY %Templates% %Destination%\Templates /S)
If Exist %SSH% (ROBOCOPY %SSH% %Destination%\SSH /S)
If Exist %Skype% (ROBOCOPY %Skype% %Destination%\Skype /S)
If Exist %FileZilla% (ROBOCOPY %FileZilla% %Destination%\FileZilla /S)
If Exist %Box% (ROBOCOPY %Box% %Destination%\Box /S)
If Exist %Snagit% (ROBOCOPY %Snagit% %Destination%\Snagit /S)
If Exist %Cisco% (ROBOCOPY %Cisco% %Destination%\Cisco /S)
If Exist %StickyNotes% (ROBOCOPY %StickyNotes% %Destination%\StickyNotes /S)
If Exist %FirefoxPath% (for /F %%I in ('dir "%FirefoxPath%\!FileToFind!" /A-D /B /S 2^>nul') do (
set "FoundIt=%%~dpI"
goto FileFound
)
:FileFound
ROBOCOPY %FoundIt% %Destination%\FirefoxBookmarks places.sqlite /S
)
) > "%LogFile%\BackupLog.txt"
pause

1 个答案:

答案 0 :(得分:0)

@echo off
setlocal enableextensions enabledelayedexpansion
REM Backup Script from Windows 7 to Windows 10
REM Made By Adam Avila
REM Date: 6/7/2018
Set "Destination=%UserProfile%\BackupS2018"
Set "LogDir=%Destination%\Log"

REM Determine function and set logfile path.
If exist "%Destination%" (
    set "function=restore"
    set "LogFile=%LogDir%\RestoreLog.txt%"
) else (
    set "function=backup"
    set "LogFile=%LogDir%\BackupLog.txt"
)

if not exist "%LogDir%" md "%LogDir%"

REM Each argument of "name|target" is split at the pipe to make 2 arguments.
for %%A in (
 "Desktop|%UserProfile%\Desktop"
 "Favorites|%UserProfile%\Favorites"
 "ChromeBookmarks|%LocalAppData%\Google\Chrome\User Data\Default"
 "FirefoxPath|%AppData%\Mozilla\Firefox\profiles"
 "UProof|%AppData%\Microsoft\Uproof"
 "Templates|%AppData%\Microsoft\Templates"
 "SSH|%AppData%\SSH"
 "Skype|%AppData%\Skype"
 "FileZilla|%AppData%\FileZilla"
 "Box|%AppData%\Box"
 "Snagit|%LocalAppData%\TechSmith\SnagIt"
 "Cisco|%LocalAppData%\Cisco"
 "StickyNotes|%AppData%\Microsoft\Sticky Notes"
) do for /f "tokens=1,2 delims=|" %%B in ("%%~A") do call :%function% "%%~B" "%%~C"

REM Remove any possible empty folders so rerun chooses correct function.
for %%A in (
 "%LogDir%"
 "%Destination%"
) do if exist "%%~A" rd "%%~A" 2>nul

exit /b


:restore
setlocal
set "name=%~1"
set "target=%~2"

if "%name%" == "StickyNotes" (
    2>nul taskkill /im Microsoft.Notes.exe /t /f
    set "NewStickyNotes=%LocalAppData%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
    robocopy "%Destination%\%name%" "!NewStickyNotes!" StickyNotes.snt /S /np /log+:"%LogFile%"
    if exist "%NewStickyNotes%\StickyNotes.snt" (
        rename "%NewStickyNotes%\StickyNotes.snt" ThresholdNotes.snt
    )
    exit /b 0
)

robocopy "%Destination%\%name%" "%target%" /S /log+:"%LogFile%"
exit /b 0


:backup
setlocal
set "name=%~1"
set "target=%~2"

if not exist "%target%" exit /b 0

if "%name%" == "ChromeBookmarks" (
    if not exist "%target%\Bookmarks" exit /b 0
    robocopy "%target%" "%Destination%\%name%" Bookmarks /S /np /log+:"%LogFile%"
    exit /b 0
)

if "%name%" == "FirefoxPath" (
    set "FileToFind=*"

    for /f %%I in ('dir "%target%\!FileToFind!" /A-D /B /S 2^>nul') do (
        set "firefox_profile=%%~dpI"
        if "!firefox_profile:~-1!" == "\" set "firefox_profile=!firefox_profile:~,-1!"

        robocopy "!firefox_profile!" "%Destination%\FirefoxBookmarks" places.sqlite /S /np /log+:"%LogFile%"
        exit /b 0
    )
    exit /b 0
)

robocopy "%target%" "%Destination%\%name%" /S /np /log+:"%LogFile%"
exit /b 0

你在命名中使用了一个很好的模式,所以我创建了它 代码,以尽量减少名称重复等。

初始for循环使用name|target的模式 由|拆分为2个参数并传递给 call。 调用什么标签取决于目标是否存在。

robocopy使用append logging参数和no 进步论证。 robocopy个参数可以双倍转义 带有反斜杠的引号,即\"是文字"。 因此,我避免使用带有反斜杠的路径。

不确定FileToFind的值是什么,因为它未定义 所以我把它设置为*for循环获取第一个配置文件 文件夹并在其上运行robocopy