0

The problem lies in this part of the code:

for /l %%x in (1, 1, %num%) do (
   set /p "inp=Enter Ext of PC: "
   echo pc-%inp%x>>"Myfile.log"
)

Here is the full code:

@ECHO OFF

:: +++++++++++++++++++ MENU +++++++++++++++++++++
:menu
cls
echo.
echo 1) Add PCs to Log File
echo 2) View Log File
echo 3) Exit
echo.
goto option

:option
set /p op=Type option #:
if "%op%"=="1" goto addext
if "%op%"=="2" goto viewcnt
if "%op%"=="3" goto exit


:: ++++++++++++++++++++ OPTIONS ++++++++++++++++++++++

:addext
:: Enter Number of PCs to Add to Log File
echo.
set /p "num=Enter Num of PCs to Add: "
echo.
goto addlog

:addlog
:: Create/Clear Log File/Enter Computer Extensions/Write to Log File
cls
echo.
echo.>"Myfile.log"
for /l %%x in (1, 1, %num%) do (
   set /p "inp=Enter Ext of PC: "
   echo pc-%inp%x>>"Myfile.log"
)
echo.
pause
goto menu

:viewcnt
:: View Log File
type Myfile.log
echo.
pause
goto menu

:exit
@exit

Objectives

Enter Number of PCs to Add to Log File - Works

set /p "num=Enter Num of PCs to Add To: "

Output:

Enter Num of PCs to Add To:

Create / Erase Log File (each time option 1 of menu is choosen) - Works

echo.>"Myfile.log"

Set variable %inp% for each of the Number of Computers from %num% variable - Need Help

for /l %%x in (1, 1, %num%) do (
   set /p "inp=Enter Ext of PC: "
   echo pc-%inp%x>>"Myfile.log"
)

Output:

Enter Ext of PC:

(Iterates the number of times %num% is equal to)

  • Prompts for input of extension variable for the number of times prescribed by previous input - Works
  • Able to type in each extension - Works
  • Not sure what happens to the variable entered - Need Help

Write the Variables to the Log File - Need Help

echo pc-%inp%x>>"Myfile.log"
  • The prefix preceding the variable shows the number of times entered - Works
  • Not including the inputted variables - Need Help

View Log File - Works

type Myfile.log

Further Considerations:

  • pc-%inp% is the computer name ( prefix: pc- ) followed by (variable: %inp% / example: pc-a001 )
  • %inp% needs to be Alphanumberic ( one letter followed by 3 numbers: a001 )
  • 1
  • @DavidPostill - Thank you for your input, but I am still getting the same results. I inserted the EnableDelayedExpansion: SETLOCAL EnableDelayedExpansion echo.>"Myfile.log" for /l %%x in (1, 1, %num%) do ( set /p "inp=Enter Ext of PC: " echo pc-%inp%x>>"Myfile.log" ) – cgitech May 17 '18 at 20:07
  • You missed the point. Your need to replace -%inp%x with -!inp!x as well. – DavidPostill May 17 '18 at 20:20
  • Wow!!! I did miss that totally!!! Thanks. Also the x in !inp!x is unnecessary. – cgitech May 17 '18 at 20:32
0

Here is the code that works:

SETLOCAL EnableDelayedExpansion
echo.>"Myfile.log"
for /l %%x in (1, 1, %num%) do (
   set /p "inp=Enter Ext of PC: "
   echo pc-!inp!>>"Myfile.log"
)

Changes needed were:

  • adding the SETLOCAL EnableDelayedExpansion
  • changing %inp%x to !inp!

Thanks to DavidPostill for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.