【发布时间】:2018-10-05 14:58:23
【问题描述】:
我正在尝试编写一个脚本来使用 PowerShell 从某个文件夹中的文件名中删除字符。它提示用户输入他们想要从前面、后面删除多少个字符,以及他们想要删除的某个字符串。
# Prompt User how many characters in the front they want removed
$FrontRemove = Read-Host 'Enter how many characters you want removed from the front'
# Prompt user how many characters in the back they want removed
$BackRemove = Read-Host 'Enter how many characters you want removed from the back'
# Prompt user for string to be removed
$MiddleRemove = Read-Host 'Enter a string you want removed from the file name'
dir | Rename-Item -NewName{$_.name.substring(0,$_.BaseName.length-$BackRemove)}
dir | Rename-Item -NewName{$_.name.substring($FrontRemove)}
dir | Rename-Item -NewName{$_.name -replace "$MiddleRemove", ""}
我遇到的当前问题是它正在删除这些文件的扩展名,并且它还在重命名脚本本身。我将如何保留文件扩展名并排除 .ps1?
【问题讨论】:
-
dir是 Get-ChildItem 的别名。查看帮助(链接),了解如何在枚举中包含/排除项目的详细信息。
标签: powershell