Powershell:基于动态菜单的程序来复制文件
Powershell: Dynamic Menu-based program to copy file
这样做的目的是将一个(或多个)文件从一个用户选择的动态位置复制到另一个用户选择的动态位置。
Write-Host "Choose where to copy FROM"
MainFolder = (Get-ChildItem "C:\Users$env:USERNAME\FROM" -Directory | Sort-Object)
$menu = @{}
for ($i=1;$i -le MainFolder.count; $i++) {
Write-Host "$i. $(MainFolder[$i-1].name)"
$menu.Add($i,(MainFolder[$i-1].name))
}
[int]$ans = Read-Host 'Enter selection'
$selection = $menu.Item($ans)
# The directory after the selection is only one, however it's different for every user; this remains the same for "COPY TO"
$First = (Get-ChildItem "C:\Users$env:USERNAME\FROM$selection" -Directory)
# This directory is the same for every user; this remains the same for "COPY TO"
$DataFROM = (Set-Location $First\USERFILE)
Clear-Host
Write-Host "Choose where to copy TO"
MainFolder = (Get-ChildItem "C:\Users$env:USERNAME\TO" -Directory | Sort-Object)
$menu2 = @{}
for ($i=1;$i -le MainFolder.count; $i++) {
Write-Host "$i. $(MainFolder[$i-1].name)"
$menu2.Add($i,(MainFolder[$i-1].name))
}
[int]$ans2 = Read-Host 'Enter selection'
$selection2 = $menu2.Item($ans2)
$Second = (Get-ChildItem "C:\Users$env:USERNAME\TO$selection2" -Directory)
$DataTO = (Set-Location $Second\USERFILE)
Clear-Host
第一节和第二节到此结束。第三部分允许用户选择他们想要复制的文件——这是一个单独的功能,效果很好,但与此相关时会出错。我已经简化了函数以获得相同的错误:
# The Select-Object section being the simplified part...
Get-ChildItem $DataFROM | Select-Object "DATA.log*" | Copy-Item -Destination $DataTO
我希望发生的是将数据从初始动态位置复制到最终动态目标。
我收到一条错误消息,指出“无法将参数绑定到参数 'Path',因为它是一个空字符串。”它指向 $DataTO 变量。
这导致我忽略第三部分并测试我发现的变量,如果我在前两部分之后使用以下内容:
Get-ChildItem $DataFROM
# or
Get-ChildItem $DataTO
它 returns 从上次选择的菜单返回信息,完全忽略变量。那么动态菜单是不是不能这样用,还是我用错了菜单?
有人能告诉我哪里误入歧途了吗?
更新:自从 re-worked 以来,我已经将其更新为满足我要求的需求的以下内容:(希望这将帮助其他需要类似内容的人)
[array]$sMainFolder = Get-ChildItem "C:\Users$env:USERNAME\DATA-TO_FROM" -Directory | ForEach-Object {
Get-ChildItem $_.FullName | ForEach-Object {
$found = Get-ChildItem $_.FullName | Where-Object { $_.Name -eq 'USERFILE' } | Select-Object -First 1
if ($found) {
[PSCustomObject]@{
RootFolder = [string]$_.FullName
Path = $found.FullName
}
}
}
}
[array]$dMainFolder = Get-ChildItem "C:\Users$env:USERNAME\DATA-TO_FROM" -Directory | ForEach-Object {
$subs = Get-ChildItem $_.FullName
$found = $subs | ForEach-Object { Get-ChildItem $_.FullName } | Where-Object { $_.Name -eq 'USERFILE' } | Select-Object -First 1
if ($found) {
[PSCustomObject]@{
RootFolder = [string]$_.FullName
Path = $found.FullName
}
}
}
function ShowMenu ($Folders, $Message) {
Write-Host $Message
for ($i = 1; $i -le $Folders.count; $i++) {
$name = $Folders[$i - 1].RootFolder
Write-Host "$i. $name"
}
[int]$ans = Read-Host 'Enter Selection'
Return $Folders[$ans -1].Path
}
$DataFROM = ShowMenu $sMainFolder "Choose where to copy FROM"
$DataTO = ShowMenu $dMainFolder "Choose where to copy TO"
Clear-Host
Copy-Item "$DataFROM\*" $DataTO -Recurse -Force
这样做的目的是将一个(或多个)文件从一个用户选择的动态位置复制到另一个用户选择的动态位置。
Write-Host "Choose where to copy FROM"
MainFolder = (Get-ChildItem "C:\Users$env:USERNAME\FROM" -Directory | Sort-Object)
$menu = @{}
for ($i=1;$i -le MainFolder.count; $i++) {
Write-Host "$i. $(MainFolder[$i-1].name)"
$menu.Add($i,(MainFolder[$i-1].name))
}
[int]$ans = Read-Host 'Enter selection'
$selection = $menu.Item($ans)
# The directory after the selection is only one, however it's different for every user; this remains the same for "COPY TO"
$First = (Get-ChildItem "C:\Users$env:USERNAME\FROM$selection" -Directory)
# This directory is the same for every user; this remains the same for "COPY TO"
$DataFROM = (Set-Location $First\USERFILE)
Clear-Host
Write-Host "Choose where to copy TO"
MainFolder = (Get-ChildItem "C:\Users$env:USERNAME\TO" -Directory | Sort-Object)
$menu2 = @{}
for ($i=1;$i -le MainFolder.count; $i++) {
Write-Host "$i. $(MainFolder[$i-1].name)"
$menu2.Add($i,(MainFolder[$i-1].name))
}
[int]$ans2 = Read-Host 'Enter selection'
$selection2 = $menu2.Item($ans2)
$Second = (Get-ChildItem "C:\Users$env:USERNAME\TO$selection2" -Directory)
$DataTO = (Set-Location $Second\USERFILE)
Clear-Host
第一节和第二节到此结束。第三部分允许用户选择他们想要复制的文件——这是一个单独的功能,效果很好,但与此相关时会出错。我已经简化了函数以获得相同的错误:
# The Select-Object section being the simplified part...
Get-ChildItem $DataFROM | Select-Object "DATA.log*" | Copy-Item -Destination $DataTO
我希望发生的是将数据从初始动态位置复制到最终动态目标。
我收到一条错误消息,指出“无法将参数绑定到参数 'Path',因为它是一个空字符串。”它指向 $DataTO 变量。
这导致我忽略第三部分并测试我发现的变量,如果我在前两部分之后使用以下内容:
Get-ChildItem $DataFROM
# or
Get-ChildItem $DataTO
它 returns 从上次选择的菜单返回信息,完全忽略变量。那么动态菜单是不是不能这样用,还是我用错了菜单?
有人能告诉我哪里误入歧途了吗?
更新:自从 re-worked 以来,我已经将其更新为满足我要求的需求的以下内容:(希望这将帮助其他需要类似内容的人)
[array]$sMainFolder = Get-ChildItem "C:\Users$env:USERNAME\DATA-TO_FROM" -Directory | ForEach-Object {
Get-ChildItem $_.FullName | ForEach-Object {
$found = Get-ChildItem $_.FullName | Where-Object { $_.Name -eq 'USERFILE' } | Select-Object -First 1
if ($found) {
[PSCustomObject]@{
RootFolder = [string]$_.FullName
Path = $found.FullName
}
}
}
}
[array]$dMainFolder = Get-ChildItem "C:\Users$env:USERNAME\DATA-TO_FROM" -Directory | ForEach-Object {
$subs = Get-ChildItem $_.FullName
$found = $subs | ForEach-Object { Get-ChildItem $_.FullName } | Where-Object { $_.Name -eq 'USERFILE' } | Select-Object -First 1
if ($found) {
[PSCustomObject]@{
RootFolder = [string]$_.FullName
Path = $found.FullName
}
}
}
function ShowMenu ($Folders, $Message) {
Write-Host $Message
for ($i = 1; $i -le $Folders.count; $i++) {
$name = $Folders[$i - 1].RootFolder
Write-Host "$i. $name"
}
[int]$ans = Read-Host 'Enter Selection'
Return $Folders[$ans -1].Path
}
$DataFROM = ShowMenu $sMainFolder "Choose where to copy FROM"
$DataTO = ShowMenu $dMainFolder "Choose where to copy TO"
Clear-Host
Copy-Item "$DataFROM\*" $DataTO -Recurse -Force