在同一行中使用两个或多个 set /p
Using two or more set /p in the same line
我正在尝试制作一个脚本,用户需要在同一行中填充 3 个空格。
可能吗?
例如填写日期:25/02/2019
这样:day here / month here / year here
不是逐行的。
正常的方式是:
set /p "dd=Type the Day: "
set /p "mm=Type the Month: %dd% / "
set /p "aa=Type The Year: %dd% / %mm% / "
结果将是:22/05/2019
但我想把这 3 个 set /p
放在一行中,只填充这一行。
我这样试过:
set dd=%%a
set mm=%%b
set aa=%%c
set /p "test= "!dd! !mm! !aa!
echo %%a / %%b / %%c
pause
也尝试过:
set /p "dd=Type the day: " / &set /p "mm=Type the month " &set /p "aa=Type the year "
但不幸的是没有奏效。第二种方式逐行工作。
编辑 1:
按照我的最终脚本通过批处理命令更改 Windows 日期:
@echo off
setlocal enableextensions
setlocal EnableDelayedExpansion
cd /d "%~dp0"
mode 42,12
:begin
cls
echo ------------------------------------------
echo MUDAR A DATA DO WINDOWS
echo ------------------------------------------
echo( &echo(
echo 1 - Escolher a data & echo( &echo 2 - Retornar para a data atual
echo( & echo( &echo(
choice /n /c:12 /m "Digite uma op‡Æo:"%1
call :lab%errorlevel%
:lab 1
cls
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set /P "=Digite a data: / / !CR!Digite a data: " < nul
call :GetDigits 2 3 Day=
set /P "=.%BS% / " < nul
call :GetDigits 2 1 Mon=
set /P "=.%BS% / " < nul
call :GetDigits 4 2 Year=
echo/
echo/
net stop w32time >nul 2>nul
sc config w32time start= disabled >nul 2>nul
date %Day%-%Mon%-%Year% <nul && (
echo Data modificada: %Day%/%Mon%/%Year%
timeout /nobreak /t 2 >nul 2>nul
goto begin
) || (
cls & echo Erro ao mudar a data.
echo( &echo( &echo(
pause
goto begin
)
:GetDigits HowMany FirstLimit Result=
setlocal EnableDelayedExpansion
set "digits=123456789"
set "%3="
choice /C "0!digits:~0,%2!" /N > nul
set /A digit=%errorlevel%-1
set /P "=%digit%" < nul
set "%3=!%3!%digit%"
for /L %%i in (2,1,%1) do (
choice /C "0%digits%" /N > nul
set /A digit=!errorlevel!-1
set /P "=!digit!" < nul
set "%3=!%3!!digit!"
)
for %%a in (!%3!) do endlocal & set "%3=%%a"
exit /B
:lab2
sc config w32time start= demand >nul
net start w32time >nul 2>nul
cls & w32tm /resync >nul 2>&1
echo Data atual retornada.
timeout /nobreak /t 2 >nul 2>nul
cls & goto begin
这是最接近您想要的。它需要 choice
命令。
仍然有一个闪烁的光标有点烦人,但如果您愿意,您可以使用第三方命令解决这个问题。
@echo off
choice /c "1234567890" /m "__/__/__" /n
set x1=%errorlevel%
if errorlevel 10 set x1=0
cls
choice /c "1234567890" /m "%x1%_/__/__" /n
set x2=%errorlevel%
if errorlevel 10 set x2=0
cls
choice /c "1234567890" /m "%x1%%x2%/__/__" /n
set x3=%errorlevel%
if errorlevel 10 set x3=0
cls
choice /c "1234567890" /m "%x1%%x2%/%x3%_/__" /n
set x4=%errorlevel%
if errorlevel 10 set x4=0
cls
choice /c "1234567890" /m "%x1%%x2%/%x3%%x4%/__" /n
set x5=%errorlevel%
if errorlevel 10 set x5=0
cls
choice /c "1234567890" /m "%x1%%x2%/%x3%%x4%/%x5%_" /n
set x6=%errorlevel%
if errorlevel 10 set x6=0
cls
echo Date - %x1%%x2%/%x3%%x4%/%x5%%x6%
pause>nul
如果系统是受支持的 Windows 平台,可以从 .bat 文件脚本调用 PowerShell 以显示用于日期选择的 GUI 日历。将 .ps1 脚本放在与 .bat 脚本相同的目录中。改编自 https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-graphical-date-picker?view=powershell-6
=== guical.bat
set "THEDATE="
for /f "delims=" %%d in ('powershell -NoLogo -NoProfile -File "%~dp0guical.ps1"') do (
set "THEDATE=%%~d"
)
if "%THEDATE%" == "" (
echo No date selected.
) else (
echo Selected date is %THEDATE%
)
=== 鬼怪。ps1
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object Windows.Forms.Form -Property @{
StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
Size = New-Object Drawing.Size 233, 270
Text = 'Select a Date'
Topmost = $true
}
$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
ShowTodayCircle = $false
MaxSelectionCount = 1
}
$form.Controls.Add($calendar)
$OKButton = New-Object Windows.Forms.Button -Property @{
Location = New-Object Drawing.Point 38, 200
Size = New-Object Drawing.Size 75, 23
Text = 'OK'
DialogResult = [Windows.Forms.DialogResult]::OK
}
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object Windows.Forms.Button -Property @{
Location = New-Object Drawing.Point 113, 200
Size = New-Object Drawing.Size 75, 23
Text = 'Cancel'
DialogResult = [Windows.Forms.DialogResult]::Cancel
}
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$result = $form.ShowDialog()
if ($result -eq [Windows.Forms.DialogResult]::OK) {
$date = $calendar.SelectionStart
Write-Host "$($date.ToString('MM\/dd\/yyyy'))"
}
您也可以使用 powershell directly from a for loop in your batch-file,如果它更适合您的目的:
像这样可能就足够了;提示最终用户以特定格式输入日期,并且仅当 PowerShell
将输入识别为有效日期时才继续:
@Echo Off
Set "Input="
For /F "Tokens=*" %%A In ('PowerShell -NoP^
"$d=$Null;While(1){Try{$d=[DateTime](Read-Host 'Date [dd/MM/yyyy]')"^
";Break}Catch{}};$d.ToString('dd/MM/yyyy')" 2^>Nul')Do Set "Input=%%A"
Set Input 2>Nul&Pause
没有评论...;)
@echo off
setlocal EnableDelayedExpansion
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set /P "=Enter the date: DD / MM / YYYY!CR!Enter the date: " < nul
call :GetDigits 2 3 Day=
set /P "=.%BS% / " < nul
call :GetDigits 2 1 Mon=
set /P "=.%BS% / " < nul
call :GetDigits 4 2 Year=
echo/
echo/
echo Result: %Day%/%Mon%/%Year%
goto :EOF
:GetDigits HowMany FirstLimit Result=
setlocal EnableDelayedExpansion
set "digits=123456789"
set "%3="
choice /C "0!digits:~0,%2!" /N > nul
set /A digit=%errorlevel%-1
set /P "=%digit%" < nul
set "%3=!%3!%digit%"
for /L %%i in (2,1,%1) do (
choice /C "0%digits%" /N > nul
set /A digit=!errorlevel!-1
set /P "=!digit!" < nul
set "%3=!%3!!digit!"
)
for %%a in (!%3!) do endlocal & set "%3=%%a"
exit /B
我正在尝试制作一个脚本,用户需要在同一行中填充 3 个空格。 可能吗?
例如填写日期:25/02/2019
这样:day here / month here / year here
不是逐行的。
正常的方式是:
set /p "dd=Type the Day: "
set /p "mm=Type the Month: %dd% / "
set /p "aa=Type The Year: %dd% / %mm% / "
结果将是:22/05/2019
但我想把这 3 个 set /p
放在一行中,只填充这一行。
我这样试过:
set dd=%%a
set mm=%%b
set aa=%%c
set /p "test= "!dd! !mm! !aa!
echo %%a / %%b / %%c
pause
也尝试过:
set /p "dd=Type the day: " / &set /p "mm=Type the month " &set /p "aa=Type the year "
但不幸的是没有奏效。第二种方式逐行工作。
编辑 1:
按照我的最终脚本通过批处理命令更改 Windows 日期:
@echo off
setlocal enableextensions
setlocal EnableDelayedExpansion
cd /d "%~dp0"
mode 42,12
:begin
cls
echo ------------------------------------------
echo MUDAR A DATA DO WINDOWS
echo ------------------------------------------
echo( &echo(
echo 1 - Escolher a data & echo( &echo 2 - Retornar para a data atual
echo( & echo( &echo(
choice /n /c:12 /m "Digite uma op‡Æo:"%1
call :lab%errorlevel%
:lab 1
cls
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set /P "=Digite a data: / / !CR!Digite a data: " < nul
call :GetDigits 2 3 Day=
set /P "=.%BS% / " < nul
call :GetDigits 2 1 Mon=
set /P "=.%BS% / " < nul
call :GetDigits 4 2 Year=
echo/
echo/
net stop w32time >nul 2>nul
sc config w32time start= disabled >nul 2>nul
date %Day%-%Mon%-%Year% <nul && (
echo Data modificada: %Day%/%Mon%/%Year%
timeout /nobreak /t 2 >nul 2>nul
goto begin
) || (
cls & echo Erro ao mudar a data.
echo( &echo( &echo(
pause
goto begin
)
:GetDigits HowMany FirstLimit Result=
setlocal EnableDelayedExpansion
set "digits=123456789"
set "%3="
choice /C "0!digits:~0,%2!" /N > nul
set /A digit=%errorlevel%-1
set /P "=%digit%" < nul
set "%3=!%3!%digit%"
for /L %%i in (2,1,%1) do (
choice /C "0%digits%" /N > nul
set /A digit=!errorlevel!-1
set /P "=!digit!" < nul
set "%3=!%3!!digit!"
)
for %%a in (!%3!) do endlocal & set "%3=%%a"
exit /B
:lab2
sc config w32time start= demand >nul
net start w32time >nul 2>nul
cls & w32tm /resync >nul 2>&1
echo Data atual retornada.
timeout /nobreak /t 2 >nul 2>nul
cls & goto begin
这是最接近您想要的。它需要 choice
命令。
仍然有一个闪烁的光标有点烦人,但如果您愿意,您可以使用第三方命令解决这个问题。
@echo off
choice /c "1234567890" /m "__/__/__" /n
set x1=%errorlevel%
if errorlevel 10 set x1=0
cls
choice /c "1234567890" /m "%x1%_/__/__" /n
set x2=%errorlevel%
if errorlevel 10 set x2=0
cls
choice /c "1234567890" /m "%x1%%x2%/__/__" /n
set x3=%errorlevel%
if errorlevel 10 set x3=0
cls
choice /c "1234567890" /m "%x1%%x2%/%x3%_/__" /n
set x4=%errorlevel%
if errorlevel 10 set x4=0
cls
choice /c "1234567890" /m "%x1%%x2%/%x3%%x4%/__" /n
set x5=%errorlevel%
if errorlevel 10 set x5=0
cls
choice /c "1234567890" /m "%x1%%x2%/%x3%%x4%/%x5%_" /n
set x6=%errorlevel%
if errorlevel 10 set x6=0
cls
echo Date - %x1%%x2%/%x3%%x4%/%x5%%x6%
pause>nul
如果系统是受支持的 Windows 平台,可以从 .bat 文件脚本调用 PowerShell 以显示用于日期选择的 GUI 日历。将 .ps1 脚本放在与 .bat 脚本相同的目录中。改编自 https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-graphical-date-picker?view=powershell-6
=== guical.bat
set "THEDATE="
for /f "delims=" %%d in ('powershell -NoLogo -NoProfile -File "%~dp0guical.ps1"') do (
set "THEDATE=%%~d"
)
if "%THEDATE%" == "" (
echo No date selected.
) else (
echo Selected date is %THEDATE%
)
=== 鬼怪。ps1
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object Windows.Forms.Form -Property @{
StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
Size = New-Object Drawing.Size 233, 270
Text = 'Select a Date'
Topmost = $true
}
$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
ShowTodayCircle = $false
MaxSelectionCount = 1
}
$form.Controls.Add($calendar)
$OKButton = New-Object Windows.Forms.Button -Property @{
Location = New-Object Drawing.Point 38, 200
Size = New-Object Drawing.Size 75, 23
Text = 'OK'
DialogResult = [Windows.Forms.DialogResult]::OK
}
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object Windows.Forms.Button -Property @{
Location = New-Object Drawing.Point 113, 200
Size = New-Object Drawing.Size 75, 23
Text = 'Cancel'
DialogResult = [Windows.Forms.DialogResult]::Cancel
}
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$result = $form.ShowDialog()
if ($result -eq [Windows.Forms.DialogResult]::OK) {
$date = $calendar.SelectionStart
Write-Host "$($date.ToString('MM\/dd\/yyyy'))"
}
您也可以使用 powershell directly from a for loop in your batch-file,如果它更适合您的目的:
像这样可能就足够了;提示最终用户以特定格式输入日期,并且仅当 PowerShell
将输入识别为有效日期时才继续:
@Echo Off
Set "Input="
For /F "Tokens=*" %%A In ('PowerShell -NoP^
"$d=$Null;While(1){Try{$d=[DateTime](Read-Host 'Date [dd/MM/yyyy]')"^
";Break}Catch{}};$d.ToString('dd/MM/yyyy')" 2^>Nul')Do Set "Input=%%A"
Set Input 2>Nul&Pause
没有评论...;)
@echo off
setlocal EnableDelayedExpansion
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set /P "=Enter the date: DD / MM / YYYY!CR!Enter the date: " < nul
call :GetDigits 2 3 Day=
set /P "=.%BS% / " < nul
call :GetDigits 2 1 Mon=
set /P "=.%BS% / " < nul
call :GetDigits 4 2 Year=
echo/
echo/
echo Result: %Day%/%Mon%/%Year%
goto :EOF
:GetDigits HowMany FirstLimit Result=
setlocal EnableDelayedExpansion
set "digits=123456789"
set "%3="
choice /C "0!digits:~0,%2!" /N > nul
set /A digit=%errorlevel%-1
set /P "=%digit%" < nul
set "%3=!%3!%digit%"
for /L %%i in (2,1,%1) do (
choice /C "0%digits%" /N > nul
set /A digit=!errorlevel!-1
set /P "=!digit!" < nul
set "%3=!%3!!digit!"
)
for %%a in (!%3!) do endlocal & set "%3=%%a"
exit /B