有人能把这两个脚本从MKS翻译成Powershell吗?我想从我们的ETL工具中删除MKS,并使用Powershell来实现这一点,但没有印章
1) FileSize=ls -l $1 | awk '{print $5}'
如果是$FileSize -ge 100000000,则拆分-b 60000000 $1 $1 fi
2)找到$1 -type f -name *.txt -mtime +30 -exec rm {} \;
非常感谢,德鲁
发布于 2011-03-15 02:53:13
避免在这里使用标准别名(例如,可以使用dir或ls而不是Get-ChildItem):
打印1)打印$1 | awk‘{
$5}’
$filesize = (Get-ChildItem $name).Length如果$FileSize -ge 100000000拆分,则拆分-b 60000000 $1 $1 fi
if ($filesize -ge 100000000) { ... }(无法调用split的功能)
2)找到$1 -type f -name *.txt -mtime +30 -exec rm {} \;
$t = [datetime]::Now.AddSeconds(-30)
Get-ChildItem -path . -recurse -filter *.txt |
Where-Object { $_.CreationTime -gt $t -and $_.PSIsContainer } |
Remove-Item(将-whatif添加到Remove-Item以列出要删除的内容,但不删除它们。)
发布于 2011-09-24 23:50:10
1)获取$1命名的文件大小。如果大小超过100兆字节,则将其split为每个60兆字节的部分。
MKS
FileSize=`ls -l $1 | awk '{print $5}'`
if [ $FileSize -ge 100000000 ]; then
split -b 60000000 $1 $1
fiPowerShell
function split( [string]$path, [int]$byteCount ) {
# Find how many splits will be made.
$file = Get-ChildItem $path
[int]$splitCount = [Math]::Ceiling( $file.Length / $byteCount )
$numberFormat = '0' * "$splitCount".Length
$nameFormat = $file.BaseName + "{0:$numberFormat}" + $file.Extension
$pathFormat = Join-Path $file.DirectoryName $nameFormat
# Read the file in $byteCount chunks, sending each chunk to a numbered split file.
Get-Content $file.FullName -Encoding Byte -ReadCount $byteCount |
Foreach-Object { $i = 1 } {
$splitPath = $pathFormat -f $i
Set-Content $splitPath $_ -Encoding Byte
++$i
}
}
$FileSize = (Get-ChildItem $name).Length
if( $FileSize -gt 100MB ) {
split -b 60MB $name
}注意:仅实现了问题所需的拆分功能,并仅在小文件大小上进行了测试。您可能需要研究StreamReader和StreamWriter,以执行更高效的缓冲IO。
2)在名为$1的目录中,对30天前修改过的所有扩展名为.txt的常规文件执行find操作,并将其删除。
MKS
find $1 -type f -name *.txt -mtime +30 -exec rm {} \;PowerShell
$modifiedTime = (Get-Date).AddDays( -30 )
Get-ChildItem $name -Filter *.txt -Recurse |
Where-Object { $_.LastWriteTime -lt $modifiedTime } |
Remove-Item -WhatIf注意:取下-WhatIf开关,实际执行删除操作,而不是预览。
https://stackoverflow.com/questions/5302734
复制相似问题