想要一些帮助。我是一个初学者。
示例:
c:\folder a\folder b3\folder c\
c:\folder a\folder b2\folder c\file.txt
c:\folder a\folder b1\folder c\file.txt
步骤:
file.txt
"中检查...folder b3\folder c\
- >文件不存在file.txt
- > " ...\folder b2\folder c\file.txt
"确实存在c:\my docs\
答案 0 :(得分:0)
你必须使用一些逻辑来做到这一点。下面的一个非常简单的例子正是您正在寻找的问题而不是其他内容。你最好将它扩展到一个函数,以便你可以将它与其他文件夹和文件一起使用,这当然可以简化,但我把它写得很冗长,这样你就可以看到正在发生的事情并学习。例如,您可以轻松地将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++
}
答案 1 :(得分:0)
如果.txt文件的名称始终相同,则可以使用此代码
$src = Get-ChildItem "C:\folder a\*\folder c\file.txt"
$i=0
foreach ($file in $src)
{
$i++
Copy-Item -Path $file -Destination "c:\my docs\file$i.txt" -Force
}