自动复制在某个文件夹中出现的每个文件

时间:2017-08-17 07:36:52

标签: batch-file automation

我将工作所需的所有文件(word文档,Excel工作表,zip存档等)保存到某个文件夹中。在我完成这些文件后,我删除了它们。但有时我需要在我删除的旧文档中查找内容。所以我需要自动将工作文件夹中的所有文件保存到另一个文件夹中。喜欢备份。有人可以用scipt帮助我吗?

P.S。我一天收到大量文件,因此手动保存所有文件需要很长时间。我可以运行一个脚本为我做这个。此外,scipt必须在文件出现时立即保存。

1 个答案:

答案 0 :(得分:0)

我使用xcopy命令批量创建了此批处理脚本以进行增量备份和计划备份。

此脚本的第一次执行是配置源和目标的路径。并将它们保存在.cfg文件中,然后第一次制作完整副本。

它创建一个计划任务,每小时运行一次增量副本(即:仅复制新文件或已从源文件中修改)

因此,您可以尝试使用此批处理脚本: Incremental_Backup.bat

@echo off
:: Incremental_Backup.bat Created by Hackoo on 12/08/2017
:: It is a total copy first and then incrementally,
:: ie, it just copies the new files and changed files.
:: Create a Schedule Task for Copying files Hourly
Mode con cols=95 lines=5 & color 0E
Title %~nx0 for Incremental Backup with XCopy Command by Hackoo 2017
set "Settings=%~dpn0_Settings.cfg"
Set "FirstFull_CopyLog=%~dpn0_FirstFull_CopyLog.txt"
Set "LogFile=%~dpn0_Incremental_CopyLog.txt"
Set "TaskName=Backup_Task"
Rem The repeated task is in minutes (60 min = 1 hour)
Set "Repeat_Task=60"
If not exist "%Settings%" (
    Call :BrowseForFolder "Please choose the source folder for the backup" SourceFolder
    Setlocal EnableDelayedExpansion
    If defined SourceFolder (
        echo(
        echo             You chose "!SourceFolder!" as source folder
    ) else (
        echo(
        Color 0C & echo                    The source folder is not defined ... Exiting ......
        Timeout /T 2 /nobreak>nul & exit
    )
    Call :BrowseForFolder "Please choose the target folder for the backup" TargetFolder
    If defined TargetFolder (
        echo(
        echo             You chose "!TargetFolder!" as Target folder
    ) else (
        echo(
        Color 0C & echo                    The Target folder is not defined ... Exiting ......
        Timeout /T 2 /nobreak>nul & exit
    )
Timeout /T 3 /nobreak>nul
    (
        echo "!SourceFolder!" 
        echo "!TargetFolder!\Backups_%ComputerName%\" 
    )> "%Settings%"
cls & echo( & echo(
echo         Please wait a while ... The Backup to "!TargetFolder!\Backups_%ComputerName%\" is in progress... 
Call :Backup_XCopy "!SourceFolder!" "!TargetFolder!\Backups_%ComputerName%\" "!FirstFull_CopyLog!"
Timeout /T 1 /nobreak>nul 
Call :Create_Schedule_Task_Copy %Repeat_Task% %TaskName%
Start "" "!FirstFull_CopyLog!" & exit
) else (
Setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%Settings%"') do (
    set /a idx+=1
    set Param[!idx!]=%%a
)

Set "SourceFolder=!Param[1]!"
Set "TargetFolder=!Param[2]!"

Cls & echo( & echo(
echo       Please wait a while ... The Backup to !TargetFolder! is in progress... 
Call :Backup_XCopy !SourceFolder! !TargetFolder! !LogFile!
Rem Just to query the Backup_Task and log it
    (
        echo(
        echo %Date% @ %Time%
        echo( 
        echo ======================================== ====================== ===============
        @for /f "skip=2 delims=" %%a in ('Schtasks /Query /TN %TaskName%') do (
            @echo %%a
        )
        echo ======================================== ====================== ===============
    )>> !LogFile!
)
Timeout /T 1 /nobreak>nul 
Exit
::****************************************************************************
:BrowseForFolder
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'%1',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "%2=%%I"
exit /b
::****************************************************************************
:Backup_XCopy <Source> <Target> <LogFile>
Xcopy /I /D /Y /S /E /J /C /F %1 %2 > %3 2>&1
Exit /b
::****************************************************************************
:Create_Schedule_Task_Copy <Repeat_Task_Every(N)Minute> <TaskName>
(
    Schtasks /create /SC minute /MO %1 /TN "%2" /TR "%~f0" 
    Schtasks /Query /TN "%2" 
)>> "!FirstFull_CopyLog!" 2>&1
exit /b
::****************************************************************************