如何设置文件夹深度正确移动文件与powershell

thtygnil  于 2023-01-13  发布在  Shell
关注(0)|答案(1)|浏览(200)

我在C:\temp中有这样的目录结构

a
|
+--aa (folder)
|       +
|       +-- subfile1.zip
|
+- file.txt

z
|
+--hh (folder)
|       |
|       +-- subfile2.txt
|
+- file2.txt

c
|
+--kku (folder)
|       |
|       +-- subfile3.txt
|
+- file3.txt

我只想移入这些路径中的C:\temp文件

C:\temp\a
C:\temp\z
C:\temp\c

但我不想从这些路径移动文件

C:\temp\a\aa
C:\temp\z\hh
C:\temp\c\kku

我不太明白如何设置目录的级别。我尝试用这个

$rootPath = "C:\temp"

Get-ChildItem -Path $rootPath -Recurse -File | ForEach-Object {
  $filePath = $_.FullName
  $fileName = $_.Name
  $folderPath = $_.DirectoryName
  if ($folderPath -notmatch "^$rootPath\\[a-z]\\.*\\hh$" -and $folderPath -notmatch "^$rootPath\\[a-z]\\.*$" -and $folderPath -ne $rootPath) {
    Move-Item -Path $filePath -Destination "$rootPath\$fileName"
  }
}

我想我搞砸了正则表达式,但我想知道这是不是问题所在

yc0p9oo0

yc0p9oo01#

首先,你把这项工作搞得过于复杂了。
其次,代码中没有设置深度,这是使用Get-ChildItem cmdlet的开关。

Get-Help -Name Get-ChildItem -Examples
# Results
<#
NAME
    Get-ChildItem
    
SYNOPSIS
    Gets the items and child items in one or more specified locations.

...
    -------- Example 8: Get items using the Depth parameter --------
    
    Get-ChildItem -Path C:\Parent -Depth 2
    
    Directory: C:\Parent
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        2/14/2019     10:24                SubDir_Level1
    -a----        2/13/2019     08:55             26 file.txt
    
        Directory: C:\Parent\SubDir_Level1
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        2/14/2019     10:24                SubDir_Level2
    -a----        2/13/2019     08:55             26 file.txt
    
        Directory: C:\Parent\SubDir_Level1\SubDir_Level2
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        2/14/2019     10:22                SubDir_Level3
    -a----        2/13/2019     08:55             26 file.txt
    
    The `Get-ChildItem` cmdlet uses the Path parameter to specify C:\Parent . 
    The Depth parameter specifies two levels of recursion. 
    
    `Get-ChildItem` displays the contents of the 
    directory specified by the Path parameter and the two levels of subdirectories.
...
#>

如果您只需要特定的目录,那么只需定位这些目录,而不要使用-Recurse开关,您也不必担心子目录。
采用-Depth方法,您根本不需要RegEx。要避免这种调试难题,请始终一步一步地编写和处理代码。在转到下一行之前,请确保您在每一行都得到了预期的结果。它几乎消除了调试难题和不必要的代码工作。
例如:

# Create the directory tree and file structure to test
Clear-Host
@(

'a',
'z',
'c',
'a\aa',
'z\hh',
'c\kku'
) | 
ForEach-Object {
New-Item -Path 'D:\temp\FolderTest\' -ItemType Directory -Name $PSItem
Start-Sleep -Seconds 1

If ($PSItem -Notmatch '\\')
{
    New-Item -Path "D:\temp\FolderTest\$PSItem\" -ItemType File -Name "$PSitem.txt" 
    Start-Sleep -Seconds 1
}
Else
{
    New-Item -Path "D:\temp\FolderTest\$PSItem\" -ItemType File -Name "$(($PSitem -split '\\')[-1]).txt" 
    Start-Sleep -Seconds 1
    }
}

Get-ChildItem -Path 'D:\temp\FolderTest\'
# Results
<#
    Directory: D:\temp\FolderTest

Mode          LastWriteTime Length Name
----          ------------- ------ ----
d-----  24-Dec-22     20:11        a   
d-----  24-Dec-22     20:11        c   
d-----  24-Dec-22     20:11        z   
#>

Get-ChildItem -Path 'D:\temp\FolderTest\' -Recurse
# Results
<#
    Directory: D:\temp\FolderTest

