【问题标题】:Use part of filename in powershell在powershell中使用部分文件名
【发布时间】:2014-03-31 13:09:11
【问题描述】:

我使用这个命令:

Get-ChildItem C:\test\AB*.zip | 
% {
    & "C:\test\7z.exe" "x" "-y" $_.fullname "-oC:\test\AB\"
}

我想保存在文件名 -2 符号的位置。 有什么建议吗?

谢谢

【问题讨论】:

  • 这个问题不清楚 - 你想删除扩展还是什么? filename -2 symbols 是什么意思?
  • 我想要我们没有扩展名的文件名,如“AB20140331.zip”解压缩到路径“...\AB201403\”,没有最后两个符号。对不起。
  • 你可以得到不带扩展名的文件名[System.IO.Path]::GetFileNameWithoutExtension(path)(docs)。但是,我仍然不明白“没有最后 2 个符号的路径”是什么意思 - 什么是符号?
  • 我想按文件名命名文件夹,但要短一些。脚本应采用文件的基本名称并缩短它。 “123456.zip”解压到目录名称“1234”
  • @WhiteHorse 如果你有两个 Zip 文件怎么办:AB12345 和 AB12354。两者最终都将成为 AB123。这是你想要的吗?

标签: powershell filenames


【解决方案1】:

试试这个方法(未测试)

Get-ChildItem C:\test\AB*.zip | 
 % {
     $to  = $_.fullname.length - 2
     $path = $_.fullname.substring( 0, $to)
     & "C:\test\7z.exe" "x" "-y" $_.fullname "-o$path"
   }

【讨论】:

    【解决方案2】:

    试试这个:

    Get-ChildItem C:\test\AB*.zip | % {
        $output = $_.fullname -replace '\w{2}$', '\'
        & "C:\test\7z.exe" "x" "-y" $_.fullname "-o$output"
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2021-09-19
      • 2018-12-31
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 2013-02-27
      相关资源
      最近更新 更多