首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Powershell替换MKS

用Powershell替换MKS
EN

Stack Overflow用户
提问于 2011-03-15 02:27:14
回答 2查看 519关注 0票数 2

有人能把这两个脚本从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 {} \;

非常感谢,德鲁

EN

回答 2

Stack Overflow用户

发布于 2011-03-15 02:53:13

避免在这里使用标准别名(例如,可以使用dirls而不是Get-ChildItem):

打印1)打印$1 | awk‘{

$5}’

代码语言:javascript
复制
$filesize = (Get-ChildItem $name).Length

如果$FileSize -ge 100000000拆分,则拆分-b 60000000 $1 $1 fi

代码语言:javascript
复制
if ($filesize -ge 100000000) { ... }

(无法调用split的功能)

2)找到$1 -type f -name *.txt -mtime +30 -exec rm {} \;

代码语言:javascript
复制
$t = [datetime]::Now.AddSeconds(-30)
Get-ChildItem -path . -recurse -filter *.txt |
  Where-Object { $_.CreationTime -gt $t -and $_.PSIsContainer } |
  Remove-Item

(将-whatif添加到Remove-Item以列出要删除的内容,但不删除它们。)

票数 2
EN

Stack Overflow用户

发布于 2011-09-24 23:50:10

1)获取$1命名的文件大小。如果大小超过100兆字节,则将其split为每个60兆字节的部分。

MKS

代码语言:javascript
复制
FileSize=`ls -l $1 | awk '{print $5}'`

if [ $FileSize -ge 100000000 ]; then
split -b 60000000 $1 $1
fi

PowerShell

代码语言:javascript
复制
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
}

注意:仅实现了问题所需的拆分功能,并仅在小文件大小上进行了测试。您可能需要研究StreamReaderStreamWriter,以执行更高效的缓冲IO。

2)在名为$1的目录中,对30天前修改过的所有扩展名为.txt的常规文件执行find操作,并将其删除。

MKS

代码语言:javascript
复制
find $1 -type f -name *.txt -mtime +30 -exec rm {} \;

PowerShell

代码语言:javascript
复制
$modifiedTime = (Get-Date).AddDays( -30 )
Get-ChildItem $name -Filter *.txt -Recurse |
  Where-Object { $_.LastWriteTime -lt $modifiedTime } |
  Remove-Item -WhatIf

注意:取下-WhatIf开关,实际执行删除操作,而不是预览。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5302734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档