Mode          LastWriteTime Length Name
----          ------------- ------ ----
d-----  24-Dec-22     20:11        a   
d-----  24-Dec-22     20:11        c   
d-----  24-Dec-22     20:11        z   

    Directory: D:\temp\FolderTest\a

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
d-----  24-Dec-22     20:11        aa   
-a----  24-Dec-22     20:11      0 a.txt

    Directory: D:\temp\FolderTest\a\aa

Mode          LastWriteTime Length Name  
----          ------------- ------ ----  
-a----  24-Dec-22     20:11      0 aa.txt

    Directory: D:\temp\FolderTest\c

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
d-----  24-Dec-22     20:11        kku  
-a----  24-Dec-22     20:11      0 c.txt

    Directory: D:\temp\FolderTest\c\kku

Mode          LastWriteTime Length Name   
----          ------------- ------ ----   
-a----  24-Dec-22     20:11      0 kku.txt

    Directory: D:\temp\FolderTest\z

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
d-----  24-Dec-22     20:11        hh   
-a----  24-Dec-22     20:11      0 z.txt

    Directory: D:\temp\FolderTest\z\hh

Mode          LastWriteTime Length Name  
----          ------------- ------ ----  
-a----  24-Dec-22     20:11      0 hh.txt
#>

Get-ChildItem -Path 'D:\temp\FolderTest\' -Filter '*.txt'
# Results
<#
No file(s) yet
#>

# Display existing files
Get-ChildItem -Path 'D:\temp\FolderTest\' -Filter '*.txt' -Depth 1
# Results
<#
    Directory: D:\temp\FolderTest\a

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
-a----  24-Dec-22     20:11      0 a.txt

    Directory: D:\temp\FolderTest\c

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
-a----  24-Dec-22     20:11      0 c.txt

    Directory: D:\temp\FolderTest\z

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
-a----  24-Dec-22     20:11      0 z.txt
#>

# Test move event
Get-ChildItem -Path 'D:\temp\FolderTest\' -Filter '*.txt' -Depth 1 | 
Move-Item -Destination 'D:\temp\FolderTest\' -WhatIf
# Results
<#
What if: Performing the operation "Move File" on target "Item: D:\temp\FolderTest\a\a.txt Destination: D:\temp\FolderTest\a.txt".
What if: Performing the operation "Move File" on target "Item: D:\temp\FolderTest\c\c.txt Destination: D:\temp\FolderTest\c.txt".
What if: Performing the operation "Move File" on target "Item: D:\temp\FolderTest\z\z.txt Destination: D:\temp\FolderTest\z.txt".
#>

# Execute move event
Get-ChildItem -Path 'D:\temp\FolderTest\' -Filter '*.txt' -Depth 1 | 
Move-Item -Destination 'D:\temp\FolderTest\' -Verbose
# Results
<#
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\FolderTest\a\a.txt Destination: D:\temp\FolderTest\a.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\FolderTest\c\c.txt Destination: D:\temp\FolderTest\c.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:\temp\FolderTest\z\z.txt Destination: D:\temp\FolderTest\z.txt".
#>

# Validate use case results
Get-ChildItem -Path 'D:\temp\FolderTest\'
# Results
<#
    Directory: D:\temp\FolderTest

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
d-----  24-Dec-22     20:21        a    
d-----  24-Dec-22     20:21        c    
d-----  24-Dec-22     20:21        z    
-a----  24-Dec-22     20:11      0 a.txt
-a----  24-Dec-22     20:11      0 c.txt
-a----  24-Dec-22     20:11      0 z.txt
#>

(Get-ChildItem -Path 'D:\temp\FolderTest\' -Recurse).FullName
# Results
<#
D:\temp\FolderTest\a
D:\temp\FolderTest\c
D:\temp\FolderTest\z
D:\temp\FolderTest\a.txt
D:\temp\FolderTest\c.txt
D:\temp\FolderTest\z.txt
D:\temp\FolderTest\a\aa
D:\temp\FolderTest\a\aa\aa.txt
D:\temp\FolderTest\c\kku
D:\temp\FolderTest\c\kku\kku.txt
D:\temp\FolderTest\z\hh
D:\temp\FolderTest\z\hh\hh.txt
#>

相关问题