如何调整脚本以复制当月的所有文件?

时间:2015-12-07 18:52:49

标签: windows batch-file

有人可以帮我调整这个脚本,以便复制当月的所有文件吗?

目前它将复制超过3天的所有文件,这对我们无效。

在一个月内,我们会在\\sharename\folder\source创建一个随机数量的文件。

以下是批次代码:

set datetimef=%date:~-4%-%date:~3,2% 
if not exist "\\sharename\folder\%datetimef%" mkdir "\\sharename\folder \%datetimef%" 
forfiles -p "\\sharename\folder\source" -s -m *.xml /D -3 /C "cmd /c copy @file "\\sharename\folder\%datetimef%" 

它似乎按设计工作。

1 个答案:

答案 0 :(得分:1)

下面评论的批处理代码使用命令xcopy(Microsoft文章)复制本月最后修改的所有文件。

@echo off
setlocal
set "SharedFolder=\\sharename\folder"

rem Get year and month from environment variable DATE in format yyyy-mm.
rem It is required for this simple method that environment variable DATE
rem contains the date in format dd/mm/yyyy or dd.mm.yyyy with or without
rem weekday at beginning. If the format of the date string output with
rem echo %DATE% in a command prompt window is different, the line below
rem must be adapted, or the two commented lines with command wmic are
rem used because wmic returns the current date in a format independent
rem on Windows region and language settings.

set "YearMonth=%DATE:~-4%-%DATE:~-7,2%"
rem for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime /VALUE') do set LocalDateTime=%%T
rem set "YearMonth=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%

rem Define source and target folder.
set "TargetFolder=%SharedFolder%\%YearMonth%"
set "SourceFolder=%SharedFolder%\source"

rem Create the target folder if not already existing and verify the
rem successful creation of target folder before copying the files.

if not exist "%TargetFolder%\*" (
    mkdir "%TargetFolder%"
    if errorlevel 1 (
        echo Error detected by %~f0:
        echo.
        echo Failed to create folder %TargetFolder%
        echo.
        echo Check availability of server and access permissions on share.
        echo.
        pause
        endlocal
        goto :EOF
    )
)

rem Define date used below on command XCOPY in format mm-dd-yyyy.
set "XcopyDate=%YearMonth:~5,2%-01-%YearMonth:~0,4%

rem Copy all files last modified this month with archive attribute set.
rem The archive attribute is removed after copying on source file to prevent
rem one more copying operation of same file if this batch file is executed
rem once more this month and source file was not modified since last run.

xcopy "%SourceFolder%\*" "%TargetFolder%\" /C /D:%XcopyDate% /H /I /K /M /Q /R /Y

endlocal

您可以在xcopy(SS64文章)的命令行中附加选项/V/Z

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • mkdir /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • xcopy /?

我还提供了第二个版本,在不使用带参数/D xcopy 的情况下艰难地完成此任务。

@echo off
setlocal EnableDelayedExpansion
set "SharedFolder=\\sharename\folder"

rem Get year and month from environment variable DATE in format yyyy-mm.
rem It is required for this simple method that environment variable DATE
rem contains the date in format dd/mm/yyyy or dd.mm.yyyy with or without
rem weekday at beginning. If the format of the date string output with
rem echo %DATE% in a command prompt window is different, the line below
rem must be adapted, or the two commented lines with command wmic are
rem used because wmic returns the current date in a format independent
rem on Windows region and language settings.

set "YearMonth=%DATE:~-4%-%DATE:~-7,2%"
rem for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime /VALUE') do set LocalDateTime=%%T
rem set "YearMonth=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%

rem Define source and target folder.
set "TargetFolder=%SharedFolder%\%YearMonth%"
set "SourceFolder=%SharedFolder%\source"

rem Create the target folder if not already existing and verify the
rem successful creation of target folder before processing the files.

if not exist "%TargetFolder%\*" (
    mkdir "%TargetFolder%"
    if errorlevel 1 (
        echo Error detected by %~f0:
        echo.
        echo Failed to create folder %TargetFolder%
        echo.
        echo Check availability of server and access permissions on share.
        echo.
        pause
        endlocal
        goto :EOF
    )
)

rem Define and initialize two counters. First one counts how many files
rem are processed successfully and second one counts how many files
rem found to process. Finally those two counts should be always equal.

set "CountProcessed=0"
set "CountTotal=0"

rem Use command DIR to get just the names of all files in source folder
rem without path sorted reverse according to last modification date, i.e.
rem file modified last being returned first and oldest modified file being
rem at end of list.

rem For each file name returned by command DIR get last modification time.
rem It is necessary here to use command FOR for this task as DIR without
rem parameter /S returns just the file name and first FOR would not be
rem able to determine location of file to get last modification time if
rem the source folder is not the current folder which is not the case here.

rem Again it is important to know the format of last modification date/time
rem which depends on Windows region and language settings to correct extract
rem just year and month with a hyphen between to compare with year and month
rem of current date. The last modification time string must start with the
rem date in format dd/mm/yyyy or dd.mm.yyyy to use the code below as is.

rem On first file with a last modification year and month not matching
rem current year and month, the FOR loop processing the file names is
rem exited with a jump to label below the FOR loop as now all other files
rem in list should be outside of current month. This early loop exit could
rem result in a wrong behavior if a file has a last modification date in
rem future in comparison to current date.

rem Copying a file is done using command COPY. This command has some
rem limitations like not overwriting read-only files in target folder.
rem Success on copying the file is evaluated by the batch script. It
rem would be also possible to use XCOPY or ROBOCOPY with the appropriate
rem parameters to copy also hidden or read-only files if this is necessary.

for /F "delims=" %%I in ('dir /A-D /B /O-D /TW "%SourceFolder%\*" 2^>nul') do (

    for %%F in ("%SourceFolder%\%%I") do set "LastModification=%%~tF"

    if not "!LastModification:~6,4!-!LastModification:~3,2!" == "%YearMonth%" goto ProcessingDone

    set /A CountTotal+=1
    copy /B /Y "%SourceFolder%\%%I" "%TargetFolder%" >nul
    if not errorlevel 1 (
        set /A CountProcessed+=1
    ) else (
        echo Failed to copy file %%I
    )
)

:ProcessingDone
if "%CountTotal%" == "1" (
    set "PluralTotal="
) else (
    set "PluralTotal=s"
)
if "%CountProcessed%" == "1" (
    set "PluralProcessed="
) else (
    set "PluralProcessed=s"
)
echo.
echo Processed %CountProcessed% file%PluralProcessed% of total %CountTotal% file%PluralTotal% for %YearMonth%.
endlocal

此批处理代码可用于移动,修改或删除本月最后修改的所有文件,方法是搜索copy不区分大小写,用不同的命令替换命令并调整注释和输出消息。

相关问题