Powershell 将文件夹内容移动到另一个文件夹
Powershell move folders content to another folders
两周前我开始学习powershell。我有以下文件夹结构
C:\not prepared\Not ready
ABCD_EFGH-whatever\work
IJK-whatever\work
LMN-whatever\work
OPRSTU_WXYZ-whatever\work
C:\Already ready
ABCD_EFGH
IJK
LMN
OPRWXY
脚本将从 C:\Already ready
运行。我想将每个 work
文件夹从 "Not ready\*\"
移动到 "Already ready\*"
以获得
C:\not prepared\Not ready
ABCD_EFGH-whatever
IJK-whatever
LMN-whatever
OPRSTU_WXYZ-whatever
C:\Already ready
ABCD_EFGH\work
IJK\work
LMN\work
OPRWXY\work
我不想指定确切的文件夹名称,因为这些名称可能会更改。只有 Not ready
和 Already ready
子文件夹的前 3 个字符匹配。我想我应该从两个位置读取所有子文件夹的名称,然后将它们放在一个数组中并逐个比较名称。此外,并非所有文件夹都可能始终存在于 C:\not prepared\Not ready
.
中
有没有更聪明的方法呢?请给我一个提示或例子。
Get-Item 'C:\not prepared\Not ready\*\work' | ForEach-Object {
# Derive the destination dir's name from the parent directory name,
# by taking the string before "-". To also split by "_", use:
# $destDir = ($_.Parent.Name -split '[-_]')[0]
# To take just the first 3 characters, use:
# $destDir = $_.Parent.Name.Substring(0, 3)
$destDir = ($_.Parent.Name -split '-')[0]
# Handle the lone exception to the name mapping.
if ($_.Parent.Name -like 'OPRSTU_WXYZ-*') { $destDir = 'OPRWXY' }
# Make sure that the destination dir. exists.
$null = New-Item -ErrorAction Stop -Force -Type Directory $destDir
# Perform the move.
$_ | Move-Item -Destination $destDir -WhatIf
# If desired, remove the parent dir. of the source dir.
Remove-Item -LiteralPath $_.Parent.FullName -Recurse -Force -WhatIf
}
注意:上面命令中的-WhatIf
common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf
。
注意:上面的解决方案假设了两件事:
每个源 work
文件夹 唯一地 映射到目标文件夹。
如果目标文件夹恰好已经存在,则假定没有 work
子文件夹。
两周前我开始学习powershell。我有以下文件夹结构
C:\not prepared\Not ready
ABCD_EFGH-whatever\work
IJK-whatever\work
LMN-whatever\work
OPRSTU_WXYZ-whatever\work
C:\Already ready
ABCD_EFGH
IJK
LMN
OPRWXY
脚本将从 C:\Already ready
运行。我想将每个 work
文件夹从 "Not ready\*\"
移动到 "Already ready\*"
以获得
C:\not prepared\Not ready
ABCD_EFGH-whatever
IJK-whatever
LMN-whatever
OPRSTU_WXYZ-whatever
C:\Already ready
ABCD_EFGH\work
IJK\work
LMN\work
OPRWXY\work
我不想指定确切的文件夹名称,因为这些名称可能会更改。只有 Not ready
和 Already ready
子文件夹的前 3 个字符匹配。我想我应该从两个位置读取所有子文件夹的名称,然后将它们放在一个数组中并逐个比较名称。此外,并非所有文件夹都可能始终存在于 C:\not prepared\Not ready
.
有没有更聪明的方法呢?请给我一个提示或例子。
Get-Item 'C:\not prepared\Not ready\*\work' | ForEach-Object {
# Derive the destination dir's name from the parent directory name,
# by taking the string before "-". To also split by "_", use:
# $destDir = ($_.Parent.Name -split '[-_]')[0]
# To take just the first 3 characters, use:
# $destDir = $_.Parent.Name.Substring(0, 3)
$destDir = ($_.Parent.Name -split '-')[0]
# Handle the lone exception to the name mapping.
if ($_.Parent.Name -like 'OPRSTU_WXYZ-*') { $destDir = 'OPRWXY' }
# Make sure that the destination dir. exists.
$null = New-Item -ErrorAction Stop -Force -Type Directory $destDir
# Perform the move.
$_ | Move-Item -Destination $destDir -WhatIf
# If desired, remove the parent dir. of the source dir.
Remove-Item -LiteralPath $_.Parent.FullName -Recurse -Force -WhatIf
}
注意:上面命令中的-WhatIf
common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf
。
注意:上面的解决方案假设了两件事:
每个源
work
文件夹 唯一地 映射到目标文件夹。如果目标文件夹恰好已经存在,则假定没有
work
子文件夹。