Windows FOR command not expanding system environment variables

keNscale

I have been using this site for sometime and have found it quite valuable in finding solutions to scripting issues. Today I resolve to ask a question as I have not found my solution after exhausting 3 days here, on Superuser, and Googling. So much for all about me, now my point...

I have several installs of a program, each slightly different version or flavor. Each install has a text file defining variables for its use. One of these variables defines a folder for user specific settings, USERSET=%APPDATA%\foo1\foo2. Overtime these user settings can become corrupt. I want to be able to delete these folders with a batch file or script. In short, I want to search a directory for text files that contain a specific string which sets a variable that points to a folder, get the value of that variable and use it to remove a directory.

This is what I have so far:

for /f "tokens=2 delims==" %%G in ('dir /b /s c:\foo\*.txt ^| findstr /i /f:/ userset') do rd /s /q %%G

The output looks like this:

H:\>rd /s /q %appdata%\foo1\foo2\USERSET   
The system cannot find the file specified.

Apparently %appdata% does not expand?

Of course if I just type rd /s /q %appdata%\foo1\foo2\USERSET at the command prompt, it works.

I'm open to other languages, I am mostly familiar with Windows command line.

Joey

This is because environment variable expansion is done at parse time of a statement. While your for loop is running and %%G actually gets a value it's too late; variables have already been expanded.

You can work around this with a subroutine, I guess:

pushd C:\foo
for /f ...  %%G in (...) do call :process "%%G"
popd
goto :eof

:process
  call set "X=%~1"
  rd /s /q "%X%"
  goto :eof

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Manipulation with user and system environment variables from command line in Windows 10

From Dev

Expanding environment variables with sed

From Dev

Expanding vim variables in the set command

From Dev

Environment variables in meteor command line for windows

From Dev

Ruby system call with environment variables in windows

From Dev

Expanding variables in a Windows batch file

From Java

Is there a command to refresh environment variables from the command prompt in Windows?

From Dev

Is there a command to refresh environment variables from the same command prompt in Windows?

From Dev

Ant build fails not expanding out environment variables

From Dev

Restrict environment variables to command when cross compiling Golang program on Windows

From Dev

Setting and getting Windows environment variables from the command prompt?

From Dev

Is there any command line tool that can be used to edit environment variables in Windows?

From Dev

Windows 10 System environment variables don't stick

From Dev

How do I set system environment variables in Windows 10?

From Dev

Are environment variables available to system-level startup scripts in WIndows?

From Dev

Using system variables without % tags in windows command prompt windows 7

From Dev

Prevent GNU make from expanding dollar signs in environment variables

From Dev

Environment variables in Windows makefiles

From Dev

Supervisord using environment variables in command

From Dev

Save command output as environment variables

From Dev

System environment variables in Jetty application

From Dev

Setting environment variables for system users

From Dev

How can I add a program path to the Windows environment variables for easy command line access?

From Dev

How do I append user-defined environment variables to the system variable PATH in Windows 7?

From Dev

How to extract environment variables from system restore point, Windows 7 64-bit

From Java

Setting Windows PowerShell environment variables

From Dev

Environment variables in windows phone 8.1

From Dev

Log access to environment variables in windows

From Dev

NPM environment variables not working in windows?

Related Related

  1. 1

    Manipulation with user and system environment variables from command line in Windows 10

  2. 2

    Expanding environment variables with sed

  3. 3

    Expanding vim variables in the set command

  4. 4

    Environment variables in meteor command line for windows

  5. 5

    Ruby system call with environment variables in windows

  6. 6

    Expanding variables in a Windows batch file

  7. 7

    Is there a command to refresh environment variables from the command prompt in Windows?

  8. 8

    Is there a command to refresh environment variables from the same command prompt in Windows?

  9. 9

    Ant build fails not expanding out environment variables

  10. 10

    Restrict environment variables to command when cross compiling Golang program on Windows

  11. 11

    Setting and getting Windows environment variables from the command prompt?

  12. 12

    Is there any command line tool that can be used to edit environment variables in Windows?

  13. 13

    Windows 10 System environment variables don't stick

  14. 14

    How do I set system environment variables in Windows 10?

  15. 15

    Are environment variables available to system-level startup scripts in WIndows?

  16. 16

    Using system variables without % tags in windows command prompt windows 7

  17. 17

    Prevent GNU make from expanding dollar signs in environment variables

  18. 18

    Environment variables in Windows makefiles

  19. 19

    Supervisord using environment variables in command

  20. 20

    Save command output as environment variables

  21. 21

    System environment variables in Jetty application

  22. 22

    Setting environment variables for system users

  23. 23

    How can I add a program path to the Windows environment variables for easy command line access?

  24. 24

    How do I append user-defined environment variables to the system variable PATH in Windows 7?

  25. 25

    How to extract environment variables from system restore point, Windows 7 64-bit

  26. 26

    Setting Windows PowerShell environment variables

  27. 27

    Environment variables in windows phone 8.1

  28. 28

    Log access to environment variables in windows

  29. 29

    NPM environment variables not working in windows?

HotTag

Archive