Windows脚本对最后修改时间超过24小时的文件进行处理

这次我接到一个任务,是写一个windows上运行的脚本,讲某个文件夹下最后修改时间超过24小时的文件移到另一个文件夹,然后在判断有生成的文件超过十分钟的关闭一个程序再启动它。上网搜了一下,有很多相关的,比如说把最后修改时间的文件超过一天的删除掉,如果用bat来写方法很多但异常麻烦,感觉版本之间还有差异。最难受的是人家要的是超过二十四小时,你如果想把前一天的文件判断出来很容易,用里面vbs或者别的方法获取,判断不是今天就OK了。有时间限制,如果昨天晚上的文件,今天处理掉了就不符合要求了。bat文件可以取日期,但是你要做个二十四小时的判断再判断有没有超过15分钟,太复杂太难了,甚至还要修改一下系统注册表,改完使用后再改回去,而且版本之间有差异,也许起作用但会在命令行报错。
后来客户提醒说用powershell来做更好,给了我个例子,我研究了一下感觉打开了一个新的世界大门。powershell功能很强大,很多函数可以用,界面看起来也比CMD的命令行帅气的多。bat批处理文件跟他比起来差很多,也好用不用绞尽脑汁想方法组合起来完成一些功能,这里面函数很多很方便,确实是powerful shell强大的shell。
话不多说,把我的代码放出来看看。

下面是我的一个脚本zp.ps1(中间没有把关闭程序和启动程序加进去,我在后面又写了个小脚本来给大家参考关于程序关闭启动,很简单。),要输入参数一个是expire(大小写都行,程序有处理)另一个是一个路径
进入powershell,不关你是直接进入还是通过命令行(当然你第一次用要把脚本权限放开,类似我们浏览器要把js限制打开,可用set-executionpolicy remotesigned,最好是管理员身份在powershell命令行下输入。)
输入以下命令运行脚本
zp.ps1 expire E:\powershell

    Param ( [string] $monitor_option , [string] $monitor_path)

New-Variable const_expire_hours -24
New-Variable const_expire_minutes -10


New-Variable files (Get-ChildItem $monitor_path\*.csv)



New-Variable files_expire_cnt 0
New-Variable files_exceeded_cnt 0
New-Variable files_cnt 0

#Current date time
$Current_date_time=Get-Date
 Write-Output (" Current date time: " + $Current_date_time )

#Folder to check
 Write-Output (" Folder to check: " + $monitor_path )

#.csv files in  %PATH%($monitor_path) last modified date 
ForEach ($file in $files) {

Write-Output ( $file.Name+ ".csv files in  " + $monitor_path + " last modified date " + $file.LastWriteTime)
}



ForEach ($file in $files) {
$now=Get-Date
    If ($file.LastWriteTime -lt $now.AddHours($const_expire_hours)) { $files_expire_cnt += 1 }

}


#24 hours
If ($monitor_option.ToLower() -eq "expire") {

Write-Output ("No of CSV files exceeeded 24 hours: " + $files_expire_cnt)

If ($files_expire_cnt -gt 0) { 

    ForEach ($file in $files) {

        If ($file.LastWriteTime -lt $now.AddHours($const_expire_hours)) {

            Move-Item -path $file -destination $monitor_path\expired

            Write-Output ( $file.Name + " moved to " + $monitor_path + "\expired" )

        }

    }

   # Exit $files_expire_cnt 

}
}
#10 minuters
ForEach ($file in $files) {
$now=Get-Date
    If ($file.LastWriteTime -lt $now.AddMinutes($const_expire_minutes)) { $files_exceeded_cnt += 1 }

}
If ($monitor_option.ToLower() -eq "expire") {

Write-Output ("No of CSV files exceeeded 10 minutes: " + $files_exceeded_cnt)

If ($files_exceeded_cnt -gt 0) { 

        ForEach ($file in $files) {

                        If ($file.LastWriteTime -lt $now.AddMinutes($const_expire_minutes)) {

                Write-Output ( $file.Name + " exceeded " + $monitor_path  )


            }

        }

        Exit $files_exceeded_cnt

    }

    Exit 0

}

下面是我写的一个启动程序小脚本,不需要参数,大家可以玩一下,根据自己想法启动关闭程序#是注释大家可以打开注释,每个命令都玩一下。powershell让我对微软印象提升了不少,没想到我们天天用的Windows系统还有这个功能,真是涨了见识。
process.ps1
Start-Process notepad -Verb runas
#Start-Process notepad -Verb runas
#dota2launcher
#Start-Process E:\DOTA2Setup20161010\Dota2\dota2launcher -Verb runas
#Stop-Process E:\DOTA2Setup20161010\Dota2\dota2launcher
#get-help Stop-Process -examples
#stop-process -name dota2launcher
#set-executionpolicy remotesigned

猜你喜欢

转载自blog.csdn.net/feichangwurao/article/details/72453637