Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell从文件名中删除最后3个字符_Powershell_File Rename - Fatal编程技术网

powershell从文件名中删除最后3个字符

powershell从文件名中删除最后3个字符,powershell,file-rename,Powershell,File Rename,我需要递归地重命名一组扩展名为“.cop”(用于复制)的文件,并去掉“.cop”字符串 根据我在get help rename item主题中阅读的内容,我发出了以下命令: gci -r -i *.cop | Rename-Item -newname {$_.name -replace '\.cop',''} 但我得到一个IO异常错误: Rename-Item : Cannot create a file when that file already exists. PowerShell不想

我需要递归地重命名一组扩展名为“.cop”(用于复制)的文件,并去掉“.cop”字符串

根据我在
get help rename item
主题中阅读的内容,我发出了以下命令:

gci -r -i *.cop | Rename-Item -newname {$_.name -replace '\.cop',''}
但我得到一个IO异常错误:

Rename-Item : Cannot create a file when that file already exists.
PowerShell不想重命名该文件,因为它已存在? 那没有道理。。。有人知道我做错了什么吗


谢谢

您需要使用Foreach对象Cmdlet:

gci -r -i *.cop | % {ren $_ -newname $_.Name.Replace(".cop", "") -whatif}

你是我的代码。请确保您没有重命名文件名的目录。

很好,非常感谢。出于好奇,最后一个问题是,有人能解释一下$\ Name.Replace的用法吗?它是什么?某种可变命令?powershell如何命名这些?我在哪里可以找到一个显示所有可能变量的列表?谢谢克里斯,但我不是这个意思。我知道一些点,但我不熟悉语法$\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,重命名项应接受管道中的对象。$\是一个变量,它引用Get ChildItem返回的FileSystemInfo对象。与大多数对象一样,它也有属性,其中一个字符串类型属性称为“Name”,它也是一个对象。它也有方法和属性,其中之一是Replace()。要发现所有可能的“命令”,您需要发现返回的对象,FileSystemInfo、String、Int32等,然后在MSDN上查找该对象的成员。