我被问到是否可以从具有目录和子目录的源文件复制许多文件,并且将taget作为单个目录。 所有文件到一个目录。 如果碰巧有一个重复的文件,他们只会复制不同的文件名,如…(1)。 我试图ROBOCOPY,但到目前为止还没有find一个开关,帮助我的任务。
谢谢!
这可以通过PowerShell轻松完成。
# Set the source and destination paths (No trailing slash) $source = "C:\subtree" $dest = "C:\consolidated" # Get a list of all files recursively $files = Get-ChildItem -Path $source -Recurse # Process each file $files | ForEach-Object { # Basename is filename w/o ext $baseName = $_.BaseName $ext = $_.Extension # Build initial new file path $newName = "$dest\$($_.Name)" # Check for duplicate file If (Test-Path $newName) { $i = 0 # While the file exists, increment the number that will be appended While (Test-Path $newName) { $i++ $newName = "$dest\$baseName($i)$ext" } } # If the file is not a duplicate, write the (placeholder) file. Else { New-Item -ItemType File -Path $newName -Force } # Copy the file contents to the destination Copy-Item $_.FullName -Destination $newName -Force }
由于您刚接触PowerShell,因此我会build议您使用附带的PowerShell ISE。 它将允许你粘贴这个脚本,并更容易地处理它。