批处理文件以处理输入并导出到文本文件

时间:2019-04-15 15:01:54

标签: batch-file

我需要用户输入1-20个输入,然后接受这些输入并在其中建立文件。

我可以使用不同的功能,但是先问用户要输入多少个条目,我想问用户输入他们的条目,完成后例如按“ q”,这将构建所需的文件。

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
setlocal

:mainmenu
echo 1. 1 user
echo 2. 2 users
echo 3. 3 users
set selection=
set /p selection=Please choose a selection [1 - 3]:
if %selection% EQU 1 goto 1user
if %selection% EQU 2 goto 2users
if %selection% EQU 3 goto 3users
cls
goto mainmenu

:1user
set /p user1=Username 1

echo %user1%

(
echo rem Delete User Profiles after 2 days - Log activity to log.txt
echo "C:\Users\Administrator\Documents\apps\delprof2\delprof2.exe /ed:%user1% /d:2 /u /i ^>^> "C:\temp\log.txt""
echo rem Set following variable for file size in Bytes (1024 Bytes=1KB,  1024KB=1MB, 1024MB=1GB)
echo SET /A FileSize=6288
echo rem Set following variable for file extensions to check (*.* = all  files)
echo SET Filter=log.txt
echo rem Set following variable with path to check insided for files
echo SET Folder=c:\temp\
echo rem Delete the file if matches above
echo FOR /R "%Folder%" %%F IN (%Filter%) DO (
echo IF %%~zF GTR %FileSize% (
echo ECHO Deleting "%%F"
echo DEL /F "%%F"))
echo EXIT /B /0
) >> "c:\temp\testbat.txt"
timeout 10
goto mainmenu

1 个答案:

答案 0 :(得分:0)

您基本上询问了“直到q为止的输入”。循环很简单:

@echo off
setlocal

set nr=1
:loop
  set /p "newuser=Enter user %nr% or [q] to quit: "
  if /i "%newuser%"=="q" set /a nr-=1 & goto :done
  set user_%nr%=%newuser%
  set /a nr+=1
goto:loop

:done
echo you entered %nr% users:
set user_
REM rest of your code

注意:LotPings是正确的:To not prematurely ending a (code block) all closing praenthesis inside have to be escaped with a caret ^):要在代码块中echo文字),您必须echo ^),否则它将结束您的代码块(太早)。