Because shell creates a new local copy of the shell environment, and sets thefilenam variable there.
Then the shell is closed, and the environment for that shell is lost.
To do what you want you have to set the filenam in the same shell as the program is executed in. A batfile to start it works.
Just do a batch file that runs the program that changes the environment, then the program that requires the new environment in the same DOS window.
If you close the window the changes will be lost, and they won't affect to a different DOS window.