我在命令shell代码中有以下代码。
Integer opt = JOptionPane.showConfirmDialog(null, "This application cannot continue without login to the database\n Are you sure you need to exit...?", "Application X", JOptionPane.YES_NO_OPTION);
if(opt== JOptionPane.NO_OPTION){
this.setVisible(true);
}else{
System.exit(0);
}
但是,我正在将我的代码从CMD转换为PowerShell。我不完全理解转换的正确方法。这可能是它应该如何完成的,但它不起作用,因为它只是直接跳过。
SET MYdir=%NewPath%\%CUST%\SuppliesTypes
SET "MYsCount=1"
SET /p MYsCount="Number of MYs in project? (default: %MYSCount%): "
for /L %%a in (1,1,%MYsCount%) do (
SET /p MYNums="Enter %%a MY Number: "
call md "%MYdir%\MY_%%MYNums%%"
)
SET "MYsCount="
我查看了以下网站和文章:
我在While-Loop中运行此代码。
感谢您的帮助!
答案 0 :(得分:3)
你的第二个代码块中有一个有趣的批处理文件和powershell混合。当某些东西是一种语言而某些东西是另一种语言时,很难阅读。让我们看看我们是否可以在这里全部了解PowerShell。
$MYdir = "$NewPath\$CUST\Product"
$MYsCount = 1
$UserMYsCount = Read-Host "Number of MYs in project? (default: $MYSCount): "
If([string]::IsNullOrEmpty($UserMYsCount){
$UserMYsCount = $MYsCount
}
For ($i = 1; $i -le $UserMYsCount; $I++){
$MyNums = Read-Host "Enter $i Product Numbers: "
New-Item -Path "$MYdir\MY_$MyNums" -ItemType Directory
}
答案 1 :(得分:1)
我认为这个问题来自你如何宣布你的变量。 SET将变量创建为环境变量,powershell不会本机访问。以下是我将如何编写您的代码部分:
$MYDir = "$env:NewPath\$env:CUST\SuppliesTypes"
$MYsCount = 1
$MYsCount = read-host -prompt "Number of MYs in project? (default: $MYSCount): "
foreach ($a in 0..$MYsCount){
$MYNums = Read-Host -Prompt "Enter $a Product Numbers: "
New-Item -Path "$MYDir\MY_$MYNums" -ItemType Directory
}
$MYsCount = $null
我使用了一个foreach循环而不是一个普通for循环,因为你每次都递增1,我注意到当步骤不复杂时使用foreach会有很小的性能提升。 0 .. $ variable是使用从0到声明变量的每个数字的简写。
如果您确实想要使用for循环,那么您可以使用:
For ($MYsCount = 1; $MYsCount -eq 10; $MYsCount++){
正如你所预料的那样。这个循环只有在$ MYsCount变量等于10时才会停止,所以如果有人将变量设置为高于10的值,它将无限期地运行。