简体   繁体   English

使用 rename-item 命令覆盖现有文件?

[英]Using the rename-item command to overwrite an existing file?

I'm trying to tidy up imported files from my iPhone using a PowerShell script, specifically wanting edited photos (IMG_E001.jpg) to overwrite the non-edited photos (IMG_001.jpg).我正在尝试使用 PowerShell 脚本整理从我的 iPhone 导入的文件,特别是希望编辑的照片 (IMG_E001.jpg) 覆盖未编辑的照片 (IMG_001.jpg)。

I'm using the rename-item but can't figure out how to get it to overwrite the file.我正在使用rename-item ,但不知道如何让它覆盖文件。 My current workaround is to move all of the edited files into their own folder, run the script, and then copy them over the top of the original files but this is a bit of a pain, plus it doesn't seem as reliable as a fully automated script.我目前的解决方法是将所有已编辑的文件移动到它们自己的文件夹中,运行脚本,然后将它们复制到原始文件的顶部,但这有点麻烦,而且它看起来不像完全自动化的脚本。

Here's the code I'm using currently:这是我目前使用的代码:

get-childitem -recurse -Include *.JPG | foreach { rename-item $_ $_.Name.Replace("E", "") };

Please can anyone help me to get this script to overwrite the existing files?请任何人都可以帮助我让这个脚本覆盖现有文件吗? Thank you谢谢

This is a limitation of rename-item as @postanote mentioned in his comment this is because windows does not allow you to rename a file to an existing name.这是@postanote 在他的评论中提到的重命名项目的限制,这是因为 windows 不允许您将文件重命名为现有名称。 You will need to remove the other file first or overwrite it.您需要先删除另一个文件或覆盖它。 Instead you can use move-item -force for essentially the same effect as it will perform the overwrite.相反,您可以使用move-item -force来获得与执行覆盖基本相同的效果。

get-childitem -recurse -Include *.txt | foreach { Move-Item $_ $_.Name.Replace("E", "") -Force };

For more information;了解更多信息; here is a related thread rename-item and override if filename exists这是一个相关的线程重命名项,如果文件名存在则覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM