【发布时间】:2019-11-02 18:59:36
【问题描述】:
尝试制作一个简单的备份脚本,如下所示:
移动以下
\source\example1.txt
\source\path\example2.txt
到
\dest\example1.txt
\dest\path\example2.txt
同时,重命名dest中已存在的所有文件。
我的代码:
$src = "C:\Users\User\Desktop\test1"
$dest = "C:\Users\User\Desktop\test2"
Get-ChildItem -Path $src -Filter *.txt -Recurse | ForEach-Object {
$num=1
$nextName = Join-Path -Path $dest -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $dest ($_.BaseName + " ($num)" + $_.Extension)
$num+=1
}
$_ | Move-Item -Destination $nextName
}
这几乎可以工作,但它将所有文件压缩到dest 中的一个文件夹中。
(\source\path\example.txt 变为 \dest\example.txt)
如何解决?
【问题讨论】:
标签: powershell command-line terminal