Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to make a batch file interactive

How to make a batch file interactive

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminwindows-adminlinuxtutorialworkspace
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave Midgley
    wrote on last edited by
    #1

    I'm trying to make an interactive batch file (.cmd) to run under Windows NT. The obvious way is to have a program that displays a prompt and sets an environment variable with the response - the batch file can then access the input value via the environment variable (as %environmentvariable%), however 1) using _setenv() only alters the environment for the current program, so the batch file that ran the program does not see the new variable; conversely 2) altering the registry where the environment variables are kept (HKEY_CURRENT_USER\Environment etc.) has no effect until a new DOS shell is started, so the batch file that ran the program does not see the new variable; 3) the old DOS method of getting the PSP and walking up the environment areas isn't allowed under NT (and there's never enough space in the environment area anyway); If anyone knows how to write a program that can do this, or any other way of making a batch file interactive, I'd love to hear about it. (PS I know that using VBS or some other scripting language is the right way to go, but I need this to run on any NT Server machine, and the CMD processor seems to be the only scripting engine that is guaranteed to be available) Thanks Dave

    T L 2 Replies Last reply
    0
    • D Dave Midgley

      I'm trying to make an interactive batch file (.cmd) to run under Windows NT. The obvious way is to have a program that displays a prompt and sets an environment variable with the response - the batch file can then access the input value via the environment variable (as %environmentvariable%), however 1) using _setenv() only alters the environment for the current program, so the batch file that ran the program does not see the new variable; conversely 2) altering the registry where the environment variables are kept (HKEY_CURRENT_USER\Environment etc.) has no effect until a new DOS shell is started, so the batch file that ran the program does not see the new variable; 3) the old DOS method of getting the PSP and walking up the environment areas isn't allowed under NT (and there's never enough space in the environment area anyway); If anyone knows how to write a program that can do this, or any other way of making a batch file interactive, I'd love to hear about it. (PS I know that using VBS or some other scripting language is the right way to go, but I need this to run on any NT Server machine, and the CMD processor seems to be the only scripting engine that is guaranteed to be available) Thanks Dave

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      I don't know if modifying parent process' env block is possible. I'd give up setting the result in environment vars directly, instead, I'd write something to dynamically created .bat or .cmd file. If user enters 'foo', the file would look like this: set var=foo Main batch would call this file to make changes. Tomasz Sowinski -- http://www.shooltz.com

      D 1 Reply Last reply
      0
      • T Tomasz Sowinski

        I don't know if modifying parent process' env block is possible. I'd give up setting the result in environment vars directly, instead, I'd write something to dynamically created .bat or .cmd file. If user enters 'foo', the file would look like this: set var=foo Main batch would call this file to make changes. Tomasz Sowinski -- http://www.shooltz.com

        D Offline
        D Offline
        Dave Midgley
        wrote on last edited by
        #3

        Thanks, but it won't work - when the main batch calls the sub batch the sub batch will set the variable in its own environment, but then it disappears again when the sub batch returns to the main batch. Dave Dave

        T 1 Reply Last reply
        0
        • D Dave Midgley

          Thanks, but it won't work - when the main batch calls the sub batch the sub batch will set the variable in its own environment, but then it disappears again when the sub batch returns to the main batch. Dave Dave

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          Are you sure? I've tested this on my XP Pro. When you execute b1, SET displays var with the value of 'foo'.

          rem b1.bat
          call b2.bat
          set

          rem b2.bat
          set var=foo

          Tomasz Sowinski -- http://www.shooltz.com

          1 Reply Last reply
          0
          • D Dave Midgley

            I'm trying to make an interactive batch file (.cmd) to run under Windows NT. The obvious way is to have a program that displays a prompt and sets an environment variable with the response - the batch file can then access the input value via the environment variable (as %environmentvariable%), however 1) using _setenv() only alters the environment for the current program, so the batch file that ran the program does not see the new variable; conversely 2) altering the registry where the environment variables are kept (HKEY_CURRENT_USER\Environment etc.) has no effect until a new DOS shell is started, so the batch file that ran the program does not see the new variable; 3) the old DOS method of getting the PSP and walking up the environment areas isn't allowed under NT (and there's never enough space in the environment area anyway); If anyone knows how to write a program that can do this, or any other way of making a batch file interactive, I'd love to hear about it. (PS I know that using VBS or some other scripting language is the right way to go, but I need this to run on any NT Server machine, and the CMD processor seems to be the only scripting engine that is guaranteed to be available) Thanks Dave

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Consider using Windows Scripting Host (WSH) - it is intended exactly for such purposes. In WinNT WSH is available after installing Option Pack 4. In WSH you write scripts using Visual Basic Scripting Edition.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups