2014-10-01 93 views
2

我試圖複製7個隨機.txt文件到不同的位置,但子文件夾被複制而不是.txt文件。PowerShell複製隨機文件

這裏是我的腳本:

$d = @(gci G:\Users\Public\Test) | resolve-path | get-random -count 2 
$d | gci | get-random -count 7 
Copy-Item $d -destination G:\Users\Public\Videos 

我需要做什麼改變嗎?

回答

2

一個可能的解決方案可能是使用PSIsContainer屬性過濾掉文件夾。 我嘗試以下...

$d = gci "C:\Work\a\*.txt" | Where {$_.psIsContainer -eq $false}| resolve-path | get-random -count 7 
Copy-Item $d -destination C:\Work\b 

where子句過濾掉任何東西,這不是一個「容器」,而忽視我已經建立測試文件夾。如果您需要特定的.txt文件,請使用上述路徑中包含的通配符。

此外,如果您要添加-recurse,那麼它可能會搜索您原始搜索位置的所有子文件夾,並仍然過濾掉任何「文件夾」進行復制。雖然我沒有很徹底地測試過。

$d = gci "C:\Work\a\*.txt" -recurse | Where {$_.psIsContainer -eq $false}| resolve-path | get-random -count 7