将某些特定文件从一个文件夹结构移动到另一个具有相同目录结构的文件
Moving certain specific files from a folder structure to another with same directory structure
我在许多子文件夹中有数千个图像文件(jpg、PNG、gif 等)。例子
images18\jan\baba\hello1_abc.jpg
images18\jan\kaka\hello6_pqr.gif
images16\mar\baby\hello7_abc.png
images18\jan\yoga\hello3_xyz.jpg
images18\jan\yoga\hello345_abc.bmp
等等等等
(ABC、pqr、XYZ 是一些 3 位数字,表示一些文件属性)
现在,在不影响目录结构的情况下,我想移动名称中包含 abc
的所有文件。使其按年、月、主题和 'abc' 'xyz' 'pqr' 排序。喜欢,
images\abc18\jan\baba\hello1_abc.jpg
images\abc16\mar\baby\hello7_abc.png
images\abc18\jan\yoga\hello345_abc.bmp
images18\jan\kaka\hello6_pqr.gif
images18\jan\yoga\hello3_xyz.jpg
现在我使用我的手动方法,即
- 我在"C:\images"中搜索'abc'。
- 删除名称中包含 'abc' 的结果文件。 (这会将它们移至回收站)
- 将父文件夹 "images" 重命名为 "images1"。
- 然后恢复删除的文件,创建时自动创建以前的目录结构。
- 然后使用"remove empty directory"软件删除任何空文件夹。
虽然此方法按我的预期工作,但它非常慢,因为我的图像集超过数千张图像。
搜索、删除和恢复它们非常耗时。
我知道这可以通过使用某些脚本(does、powershell 或自动热键)(这些是我最喜欢的脚本语言)来实现,但我不确定如何实现。
请帮忙。
您可以尝试这样的操作:
#Search query
$Query = [RegEx]'_abc$'
#Root of search and place for new structure.
$FileRoot = "C:\images\"
#Get all files in the desired structure that match your search query
$Files = Get-ChildItem -Path $FileRoot -Recurse | Where-Object { ! $_.PSIsContainer -and $_.Basename -match $Query }
foreach ($File in $Files) {
#Generates a new path
$NewPath = $FileRoot + $Query + "\"+ $File.DirectoryName.Replace("$FileRoot","")
#Create the folder if it is not already created and move the item.
New-Item -Path $NewPath -ItemType directory
Move-Item -Path $File.Fullname -Destination $NewPath
}
我已经做了一些简单的测试,但可能有些事情我没有考虑到你的文件结构。
您可以将一些文件夹复制到 c:\images1\ 之类的测试中,然后尝试 运行 该文件夹中的脚本。只需更改 $FileRoot 变量
要选择要查找的文件,请更改 $Query 变量。
结果应该是所有包含 "ABC" 的文件都将移动到:
C:\images\abc\RestOfThePathTheItemHadBefore\file_abc.ext
我在许多子文件夹中有数千个图像文件(jpg、PNG、gif 等)。例子
images18\jan\baba\hello1_abc.jpg
images18\jan\kaka\hello6_pqr.gif
images16\mar\baby\hello7_abc.png
images18\jan\yoga\hello3_xyz.jpg
images18\jan\yoga\hello345_abc.bmp
等等等等
(ABC、pqr、XYZ 是一些 3 位数字,表示一些文件属性)
现在,在不影响目录结构的情况下,我想移动名称中包含 abc
的所有文件。使其按年、月、主题和 'abc' 'xyz' 'pqr' 排序。喜欢,
images\abc18\jan\baba\hello1_abc.jpg
images\abc16\mar\baby\hello7_abc.png
images\abc18\jan\yoga\hello345_abc.bmp
images18\jan\kaka\hello6_pqr.gif
images18\jan\yoga\hello3_xyz.jpg
现在我使用我的手动方法,即
- 我在"C:\images"中搜索'abc'。
- 删除名称中包含 'abc' 的结果文件。 (这会将它们移至回收站)
- 将父文件夹 "images" 重命名为 "images1"。
- 然后恢复删除的文件,创建时自动创建以前的目录结构。
- 然后使用"remove empty directory"软件删除任何空文件夹。
虽然此方法按我的预期工作,但它非常慢,因为我的图像集超过数千张图像。 搜索、删除和恢复它们非常耗时。
我知道这可以通过使用某些脚本(does、powershell 或自动热键)(这些是我最喜欢的脚本语言)来实现,但我不确定如何实现。
请帮忙。
您可以尝试这样的操作:
#Search query
$Query = [RegEx]'_abc$'
#Root of search and place for new structure.
$FileRoot = "C:\images\"
#Get all files in the desired structure that match your search query
$Files = Get-ChildItem -Path $FileRoot -Recurse | Where-Object { ! $_.PSIsContainer -and $_.Basename -match $Query }
foreach ($File in $Files) {
#Generates a new path
$NewPath = $FileRoot + $Query + "\"+ $File.DirectoryName.Replace("$FileRoot","")
#Create the folder if it is not already created and move the item.
New-Item -Path $NewPath -ItemType directory
Move-Item -Path $File.Fullname -Destination $NewPath
}
我已经做了一些简单的测试,但可能有些事情我没有考虑到你的文件结构。
您可以将一些文件夹复制到 c:\images1\ 之类的测试中,然后尝试 运行 该文件夹中的脚本。只需更改 $FileRoot 变量
要选择要查找的文件,请更改 $Query 变量。
结果应该是所有包含 "ABC" 的文件都将移动到:
C:\images\abc\RestOfThePathTheItemHadBefore\file_abc.ext