如何使用PowerShell移动不包括某些文件夹的项目?
问题是对真正问题的简化。
我有一个文件夹,我们将其称为“ Parent -Folder”。在此文件夹中是文件和子文件夹。我希望除一个特定的子文件夹外,所有的文件和子文件夹从“父福尔德”中移动,我们称其为“ pensecthildfolder”。对于“特定ChildFolder”,我不希望将文件夹移动,而只希望其中的文件。
我可以分别执行这两个任务。我可以移动所有文件和文件夹(包括“ Parent -Folder”中的“ PenderChildFolder),或者我只能从“ PensefChildFolder”中移动文件(不包括“ Parent -Folder”中的其余文件和子文件夹)。
我想要这些。我想要这些 以外的两个任务是
我
- 在两个单独的功能中完成的:移动所有内容,“ pensuilchildfolder”
- 从“特定Childfolder”中移动文件
。 。 我还尝试了Get -ChildItem $ SRC -ERRORACTION sillyContinue | where -object {$_。directory.name-notlike“*pensecyChildFolder*”} | foreach-object {}
,但这是不起作用的
,其次不能在一条PowerShell中发生吗?
我正在使用PowerShell Core 7.2
阶段1:
#Sources
$src = "C:\User\Desktop\TEST\ParentFolder\*"
$srcMcaNameChg = "C:\User\Desktop\TEST\ParentFolder"
#Destination
$dest = "C:\Users\harguls\Desktop\TEST\DestinationFolder"
Function MoveFiles{
Param(
[string]$src,
[string]$dest,
[string]$srcNameChange
)
Get-ChildItem $src -Recurse -Exclude 'SpecificChildFolder' -ErrorAction SilentlyContinue | ForEach-Object{
$fileName = $_.Name
# Check for duplicate files
$file = Test-Path -Path $dest\$fileName
Write-Output $file
if($file)
{
"$srcNameChange\$fileName" | Rename-Item -NewName ("Copy_"+$fileName)
}
}
Move-Item -Path $src -Destination $dest -Force
}
MoveFiles -src $src -dest $dest -srcNameChange $srcMcaNameChg
The question is an oversimplification of the real issue.
I have a folder let's call it "ParentFolder". Within this folder are files and subfolders. I want all the files and subfolders moved from the "ParentFolder" except one specific subfolder, let's call it "SpecificChildFolder". For the "SpecificChildFolder" I don't want the folder to be moved but only the files in it.
I can do these two tasks separately. I can either move all the files and folders(including the "SpecificChildFolder) in the "ParentFolder" or I can move files from the "SpecificChildFolder" only (excluding the rest of the files and subfolders in the "ParentFolder").
I want these two tasks to happen simultaneously.
I thought I would accomplish this in two separate functions:
- Move everything except "SpecificChildFolder"
- Move files from within the "SpecificChildFolder"
The stage# 2 code works. It is Stage# 1 I have issues with.
I have also tried Get-ChildItem $src -ErrorAction SilentlyContinue | Where-Object {$_.Directory.Name -NotLike "*SpecificChildFolder*"} | ForEach-Object{}
but this doesn't work either
Secondly, can this is not happen in one line of PowerShell?
I am using PowerShell Core 7.2
Code for Stage 1:
#Sources
$src = "C:\User\Desktop\TEST\ParentFolder\*"
$srcMcaNameChg = "C:\User\Desktop\TEST\ParentFolder"
#Destination
$dest = "C:\Users\harguls\Desktop\TEST\DestinationFolder"
Function MoveFiles{
Param(
[string]$src,
[string]$dest,
[string]$srcNameChange
)
Get-ChildItem $src -Recurse -Exclude 'SpecificChildFolder' -ErrorAction SilentlyContinue | ForEach-Object{
$fileName = $_.Name
# Check for duplicate files
$file = Test-Path -Path $dest\$fileName
Write-Output $file
if($file)
{
"$srcNameChange\$fileName" | Rename-Item -NewName ("Copy_"+$fileName)
}
}
Move-Item -Path $src -Destination $dest -Force
}
MoveFiles -src $src -dest $dest -srcNameChange $srcMcaNameChg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您似乎想完成的工作的模糊表示,希望内联评论是解释性的。
Here is a vague representation of what it seems you're looking to accomplish, hope the inline comments are explanatory.