如何根据文件名中的时间戳将某些文件从一个文件夹移动到另一个新文件夹?

时间:2014-09-20 10:45:11

标签: batch-file

我只想创建一个bat文件,将超​​过60天的日志文件移动到另一个新文件夹中,该文件夹应该用时间戳动态创建。

日志文件的格式为“Filename_YYYYMMDD”。

请告诉我,如何通过BAT文件实现此功能?

请建议是否有其他方法可以实现此功能。

2 个答案:

答案 0 :(得分:1)

未经测试(假设文件没有问题中的扩展名)(也假设时间戳应取自文件名):

@echo off

forfiles /m *_*  /d -60  /c  "cmd /e:on /v:on /c for /f 0x22tokens=1,2 delims=_0x22 %%a in (0x22@file0x22) do (md 0x22%%~b0x22>nul 2>&1 & move 0x22%%~a_%%~b 0x22%%~b )"

也可以从命令提示符执行:

forfiles /m *_*  /d -60  /c  "cmd /e:on /v:on /c for /f 0x22tokens=1,2 delims=_0x22 %a in (0x22@file0x22) do (md 0x22%~b0x22>nul 2>&1 & move 0x22%~a_%~b 0x22%~b )"

答案 1 :(得分:1)

使用日期和时间是Windows批处理中的一个难点。我写了一个hybrid JScript/batch utility called getTimestamp.bat,可以很容易地处理日期和时间。它是纯脚本,可​​以在XP前面的任何现代Windows机器上运行。我下面的批处理解决方案使用getTimestamp.bat。

这是一个解决方案,没有任何文档。它使用名称中嵌入的日期。它忽略了文件的最后修改属性:

@echo off
setlocal disableDelayedExpansion
call getTimestamp -od -60 -f {yyyy}{mm}{dd} -r cutoff
for /f "eol=: delims=" %%F in (
  'dir /b /a-d *_*.log | findstr /ir ".*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.log"'
) do (
  set "file=%%F"
  set "name=%%~nF"
  setlocal enableDelayedExpansion
  set "dt=!name:~-8!"
  if !dt! lss %cutoff% (
    if not exist !dt!\ md !dt!
    move "!file!" !dt! >nul
  )
  endlocal
)

以下是完全相同的代码,完整记录:

@echo off

:: Outside of a code block, lines beginning with :: are comments
:: Within a code block, comments are enclosed within %= coment =%
:: Note that %=comment=% style comments cannot containn : or %

:: This solution uses a FOR loop to process file names, and names may contain !
:: FOR variable values are corrupted if they contain ! and are expanded with
:: delayed expansion enabled. So start out with delayed expansion disabled.
setlocal disableDelayedExpansion

:: Establish the cutoff by subtracting 60 days from the current date
call getTimestamp -od -60 -f {yyyy}{mm}{dd} -r cutoff

:: This outer FOR /F loop iterates the result of a command.
:: The chosen command lists the candidate files, one per line.
:: EOL=: guards against unlikely event of file name beginning with ;
:: Names that begin with EOL character will be skipped. Default EOL is ;
:: No file name can contain : so it is a safe EOL value to use.
:: DELIMS= nothing prevents name from being chopped up.
for /f "eol=: delims=" %%F in (

  %= The DIR /B option gives a bare listing consisting only of file name.   =%
  %= The DIR /A-D option screens out folder names.                          =%
  %= The DIR file mask gives an inexact file name filter.                   =%
  %= The result is piped to FINDSTR with a regular expression that exactly  =%
  %= specifies the file name filter.
  'dir /b /a-d *_*.log | findstr /ir ".*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.log"'

) do (

  %= Save the full name and extension of the file =%
  set "file=%%F"

  %= Get the base name of the file (without extension) =%
  set "name=%%~nF"

  %= Delayed expansion required to access a variable =%
  %= within the same block that defined it.          =%
  setlocal enableDelayedExpansion

  %= get the date string from the last 8 characters of name  =%
  set "dt=!name:~-8!"

  %= Verify that the file date string is less than the cutoff date =%
  if !dt! lss %cutoff% (

    %= Create the target folder if it does not already exist =%
    if not exist !dt!\ md !dt!

    %= Move the file to the target folder.                  =%
    move "!file!" !dt! >nul
  )

  %= SETLOCAL must be ended if used within a loop because stack is limited =%
  endlocal
)