用户输入带连字符的序列,例如12-18。我需要从目录复制一个文件,以分离所有名为Folder12,Folder13,Folder14 .... Folder18的现有文件夹。实现这一目标的最佳方法是什么?
答案 0 :(得分:1)
我不会向你提供整个剧本,但我会回答我能看到的对这个问题的棘手问题:将“12-18”变成Powershell可以迭代的东西。
#User Input
$param = "12-18"
#Split the string by "-", and assign to variables.
$start = $param.Split("-")[0]
$end = $param.Split("-")[1]
# using the ".." range operator you can now iterate through each number
$start..$end | foreach {
Write-host $_
#Here is where you can put your file copy code.
}