I am trying to set a variable to the number of days since a log file was modified. this way I can use that value as a flag for the next time a command runs.
我试图将变量设置为自修改日志文件以来的天数。这样我就可以将该值用作下次命令运行时的标志。
example:
if the command ran yesterday, the value will be 1
如果命令昨天运行,则值为1
if the command ran 5 days ago, the value will be 5
如果命令在5天前运行,则值为5
I'm seeing methods for similar issues such as:
我正在看到类似问题的方法,例如:
OR %%f IN (%filename%) DO SET filedatetime=%%~tf
or
if (file.exists()) {
Date lastModified = new Date(file.lastModified());
}
I just don't know batch scripting or powershell very well. I'm open to running this script in either. I think what I need to learn is:
我只是不知道批处理脚本或PowerShell。我愿意在两者中运行这个脚本。我想我需要学习的是:
1) extract the (tokens?) sub string of just the date from a date/time modified command
1)从日期/时间修改命令中提取日期(令牌?)子字符串
2) removing any colons or slashes
2)去除任何冒号或斜线
3) convert to an integer
3)转换为整数
4) repeat for today's date
4)重复今天的约会
Once I have two integers subtracting and setting a variable should be easy.
一旦我有两个整数减去和设置变量应该很容易。
Any recommendations?
3 个解决方案
#1
2
In PowerShell you can just subtract one date from another and get a timespan. So something as simple as:
在PowerShell中,您可以从另一个日期中减去一个日期并获得一个时间跨度。所以简单如下:
$File = Get-Item C:\Path\To\File.txt
$DaysSinceMod = [datetime]::now - $File.LastWriteTime | Select -Expand Days
Then $DaysSinceMod
would be how many days since the file was last modified. Or, using some shorthand, that can be reduced to:
然后$ DaysSinceMod将是文件上次修改后的天数。或者,使用一些简写,可以简化为:
$LastMod=(get-date)-(gi C:\Path\To\File.txt)|% Days
#2
1
Using a Batch file, you may convert a date into a Julian Day Number in a very simple way via a conversion "function", and then use the number in any way you wish:
使用批处理文件,您可以通过转换“功能”以非常简单的方式将日期转换为儒略日编号,然后以您希望的任何方式使用该数字:
@echo off
setlocal EnableDelayedExpansion
rem Define the "Date to Julian Day Number" conversion function
set "DateToJDN(YMD)=( a=(YMD), y=a/10000, a%%=10000, m=a/100, d=a%%100, a=(m-14)/12, (1461*(y+4800+a))/4+(367*(m-2-12*a))/12-(3*((y+4900+a)/100))/4+d-32075 )"
rem Get the JDN of today's date
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A "today=!DateToJDN(YMD):YMD=%%c%%a%%b!"
rem Get the JDN of each file, subtract it from today's JDN and show the elapsed days
for %%f in (*.csv) do for /F "tokens=1-3 delims=/ " %%a in ("%%~Tf") do (
set /A "daysOld=today - !DateToJDN(YMD):YMD=%%c%%a%%b!"
echo !daysOld! - %%f
)
For example, using these files:
例如,使用这些文件:
07/04/2018 07:45 p. m. 135 data.csv
07/04/2018 07:45 p. m. 79 data_1.csv
07/04/2018 07:45 p. m. 65 data_2.csv
06/02/2018 07:41 p. m. 104 file1.csv
03/06/2017 06:02 p. m. 5,534 file2.csv
14/04/2018 10:49 p. m. 2,674 HCT_ACT_01.csv
14/04/2018 10:49 p. m. 714 HCT_ACT_02.csv
14/04/2018 10:49 p. m. 2,010 HCT_ACT_03.csv
21/03/2018 09:15 p. m. 313 msource1.csv
21/03/2018 09:16 p. m. 402 msource2.csv
21/03/2018 09:16 p. m. 402 msource3.csv
06/10/2017 09:18 p. m. 156 output.csv
03/06/2017 06:11 p. m. 2,017 Reconresult.csv
21/03/2018 09:41 p. m. 491 result.csv
20/12/2017 12:25 a. m. 305 source.csv
This is the output:
这是输出:
135 - data.csv
135 - data_1.csv
135 - data_2.csv
195 - file1.csv
443 - file2.csv
128 - HCT_ACT_01.csv
128 - HCT_ACT_02.csv
128 - HCT_ACT_03.csv
152 - msource1.csv
152 - msource2.csv
152 - msource3.csv
318 - output.csv
443 - Reconresult.csv
152 - result.csv
243 - source.csv
Note: the data of the files shown use the DD/MM/YYYY
date format, but the code is correct for MM/DD/YYYY
date format. If your format is different, just adjust the %%c%%a%%b
part in the code.
注意:显示的文件数据使用DD / MM / YYYY日期格式,但代码对于MM / DD / YYYY日期格式是正确的。如果您的格式不同,只需在代码中调整%% c %% a %% b部分。
#3
1
Dates can be subtracted in PowerShell. The result is a [System.TimeSpan].
可以在PowerShell中减去日期。结果是[System.TimeSpan]。
PS C:\src\t> $(Get-Date) - $(Get-Item -Path 'sw.txt').LastWriteTime
Days : 193
Hours : 23
Minutes : 36
Seconds : 44
Milliseconds : 168
Ticks : 167602041681449
TotalDays : 193.983844538714
TotalHours : 4655.61226892914
TotalMinutes : 279336.736135748
TotalSeconds : 16760204.1681449
TotalMilliseconds : 16760204168.1449
PS C:\src\t> ($(Get-Date) - $(Get-Item -Path 'sw.txt').LastWriteTime).Days
193
If you want to do this in a .bat or .cmd script:
如果要在.bat或.cmd脚本中执行此操作:
@ECHO OFF
SET "THEFILE=C:\src\t\sw.txt"
FOR /F %%a IN ('powershell -NoProfile -Command ^
"((Get-Date) - (Get-Item -Path "%THEFILE%").LastWriteTime).Days"') DO (SET /A "NDAYS=%%a")
ECHO Days since "%THEFILE%" has been written is %NDAYS%
#1
2
In PowerShell you can just subtract one date from another and get a timespan. So something as simple as:
在PowerShell中,您可以从另一个日期中减去一个日期并获得一个时间跨度。所以简单如下:
$File = Get-Item C:\Path\To\File.txt
$DaysSinceMod = [datetime]::now - $File.LastWriteTime | Select -Expand Days
Then $DaysSinceMod
would be how many days since the file was last modified. Or, using some shorthand, that can be reduced to:
然后$ DaysSinceMod将是文件上次修改后的天数。或者,使用一些简写,可以简化为:
$LastMod=(get-date)-(gi C:\Path\To\File.txt)|% Days
#2
1
Using a Batch file, you may convert a date into a Julian Day Number in a very simple way via a conversion "function", and then use the number in any way you wish:
使用批处理文件,您可以通过转换“功能”以非常简单的方式将日期转换为儒略日编号,然后以您希望的任何方式使用该数字:
@echo off
setlocal EnableDelayedExpansion
rem Define the "Date to Julian Day Number" conversion function
set "DateToJDN(YMD)=( a=(YMD), y=a/10000, a%%=10000, m=a/100, d=a%%100, a=(m-14)/12, (1461*(y+4800+a))/4+(367*(m-2-12*a))/12-(3*((y+4900+a)/100))/4+d-32075 )"
rem Get the JDN of today's date
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A "today=!DateToJDN(YMD):YMD=%%c%%a%%b!"
rem Get the JDN of each file, subtract it from today's JDN and show the elapsed days
for %%f in (*.csv) do for /F "tokens=1-3 delims=/ " %%a in ("%%~Tf") do (
set /A "daysOld=today - !DateToJDN(YMD):YMD=%%c%%a%%b!"
echo !daysOld! - %%f
)
For example, using these files:
例如,使用这些文件:
07/04/2018 07:45 p. m. 135 data.csv
07/04/2018 07:45 p. m. 79 data_1.csv
07/04/2018 07:45 p. m. 65 data_2.csv
06/02/2018 07:41 p. m. 104 file1.csv
03/06/2017 06:02 p. m. 5,534 file2.csv
14/04/2018 10:49 p. m. 2,674 HCT_ACT_01.csv
14/04/2018 10:49 p. m. 714 HCT_ACT_02.csv
14/04/2018 10:49 p. m. 2,010 HCT_ACT_03.csv
21/03/2018 09:15 p. m. 313 msource1.csv
21/03/2018 09:16 p. m. 402 msource2.csv
21/03/2018 09:16 p. m. 402 msource3.csv
06/10/2017 09:18 p. m. 156 output.csv
03/06/2017 06:11 p. m. 2,017 Reconresult.csv
21/03/2018 09:41 p. m. 491 result.csv
20/12/2017 12:25 a. m. 305 source.csv
This is the output:
这是输出:
135 - data.csv
135 - data_1.csv
135 - data_2.csv
195 - file1.csv
443 - file2.csv
128 - HCT_ACT_01.csv
128 - HCT_ACT_02.csv
128 - HCT_ACT_03.csv
152 - msource1.csv
152 - msource2.csv
152 - msource3.csv
318 - output.csv
443 - Reconresult.csv
152 - result.csv
243 - source.csv
Note: the data of the files shown use the DD/MM/YYYY
date format, but the code is correct for MM/DD/YYYY
date format. If your format is different, just adjust the %%c%%a%%b
part in the code.
注意:显示的文件数据使用DD / MM / YYYY日期格式,但代码对于MM / DD / YYYY日期格式是正确的。如果您的格式不同,只需在代码中调整%% c %% a %% b部分。
#3
1
Dates can be subtracted in PowerShell. The result is a [System.TimeSpan].
可以在PowerShell中减去日期。结果是[System.TimeSpan]。
PS C:\src\t> $(Get-Date) - $(Get-Item -Path 'sw.txt').LastWriteTime
Days : 193
Hours : 23
Minutes : 36
Seconds : 44
Milliseconds : 168
Ticks : 167602041681449
TotalDays : 193.983844538714
TotalHours : 4655.61226892914
TotalMinutes : 279336.736135748
TotalSeconds : 16760204.1681449
TotalMilliseconds : 16760204168.1449
PS C:\src\t> ($(Get-Date) - $(Get-Item -Path 'sw.txt').LastWriteTime).Days
193
If you want to do this in a .bat or .cmd script:
如果要在.bat或.cmd脚本中执行此操作:
@ECHO OFF
SET "THEFILE=C:\src\t\sw.txt"
FOR /F %%a IN ('powershell -NoProfile -Command ^
"((Get-Date) - (Get-Item -Path "%THEFILE%").LastWriteTime).Days"') DO (SET /A "NDAYS=%%a")
ECHO Days since "%THEFILE%" has been written is %NDAYS%