将 MKS 替换为 Powershell

发布于 2024-10-21 18:48:05 字数 291 浏览 5 评论 0原文

有人可以将这两个脚本从 MKS 翻译成 Powershell 吗?我想从我们的 ETL 工具中删除 MKS 并使用 Powershell 来完成此操作,但没有能力

1) 文件大小=ls -l $1 | awk '{print $5}'

if [ $FileSize -ge 100000000 ];然后 分割-b 60000000 $1 $1 fi

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

非常感谢 画了

can anybody please translate these two scripts from MKS into Powershell? i would like to remove MKS from our ETL tools and accomplish this with Powershell but do not have the chops

1)
FileSize=ls -l $1 | awk '{print $5}'

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

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

thanks very much
drew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-10-28 18:48:05

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

1) 文件大小=ls -l $1 | awk '{print $5}'

$filesize = (Get-ChildItem $name).Length

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

if ($filesize -ge 100000000) { ... }

(无法回忆 split 的功能)

2) find $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 以列出要删除的内容而不删除它们。)

Avoiding using even standard aliases here (eg. can use dir or ls rather than Get-ChildItem):

1) FileSize=ls -l $1 | awk '{print $5}'

$filesize = (Get-ChildItem $name).Length

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

if ($filesize -ge 100000000) { ... }

(can't recall function of split)

2) find $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

(Add a -whatif to the Remove-Item to list what would be deleted without deleting them.)

谁把谁当真 2024-10-28 18:48:05

1) 获取$1 命名的文件的大小。如果大小超过 100 MB,split 将其分成每个 60 MB 的部分。

MKS

FileSize=`ls -l $1 | awk '{print $5}'`

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

PowerShell

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< /a> 和 StreamWriter 执行更高效的缓冲 IO。


2)$1 命名的目录中,查找所有具有.txt扩展名且在三十多天前修改过的常规文件,并将其删除。

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开关以实际执行删除操作,而不是预览它。

1) Get the size of the file named by $1. If the size is more than 100 megabytes, split it into parts of 60 megabytes each.

MKS

FileSize=`ls -l $1 | awk '{print $5}'`

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

PowerShell

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
}

Notes: Only the split functionality needed by the question was implemented, and tested just on small file sizes. You may want to look into StreamReader and StreamWriter to perform more efficient buffered IO.


2) In the directory named by $1, find all regular files with a .txt extension that were modified over thirty days ago, and remove them.

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

Notes: Take off the -WhatIf switch to actually perform the remove operation, rather than previewing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文