在所有驱动器中查找特定文件并使用其路径

时间:2019-11-23 09:26:22

标签: powershell

我正在尝试将文件从Google驱动器复制到包含文件(Old Noir.cube)的特定文件夹(LUTS)中,因为我使用的是共享PC,这将节省我的早晨时间,试图将所有东西都整理在一起。

浏览驱动器中的文件夹,然后尝试将目录捕获到变量($ path)中,这样我就可以下载zip文件并将其解压缩到其中,但是我弄得一团糟,添加时它对我不起作用$path前面的Get-ChildItem

希望有人可以对此有所启发,谢谢!

$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
    $Path = Get-ChildItem -path $drive.Root -recurse -include "Old Noir.cube" -ErrorAction SilentlyContinue | Where-Object {$_.FullName -match "LUTS"} | Select Directory
}

当前进度基于KUTlime的第一个答案。

我正在创建几个.ps文件来下载不同的文件格式,请让我知道是否还有更“复杂”的方法。

$Paths = @()
# Loop through system drives to look for x folder containing x file 
$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
    $Paths += (Get-ChildItem -path $drive.Root -recurse -include "Old Noir.cube" -ErrorAction SilentlyContinue | 
                    Where-Object {$_.FullName -match "LUTS"} | Foreach-Object {$_.Directory.FullName} )
}

# Show menu if more than 1 result returned
$Selection = $Paths | Out-GridView -OutputMode Single -Title "Select valid path from the list then click OK"
if ($Selection) {
    "You picked $Selection"
} else {
    "Cancelled"
}

# Download a zip file
# $source = "http://www.mediafire.com/file/Nov.zip"
# $target = "$Selection\tool.zip"

# Download a 7z file
$source = "http://www.mediafire.com/file/Nov.7z"
$target = "$Selection\tool.7z"

# Check if Invoke-Webrequest exists otherwise execute WebClient
if (Get-Command 'Invoke-Webrequest')
{
        Invoke-WebRequest $source -OutFile $target
}
else
{
    $WebClient = New-Object System.Net.WebClient
    $webclient.DownloadFile($source, $target)
}

# Zip
#Expand-Archive -Path $target -DestinationPath $Selection -Verbose

# 7zip
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
# Hide output with > $null
& ${env:ProgramFiles}\7-Zip\7z.exe x $target "-o$($Selection)" -y > $null

1 个答案:

答案 0 :(得分:0)

在上一次迭代的最后,您用$Path覆盖了null

将您的代码更改为以下内容:

$Paths = New-Object -TypeName "System.Collections.ArrayList"
$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
    $Paths.Add(Get-ChildItem -path $drive.Root -recurse -include "Old Noir.cube" -ErrorAction SilentlyContinue | Where-Object {$_.FullName -match "LUTS"} | Select Directory)
}

$Paths = @()
$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
    $Paths += Get-ChildItem -path $drive.Root -recurse -include "Old Noir.cube" -ErrorAction SilentlyContinue | Where-Object {$_.FullName -match "LUTS"} | Select Directory)
}