在同一行中使用两个或多个set / p

时间:2019-05-22 22:48:02

标签: batch-file batch-processing

我正在尝试编写一个脚本,用户需要在同一行中填充3个空格。 有可能吗?

例如,以以下方式填写日期:25/02/2019day 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

4 个答案:

答案 0 :(得分:2)

没有评论... ;)

@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

答案 1 :(得分:0)

这是最接近您想要的内容。它需要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

答案 2 :(得分:0)

如果系统是受支持的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%
)

=== guical.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'))"
}

答案 3 :(得分:0)

如果更适合您的目的,也可以直接从循环中使用

像这样的事情可能就足够了;提示最终用户以特定格式输入日期,并且仅当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