我有 mp3 文件(具有不同长度的名称)并且想要删除批处理文件旁边的每个 mp3 文件的“.mp3”之前的最后三个字符。
例如foobar043.mp3
需要成为foobar.mp3
这听起来很简单,但我尝试了很多方法(在批处理文件中使用 PowerShell)并尝试仅使用批处理命令来完成它 - 使用 SuperUser 上已有的答案。
我的最后一个命令最接近,但它也删除了文件扩展名。这被缩短为命令本身:
gci *.mp3 | rename-item -newname { $_.name.substring(0,$_.name.length-7) }
现在,如果只有某种方法可以在末尾保留“.mp3”(或者将现在的无扩展名文件重命名为 .mp3,而不重命名文件夹!)它会正常工作。
人们通常要求列出已经尝试过的内容,但这个问题最终会延长十倍!这么说吧,如果你在谷歌中输入“powershell remove last characters from file name”,我已经尝试了到目前为止我发现的所有东西。
只是一些尝试接受(和投票)答案的例子:
- https://stackoverflow.com/questions/33255834/powershell-rename-filename-by-removing-the-last-few-characters
- https://stackoverflow.com/questions/57202742/how-to-remove-last-x-number-of-character-from-file-name
- https://stackoverflow.com/questions/29636714/remove-end-of-filename-using-powershell
- https://stackoverflow.com/questions/10396769/powershell-remove-last-3-characters-from-file-name
(以及关于香料和reddit的答案,如果这里不允许,我不会链接到)。
提前感谢任何知道如何在保留文件扩展名的同时执行此操作的人。
这是一个艰难的过程,因为通常我可以将文件名(不带扩展名)设置为变量,但因为 PowerShell 将重命名文件,所以该变量实际上变得无用。
看看是不是你想要的:
您正在删除最后 7 个字符,因此显然扩展名将被删除。你应该只更换
BaseName