Batch file 如何根据文件名中的时间戳将某些文件从一个文件夹移动到另一个新文件夹?
我只想创建一个bat文件,将超过60天的日志文件移动到另一个新文件夹中,该文件夹应使用时间戳动态创建 日志文件的格式为“Filename_YYYYMMDD” 请让我知道,如何通过BAT文件实现此功能 请建议是否有其他方法实现此功能。未测试(假设文件没有问题中的扩展名)(还假设时间戳应从文件名中获取): 也可以从命令提示符执行:Batch file 如何根据文件名中的时间戳将某些文件从一个文件夹移动到另一个新文件夹?,batch-file,Batch File,我只想创建一个bat文件,将超过60天的日志文件移动到另一个新文件夹中,该文件夹应使用时间戳动态创建 日志文件的格式为“Filename_YYYYMMDD” 请让我知道,如何通过BAT文件实现此功能 请建议是否有其他方法实现此功能。未测试(假设文件没有问题中的扩展名)(还假设时间戳应从文件名中获取): 也可以从命令提示符执行: forfiles /m *_* /d -60 /c "cmd /e:on /v:on /c for /f 0x22tokens=1,2 delims=_0x22 %
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 )"
在Windows批处理中处理日期和时间是一件痛苦的事情。我已经写了一篇文章,可以轻松地处理日期和时间。这是一个纯脚本,可以在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
)
为什么不自己试一试,然后问自己是否面临着除了“问”答案以外的任何问题?嗨,visu,欢迎来到SO。如果你先尝试一下自己,人们更可能会给你一个好的答案,然后如果你遇到问题,在这里用一些源代码拍摄:)+1,只要上次修改的日期与文件名中的日期匹配(当然假设没有bug),这应该可以很好地工作。但我不确定是否可以假设文件自创建以来就没有被编辑过。编辑-从文件名中简化日期提取您需要一个代码库来存储您所编写的所有工具。通过dostips线程迭代来找到我可能需要的工具并不有趣:-)
@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
)