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

默斯基

在我的一生中,我无法弄清楚如何将任何形式的输出从我的脚本中输入到日志文件中。我尝试使用“/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
迈克尔·希思
@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使用附加日志参数和无进度参数。robocopy参数可以用反斜杠转义双引号,即\"是文字"因此,我避免使用带有尾随反斜杠的路径。

不确定 的值FileToFind什么,因为它未定义,所以我将它设置为*. for循环得到第一个配置文件夹,并运行robocopy它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

批处理脚本以获取文本文件的大小

来自分类Dev

是否可以使用批处理脚本编写 FFmpeg 脚本以并行编码多个文件?

来自分类Dev

批处理脚本以更改txt文件

来自分类Dev

如何编写脚本以获取Filemaker上的总记录数

来自分类Dev

批处理脚本以获取本地计算机的所有用户列表

来自分类Dev

CMD 上的批处理脚本以获取变量上的 COM 端口

来自分类Dev

如何编写批处理脚本以基于文件夹名称搜索特定文件?

来自分类Dev

转换脚本以独立处理多个表

来自分类Dev

如何从批处理脚本的Exec调用中获取ResultCode?

来自分类Dev

如何使用批处理脚本仅获取修改年份?

来自分类Dev

如何从批处理脚本的Exec调用中获取ResultCode?

来自分类Dev

批处理脚本以查找运行Apache Tomcat的端口

来自分类Dev

Windows批处理脚本以查找免费文件名

来自分类Dev

批处理/ VB脚本以设置系统提示音

来自分类Dev

编写批处理脚本以按“ Tab”和“ Enter”键

来自分类Dev

批处理脚本以查找特定的目录名称

来自分类Dev

批处理脚本以提取特定单词之间的行

来自分类Dev

批处理脚本以检查Windows 7激活

来自分类Dev

批处理命令/脚本以从.txt文件执行命令?

来自分类Dev

Windows批处理脚本以查找免费文件名

来自分类Dev

DOS批处理脚本以遍历NTFS驱动器

来自分类Dev

批处理脚本以找出主机是启动还是关闭

来自分类Dev

编写批处理脚本以查找子目录

来自分类Dev

构建DOS批处理脚本以重命名文件

来自分类Dev

批处理脚本以删除特定的文件夹

来自分类Dev

批处理脚本以启动应用程序

来自分类Dev

批处理脚本以创建大文本文件

来自分类Dev

批处理脚本以交换单词/重命名文件

来自分类Dev

批处理脚本以检查Windows 7激活

Related 相关文章

  1. 1

    批处理脚本以获取文本文件的大小

  2. 2

    是否可以使用批处理脚本编写 FFmpeg 脚本以并行编码多个文件?

  3. 3

    批处理脚本以更改txt文件

  4. 4

    如何编写脚本以获取Filemaker上的总记录数

  5. 5

    批处理脚本以获取本地计算机的所有用户列表

  6. 6

    CMD 上的批处理脚本以获取变量上的 COM 端口

  7. 7

    如何编写批处理脚本以基于文件夹名称搜索特定文件?

  8. 8

    转换脚本以独立处理多个表

  9. 9

    如何从批处理脚本的Exec调用中获取ResultCode?

  10. 10

    如何使用批处理脚本仅获取修改年份?

  11. 11

    如何从批处理脚本的Exec调用中获取ResultCode?

  12. 12

    批处理脚本以查找运行Apache Tomcat的端口

  13. 13

    Windows批处理脚本以查找免费文件名

  14. 14

    批处理/ VB脚本以设置系统提示音

  15. 15

    编写批处理脚本以按“ Tab”和“ Enter”键

  16. 16

    批处理脚本以查找特定的目录名称

  17. 17

    批处理脚本以提取特定单词之间的行

  18. 18

    批处理脚本以检查Windows 7激活

  19. 19

    批处理命令/脚本以从.txt文件执行命令?

  20. 20

    Windows批处理脚本以查找免费文件名

  21. 21

    DOS批处理脚本以遍历NTFS驱动器

  22. 22

    批处理脚本以找出主机是启动还是关闭

  23. 23

    编写批处理脚本以查找子目录

  24. 24

    构建DOS批处理脚本以重命名文件

  25. 25

    批处理脚本以删除特定的文件夹

  26. 26

    批处理脚本以启动应用程序

  27. 27

    批处理脚本以创建大文本文件

  28. 28

    批处理脚本以交换单词/重命名文件

  29. 29

    批处理脚本以检查Windows 7激活

热门标签

归档