你必須使用一些邏輯來做到這一點。下面的一個非常簡單的例子就是你要從問題中尋找什麼,而不是其他問題。你最好把它擴展到一個函數中,這樣你就可以在其他文件夾和文件中使用它,這當然可以簡化,但是我把它寫出來是很詳細的,這樣你就可以看到正在發生的事情並學習。例如,您可以輕鬆地將foreach循環合併到while循環或不使用某些變量。
這隻使用PowerShell的基礎知識,所以如果您不確定發生了什麼,可以搜索任何可以提供幫助的資源。
#define the variables for the things that don't change
$root = "C:\folder a\"
$subfolder = "\folder c\"
$filename = "file.txt"
$destination = "c:\my docs\"
#add the folders you want to cycle through to an array, and you'll be checking b3 first here
$folders = @("folder b3", "folder b2", "folder b1")
#make an empty array that will hold the full paths that we're going to create
$fullPaths = @()
#add the combined path to a list
foreach ($folder in $folders) {
$fullPaths += $root + $folder + $subfolder + $filename
}
#Loop through each item in folders, and stop when you find one that has the file.
$i = 0
while ($i -lt $fullPaths.Count) {
#find out if the item exists and put that in a variable
$exists = Test-Path $fullPaths[$i]
if ($exists){
#if the result of the test is true, copy the item and break the while loop to not go any further
Copy-Item -Path $fullPaths[$i] -Destination $destination
break
}
#make sure to increment the $i so that the while loop doesn't get stuck and run for ever
$i++
}
新的文件夾被每天新增,所以我不能只加我想通過循環的文件夾。 我需要的東西,將掃描文件夾和排序他們的最高價值,然後搜索最高價值的文件夾等 – Jamie