将具有特定扩展名的文件移动到更高层次的文件夹
move files with specific extension to folder in higher hierarchy
我的所有文件都在特定文件夹中:
17\PRO
17\PRO
17\PRO
xx\xx\xx\PRO
- 17 是年份(所以明年是 18,等等)
- 第一个1是指定案例编号的文件夹(最多100个)。
- 第二个1是案例编号上的子部件。
最后 1 个文件中有一个文件夹 PRO
,所有数据都在其中。
我们需要移动这些文件,但这些文件需要保留在各自的 "PRO" 文件夹中。
例如:
17\pro\xxx\www\
中的一个文件需要转到 17\pro\movies
17\pro\xxdfsdf\eeee\
中的一个文件需要转到 17\pro\movies
。
如果有文件要移动,应该会创建 movies 文件夹。
我需要获取文件全名的一部分并将文件移至 "movie" 文件夹。问题是我不知道如何拆分全名,向其中添加 \movies 并将文件移动到那里。
到目前为止,这是我的代码:
Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | select $_Fullname |
Move-Item -Force -Destination ($_Fullname.Split("pro"))
如果目标始终是 "movies subdirectory of the grandparent directory of the file's directory",您可以构建相对于文件位置的目标路径:
Get-ChildItem ... | ForEach-Object {
$dst = Join-Path $_.Directory '..\..\movies'
if (-not (Test-Path -LiteralPath $dst -PathType Container)) {
New-Item -Type Directory -Path $dst | Out-Null
}
Move-Item $_.FullName -Destination $dst
}
如果 PRO
目录是您的锚点,您可以使用这样的正则表达式替换:
Get-ChildItem ... | ForEach-Object {
$dst = $_.Directory -replace '^(.*\\d+\\d+\\d+\PRO)\.*', '\movies'
if (-not (Test-Path -LiteralPath $dst -PathType Container)) {
New-Item -Type Directory -Path $dst | Out-Null
}
Move-Item $_.FullName -Destination $dst
}
如果你不知道有多少个目录,我会这样做:
Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | ForEach-Object {
if ($_.FullName.IndexOf('\PRO\') -gt 0) {
$Destination = Join-Path -Path $_.FullName.Substring(0,$_.FullName.IndexOf('\PRO\') + 5) -ChildPath 'movies';
New-Item $Destination -ItemType Directory -ea Ignore;
$_ | Move-Item -Destination $Destination;
} else {
throw ("\PRO\ path not found in '$($_.FullName)'");
}
}
只要您的路径只有 \pro\
一次,这就可以正常工作。如果他们有不止一次像 customer\pro\pro\pro\xx\yy\zz\www
而你需要最后一个索引,那么使用 $_.FullName.LastIndexOf('\pro\')
.
如果您在 .\pro\movies\
所在的目录前后都有 \pro\
目录,那么,您就有麻烦了。您可能必须找到不同的参考点。
有一组文件夹
17\PRO
17\PRO
17\PRO
您可以尝试以下方法
$RootPaths = Get-ChildItem -Path C:\folder\*\*\*\pro
$RootPaths
将包含上述所有 3 个路径,下面的代码会将所有文件移动到适当的目录。
ForEach( $Path in $RootPaths)
{
$Movies = Join-Path $Path -Child "Movies"
If( -not (Test-Path $Movies ) ) { New-Item -Path $Movies -ItemType Directory }
Get-ChildItem -Path $Path -Recurse -File -Filter $Extension |
Move-Item -Path $_.FullName -Destination "$( $Path )\Movies"
}
这样一来,您的文件有多少层都没有关系。他们总是被移动到同一个目录。
我的所有文件都在特定文件夹中:
17\PRO
17\PRO
17\PRO
xx\xx\xx\PRO
- 17 是年份(所以明年是 18,等等)
- 第一个1是指定案例编号的文件夹(最多100个)。
- 第二个1是案例编号上的子部件。
最后 1 个文件中有一个文件夹 PRO
,所有数据都在其中。
我们需要移动这些文件,但这些文件需要保留在各自的 "PRO" 文件夹中。
例如:
17\pro\xxx\www\
中的一个文件需要转到17\pro\movies
17\pro\xxdfsdf\eeee\
中的一个文件需要转到17\pro\movies
。
如果有文件要移动,应该会创建 movies 文件夹。
我需要获取文件全名的一部分并将文件移至 "movie" 文件夹。问题是我不知道如何拆分全名,向其中添加 \movies 并将文件移动到那里。
到目前为止,这是我的代码:
Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | select $_Fullname |
Move-Item -Force -Destination ($_Fullname.Split("pro"))
如果目标始终是 "movies subdirectory of the grandparent directory of the file's directory",您可以构建相对于文件位置的目标路径:
Get-ChildItem ... | ForEach-Object {
$dst = Join-Path $_.Directory '..\..\movies'
if (-not (Test-Path -LiteralPath $dst -PathType Container)) {
New-Item -Type Directory -Path $dst | Out-Null
}
Move-Item $_.FullName -Destination $dst
}
如果 PRO
目录是您的锚点,您可以使用这样的正则表达式替换:
Get-ChildItem ... | ForEach-Object {
$dst = $_.Directory -replace '^(.*\\d+\\d+\\d+\PRO)\.*', '\movies'
if (-not (Test-Path -LiteralPath $dst -PathType Container)) {
New-Item -Type Directory -Path $dst | Out-Null
}
Move-Item $_.FullName -Destination $dst
}
如果你不知道有多少个目录,我会这样做:
Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | ForEach-Object {
if ($_.FullName.IndexOf('\PRO\') -gt 0) {
$Destination = Join-Path -Path $_.FullName.Substring(0,$_.FullName.IndexOf('\PRO\') + 5) -ChildPath 'movies';
New-Item $Destination -ItemType Directory -ea Ignore;
$_ | Move-Item -Destination $Destination;
} else {
throw ("\PRO\ path not found in '$($_.FullName)'");
}
}
只要您的路径只有 \pro\
一次,这就可以正常工作。如果他们有不止一次像 customer\pro\pro\pro\xx\yy\zz\www
而你需要最后一个索引,那么使用 $_.FullName.LastIndexOf('\pro\')
.
如果您在 .\pro\movies\
所在的目录前后都有 \pro\
目录,那么,您就有麻烦了。您可能必须找到不同的参考点。
有一组文件夹
17\PRO
17\PRO
17\PRO
您可以尝试以下方法
$RootPaths = Get-ChildItem -Path C:\folder\*\*\*\pro
$RootPaths
将包含上述所有 3 个路径,下面的代码会将所有文件移动到适当的目录。
ForEach( $Path in $RootPaths)
{
$Movies = Join-Path $Path -Child "Movies"
If( -not (Test-Path $Movies ) ) { New-Item -Path $Movies -ItemType Directory }
Get-ChildItem -Path $Path -Recurse -File -Filter $Extension |
Move-Item -Path $_.FullName -Destination "$( $Path )\Movies"
}
这样一来,您的文件有多少层都没有关系。他们总是被移动到同一个目录。