2

On Unix and Mac machines, environmental variables can be read from .env files which have 'export' commands in them.

For example:

export USER_ENV=test
export CLIENT=company

In windows, there's no such thing as the 'export' command. I therefore use the 'set' command to set the environmental variables.

set USER_ENV=test
set CLIENT=company

The problem is that I have to enter each and every line individually on the command prompt.
Is there anyway I can run these 'set' commands to set the variables from a single test file?

This would save me the need to run each command individually from CMD.

I tried by entering the commands in a file setenv.txt and used the following command:

cmd < setenv.txt

That did not work as intended. I saw errors while trying to start Cypress.

How can I run these commands from a file via the CMD terminal in Windows?

1 Answer 1

1

You can create a batch file where you set all your environment variables.

A batch file in Windows is just a text file with .bat extension, it could look like this:

@ECHO OFF

:: Assign all Path variables
SET TEST="some env value"

Then you execute it from the cmd:

C:\Users\PavelSaman\Desktop>echo %TEST%
%TEST%

C:\Users\PavelSaman\Desktop>env.bat

C:\Users\PavelSaman\Desktop>echo %TEST%
"some env value"

and you've set up what you previously did command by command.

You can also set a system environment variable via setx command with /m parameter:

:: add system environment variable (you need admin rights for this)
setx TEST2 "system environment" /m

You can find more here: https://stackoverflow.com/questions/21606419/set-windows-environment-variables-with-a-batch-file or here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/setx

2
  • Are all the three actions mentioned above necessary?
    – srini
    May 12, 2020 at 13:23
  • Well, you asked about "how to run these command from a file", so I answered mostly that. Another option how to set variables for Cypress is summed up here: docs.cypress.io/guides/guides/…
    – pavelsaman
    May 12, 2020 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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