需要脚本根据文件夹中的某些文件类型移动文件夹
Need script to move folders based on certain file types in the folder
我有一个场景,我需要在文件夹和子文件夹中搜索某些文件类型。如果找到这些类型,则需要移动包含所有文件和子文件夹的整个根文件夹。
源c:\testsource
示例:搜索 *.exe 文件
找到以下文件
c:\testsource\folder1\subfolder1\testfile.exe
然后需要将包含所有文件和子文件夹的 folder1 移动到 c:\testdestination
我首先尝试使用批处理文件进行此操作,并且能够移动子文件夹 1 中的所有文件,但不能移动任何其他文件或目录结构。然后我开始在 powershell 中研究它,但得到了类似的结果。我想我需要的是搜索,如果找到捕获文件夹路径,然后移动文件夹,但不知道该怎么做。
我创建的批处理文件:
for /r "C:\TestSource" %i in (*.exe)do move "%~dpi\*" "C:\TestDestination\"
Powershell 脚本
$Source = "C:\testsource"
$Dest = "C:\testdestination"
Get-ChildItem -Recurse -Path $Source | Where {$_.fullname -Match '.*\.exe'} | Move-Item -Destination $Dest
如有任何帮助,我们将不胜感激
您将需要一个循环来检查源文件夹的每个直接子文件夹是否在其子文件夹之一中有所需的文件。 ...像这样:
$Source = 'C:\testsource'
$Dest = 'C:\testdestination'
Get-ChildItem -Path $Source -Directory |
ForEach-Object{
If (Get-ChildItem -Path $_.FullName -Recurse -File -Filter '*.exe'){
Move-Item -Path $_.FullName -Destination $Dest -Recurse
}
}
我有一个场景,我需要在文件夹和子文件夹中搜索某些文件类型。如果找到这些类型,则需要移动包含所有文件和子文件夹的整个根文件夹。
源c:\testsource 示例:搜索 *.exe 文件 找到以下文件 c:\testsource\folder1\subfolder1\testfile.exe 然后需要将包含所有文件和子文件夹的 folder1 移动到 c:\testdestination
我首先尝试使用批处理文件进行此操作,并且能够移动子文件夹 1 中的所有文件,但不能移动任何其他文件或目录结构。然后我开始在 powershell 中研究它,但得到了类似的结果。我想我需要的是搜索,如果找到捕获文件夹路径,然后移动文件夹,但不知道该怎么做。
我创建的批处理文件:
for /r "C:\TestSource" %i in (*.exe)do move "%~dpi\*" "C:\TestDestination\"
Powershell 脚本
$Source = "C:\testsource"
$Dest = "C:\testdestination"
Get-ChildItem -Recurse -Path $Source | Where {$_.fullname -Match '.*\.exe'} | Move-Item -Destination $Dest
如有任何帮助,我们将不胜感激
您将需要一个循环来检查源文件夹的每个直接子文件夹是否在其子文件夹之一中有所需的文件。 ...像这样:
$Source = 'C:\testsource'
$Dest = 'C:\testdestination'
Get-ChildItem -Path $Source -Directory |
ForEach-Object{
If (Get-ChildItem -Path $_.FullName -Recurse -File -Filter '*.exe'){
Move-Item -Path $_.FullName -Destination $Dest -Recurse
}
}