在powershell脚本中将文件从一个位置复制到另一个位置,另外还要检查某些值

ain*_*ara 1 directory powershell file

所以我在某个位置有文件夹

C:\Users\ainfowara\Desktop\testfiles
Run Code Online (Sandbox Code Playgroud)

所以我想将这些文件移动到这个位置

C:\Users\ainfowara\Desktop\destinationTestfiles
Run Code Online (Sandbox Code Playgroud)

"testfiles"有这种格式的文件,txt.*.test.*所以基本上我想在我移动文件之前检查它们在第三部分中有那两个主要的东西(txt)和(test).

有人可以帮助我,如何在powershell脚本中执行此操作

我知道我可以这样做,设置文件夹路径

path_src= C:\Users\ainfowara\Desktop\testfiles
path_dst= C:\Users\ainfowara\Desktop\destinationTestfiles
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助

Fro*_* F. 13

如果testfiles中没有子文件夹(至少你需要文件),试试这个:

$src = "C:\Users\ainfowara\Desktop\testfiles"
$dst = "C:\Users\ainfowara\Desktop\destinationTestfiles"

Get-ChildItem $src -Filter "txt.*.test.*" | Move-Item -Destination $dst -Force
Run Code Online (Sandbox Code Playgroud)

如果您在源路径的子文件夹中有文件,请尝试以下操作:

$src = "C:\Users\ainfowara\Desktop\testfiles"
$dst = "C:\Users\ainfowara\Desktop\destinationTestfiles"

Get-ChildItem $src -Filter "txt.*.test.*" -Recurse | % {
    #Creates an empty file at the destination to make sure that subfolders exists
    New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force
    Move-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您的文件名包含方括号[ ],则需要另一个脚本(已知PS错误).