如何使用PowerShell移动不包括某些文件夹的项目?

发布于 2025-01-21 05:17:11 字数 1517 浏览 0 评论 0原文

问题是对真正问题的简化。

我有一个文件夹,我们将其称为“ Parent -Folder”。在此文件夹中是文件和子文件夹。我希望除一个特定的子文件夹外,所有的文件和子文件夹从“父福尔德”中移动,我们称其为“ pensecthildfolder”。对于“特定ChildFolder”,我不希望将文件夹移动,而只希望其中的文件。

我可以分别执行这两个任务。我可以移动所有文件和文件夹(包括“ Parent -Folder”中的“ PenderChildFolder),或者我只能从“ PensefChildFolder”中移动文件(不包括“ Parent -Folder”中的其余文件和子文件夹)。

我想要这些。我想要这些 以外的两个任务是

  1. 在两个单独的功能中完成的:移动所有内容,“ pensuilchildfolder”
  2. 从“特定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:

  1. Move everything except "SpecificChildFolder"
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-01-28 05:17:11

这是您似乎想完成的工作的模糊表示,希望内联评论是解释性的。

$source = '\path\to\source\folder'
$destination = '\path\to\destination\folder'
# you can add multiple folders here if you want
$exceptWith = 'foldertoexclude1', 'foldertoexclude2'

# get only the subfolders of `$source` and iterate over them
Get-ChildItem $source -Directory | ForEach-Object {
    # if this folder name is in the folders to exclude array
    if($_.Name -in $exceptWith) {
        # get only the files of this folder and move them to destination
        Get-ChildItem $exclude -File | Move-Item -Destination $destination
        # if we're here we can proceed with next subfolder
        return
    }
    # if we're here means that the name of the folder was not in `$exceptWith`
    # so we move this folder and all it's child folders / files
    Move-Item -LiteralPath $_.FullName -Destination $destination -Recurse
}

Here is a vague representation of what it seems you're looking to accomplish, hope the inline comments are explanatory.

$source = '\path\to\source\folder'
$destination = '\path\to\destination\folder'
# you can add multiple folders here if you want
$exceptWith = 'foldertoexclude1', 'foldertoexclude2'

# get only the subfolders of `$source` and iterate over them
Get-ChildItem $source -Directory | ForEach-Object {
    # if this folder name is in the folders to exclude array
    if($_.Name -in $exceptWith) {
        # get only the files of this folder and move them to destination
        Get-ChildItem $exclude -File | Move-Item -Destination $destination
        # if we're here we can proceed with next subfolder
        return
    }
    # if we're here means that the name of the folder was not in `$exceptWith`
    # so we move this folder and all it's child folders / files
    Move-Item -LiteralPath $_.FullName -Destination $destination -Recurse
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文