我正在尝试将文件从一个目录复制到另一个目录,如果存在同名的文件,请保存该文件。基本上是windows中的“复制但保留两个文件”选项。如何从windows命令行完成此操作?
我需要一种从一个命令提示符会话执行此操作的方法
目录路径1:"C:\Users\User 1“目录路径2:"C:\Users\User 2”
发布于 2017-09-09 00:20:21
您可以从google快速找到脚本,您可以将其放入bat文件中并处理此问题,但我建议您研究powershell来处理此问题。
$SourceFile = "C:\Temp\File.txt"
$DestinationFile = "C:\Temp\NonexistentDirectory\File.txt"
If (Test-Path $DestinationFile) {
$i = 0
While (Test-Path $DestinationFile) {
$i += 1
$DestinationFile = "C:\Temp\NonexistentDirectory\File$i.txt"
}
} Else {
New-Item -ItemType File -Path $DestinationFile -Force
}
Copy-Item -Path $SourceFile -Destination $DestinationFile -Force如果愿意,您可以从命令行调用powershell,因此从技术上讲,这个答案符合要求。如果您想对复制操作进行任何其他更改,Powershell将使这类任务变得更容易,并为您提供更大的灵活性。
How to Copy Individual Files and Rename Duplicates with Powershell
https://stackoverflow.com/questions/46120589
复制相似问题