我找到了一个批处理脚本,用于计算以天为单位的文件的年龄,并将结果输出到屏幕。我正在尝试修改此脚本,以便它确定文件夹中每个文件的“自文件修改日期起的天数”,并将结果输出到文本文件。
我已经在剧本上摆弄了几天,如果有人能帮助我,我会很感激。
谢谢!
@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
echo.%%~nxF is !diff! days old
)
ECHO.&PAUSE&GOTO:EOF
::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219
:$source http://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
答案 0 :(得分:0)
以下是为当前目录中除批处理文件和创建的日志文件之外的所有文件创建的批处理代码,以CSV格式输出以及列:
带有附加注释的批次代码。
@echo off
setlocal
set "LogFile=temp_log.txt"
call :jdate tnow "%date%"
(
echo "Name","Modification Time","Creation Time","Days Old"
for %%F in (*) do call :GetDiffTime "%%F"
) >"%LogFile%"
endlocal
goto :EOF
:GetDiffTime
rem Do not add this batch file in log file.
if "%~1" == "%~nx0" goto :EOF
rem Do not add log file in log file.
if "%~1" == "%LogFile%" goto :EOF
rem Determine difference in days of last modification
rem date of current file with current date in days.
call :ftime tfile "%~1"
set /A diff=tnow-tfile
rem Get creation date of current file from output of DIR.
rem The tokens are:
rem a ... creation date
rem b ... creation time
rem c ... file size in bytes
rem d ... file name without path
for /F "tokens=1-3*" %%a in ('dir /t:c "%~1"') do (
if "%%d" == "%~1" (
echo "%~1","%~t1","%%~a %%~b",%diff%
goto :EOF
)
)
goto :EOF
::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (
call :jdate JD "- %~t2"
) ELSE (
for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call :jdate JD "%%a"
)
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219
:$source http://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C
)
)
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
rem For German Windows comment line above and uncomment line below.
rem set /a "yy=10000%JJ% %%10000,mm=100%MM% %% 100,dd=100%TT% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
注意:强>
功能jdate
专为12/30/2015
或Wed 12/30/2015
或30.12.2015
等日期设计,但不能用于德语Windows作为命令输出日期不是代码所期望的dd-mm-yy
。命令日期在德语Windows TT-MM-JJ
上输出作为日期格式掩码。德语Windows的用户应该阅读我添加的批处理脚本末尾附近的两行 rem 行。
示例:
在德语Windows上的目录C:\Temp
输出中执行命令 DIR :
15.08.2015 22:01 2.286 about.txt
27.12.2014 21:28 8.871 help for.txt
15.03.2015 17:41 2.372 RegJump.bat
04.01.2016 12:24 2.937 Test.bat
10.11.2015 17:44 9.003.869 TV Manual.pdf
在德语版Windows上运行Test.bat
,上面的代码适用于德语Windows,会生成包含内容的文件C:\Temp\temp_log.txt
:
"Name","Modification Time","Creation Time","Days Old"
"about.txt","15.08.2015 22:01","04.01.2016 12:30",142
"help for.txt","27.12.2014 21:28","27.12.2014 21:28",373
"RegJump.bat","15.03.2015 17:41","15.03.2015 15:23",295
"TV Manual.pdf","10.11.2015 17:44","10.11.2015 17:40",55
文件about.txt
的创建日期比上次修改日期更新,因为只是复制到目录C:\Temp
以测试批处理文件,因此创建日期是制作此副本的当前日期。
以上是上面批量代码的增强版本,但有以下不同之处:
@echo on
或不关闭命令输出,也会产生正确的输出。/R
以仅处理当前目录中的文件。HLA Compiler (masm32+)
,其中包含左括号和右括号。即使仅在当前目录上运行,此批处理文件也会将文件名始终以完整路径写入输出文件。但是,可以在子例程GetDiffTime
中的 echo 行中使用例如%~nx1
而不是%~1
来更改这一点,这需要在子例程ftime
中进行改进。 / p>
@echo off
setlocal
set "LogFile=%CD%\temp_log.txt"
call :jdate tnow "%date%"
echo "Name","Modification Time","Creation Time","Days Old">"%LogFile%"
for /R %%F in (*) do call :GetDiffTime "%%~fF"
endlocal
goto :EOF
:GetDiffTime
rem Do not add this batch file in log file.
if "%~1" == "%~f0" goto :EOF
rem Do not add log file in log file.
if /I "%~1" == "%LogFile%" goto :EOF
rem Determine difference in days of last modification
rem date of current file with current date in days.
call :ftime tfile "%~1"
set /A diff=tnow-tfile
rem Get creation date of current file from output of DIR.
rem The tokens are:
rem a ... creation date
rem b ... creation time
rem c ... file size in bytes
rem d ... file name without path
for /F "tokens=1-3*" %%a in ('dir /t:c "%~1"') do (
if "%%d" == "%~nx1" (
>>"%LogFile%" echo "%~1","%~t1","%%~a %%~b",%diff%
goto :EOF
)
)
goto :EOF
::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL EnableDelayedExpansion
set "file=%~2"
set "attr=%~3"
if not defined attr (
call :jdate JD "- %~t2"
) ELSE (
for /f %%a in ('"dir %attr% /-c "!file!"|findstr "^^[0-9]""') do call :jdate JD "%%a"
)
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET "%~1=%JD%") ELSE (echo.%JD%)
)
EXIT /b
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219
:$source http://www.dostips.com
SETLOCAL
set "DateStr=%~2" & if "%~2"=="" set "DateStr=%date%"
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set "%%a=%%A" & set "%%b=%%B" & set "%%c=%%C"
)
)
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
rem For German Windows comment line above and uncomment line below.
rem set /a "yy=10000%JJ% %%10000,mm=100%MM% %% 100,dd=100%TT% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b