如何正确设置文件夹深度,以便使用powershell移动文件



我有这个目录结构在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:tempa
C:tempz
C:tempc

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

C:tempaaa
C:tempzhh
C:tempckku

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

$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"
}
}

我想我弄乱了正则表达式,但我想知道如果这是问题

首先,您使这项工作过于复杂。

其次,在代码中没有任何地方设置深度,这是使用Get-ChildItemcmdlet的开关。

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:ParentSubDir_Level1

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2/14/2019     10:24                SubDir_Level2
-a----        2/13/2019     08:55             26 file.txt

Directory: C:ParentSubDir_Level1SubDir_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',
'aaa',
'zhh',
'ckku'
) | 
ForEach-Object {
New-Item -Path 'D:tempFolderTest' -ItemType Directory -Name $PSItem
Start-Sleep -Seconds 1
If ($PSItem -Notmatch '\')
{
New-Item -Path "D:tempFolderTest$PSItem" -ItemType File -Name "$PSitem.txt" 
Start-Sleep -Seconds 1
}
Else
{
New-Item -Path "D:tempFolderTest$PSItem" -ItemType File -Name "$(($PSitem -split '\')[-1]).txt" 
Start-Sleep -Seconds 1
}
}
Get-ChildItem -Path 'D:tempFolderTest'
# Results
<#
Directory: D:tempFolderTest

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:tempFolderTest' -Recurse
# Results
<#
Directory: D:tempFolderTest

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:tempFolderTesta

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

Directory: D:tempFolderTestaaa

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

Directory: D:tempFolderTestc

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

Directory: D:tempFolderTestckku

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

Directory: D:tempFolderTestz

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

Directory: D:tempFolderTestzhh

Mode          LastWriteTime Length Name  
----          ------------- ------ ----  
-a----  24-Dec-22     20:11      0 hh.txt
#>
Get-ChildItem -Path 'D:tempFolderTest' -Filter '*.txt'
# Results
<#
No file(s) yet
#>
# Display existing files
Get-ChildItem -Path 'D:tempFolderTest' -Filter '*.txt' -Depth 1
# Results
<#
Directory: D:tempFolderTesta

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

Directory: D:tempFolderTestc

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

Directory: D:tempFolderTestz

Mode          LastWriteTime Length Name 
----          ------------- ------ ---- 
-a----  24-Dec-22     20:11      0 z.txt
#>
# Test move event
Get-ChildItem -Path 'D:tempFolderTest' -Filter '*.txt' -Depth 1 | 
Move-Item -Destination 'D:tempFolderTest' -WhatIf
# Results
<#
What if: Performing the operation "Move File" on target "Item: D:tempFolderTestaa.txt Destination: D:tempFolderTesta.txt".
What if: Performing the operation "Move File" on target "Item: D:tempFolderTestcc.txt Destination: D:tempFolderTestc.txt".
What if: Performing the operation "Move File" on target "Item: D:tempFolderTestzz.txt Destination: D:tempFolderTestz.txt".
#>
# Execute move event
Get-ChildItem -Path 'D:tempFolderTest' -Filter '*.txt' -Depth 1 | 
Move-Item -Destination 'D:tempFolderTest' -Verbose
# Results
<#
VERBOSE: Performing the operation "Move File" on target "Item: D:tempFolderTestaa.txt Destination: D:tempFolderTesta.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:tempFolderTestcc.txt Destination: D:tempFolderTestc.txt".
VERBOSE: Performing the operation "Move File" on target "Item: D:tempFolderTestzz.txt Destination: D:tempFolderTestz.txt".
#>
# Validate use case results
Get-ChildItem -Path 'D:tempFolderTest'
# Results
<#
Directory: D:tempFolderTest

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:tempFolderTest' -Recurse).FullName
# Results
<#
D:tempFolderTesta
D:tempFolderTestc
D:tempFolderTestz
D:tempFolderTesta.txt
D:tempFolderTestc.txt
D:tempFolderTestz.txt
D:tempFolderTestaaa
D:tempFolderTestaaaaa.txt
D:tempFolderTestckku
D:tempFolderTestckkukku.txt
D:tempFolderTestzhh
D:tempFolderTestzhhhh.txt
#>

相关内容

  • 没有找到相关文章

最新更新