



Windows in general does a fairly poor job of maintaining temporary files between users and scatters a lot of this clutter among many directories. In order to bring some perspective logic to all of this, I like to typically place temporary items in ‘c:temp<custom_subdir>’ by adjusting the tmp and temp path variables for both user and system profiles. Setenv.exe is a good tool to update system variables via the command line. I recommend placing this file in your system32 directory, or any directory specified in your search path.

The following scripts create separate directories at run-time for every unique user session and the system session, with the username, timestamp and machine name in the directory name itself. This can be effectively leveraged in multi-user environments / terminal servers with group policy. For Active Directory networks see here on how to configure this element; otherwise for a local deployment use gpedit.msc.

System Policy Script

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@ Echo off

FOR /F "TOKENS=1* DELIMS= " %%A IN ( 'DATE/T' ) DO SET CDATE= %%B

FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ( 'DATE/T' ) DO SET mm= %%B

FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ( 'echo %CDATE% ' ) DO SET dd= %%B

FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ( 'echo %CDATE% ' ) DO SET yyyy= %%B

SET date = %yyyy%%mm%%dd%

c:

cd \

md temp

cd temp

md system_ %computername%_temp_%yyyy%%mm%%dd%

setenv -d temp

setenv -d tmp

setenv -a temp c:\temp\system_ %computername%_temp_%yyyy%%mm%%dd%

setenv -a tmp c:\temp\system_ %computername%_temp_%yyyy%%mm%%dd%

User Policy Script

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@ Echo off

FOR /F "TOKENS=1* DELIMS= " %%A IN ( 'DATE/T' ) DO SET CDATE= %%B

FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ( 'DATE/T' ) DO SET mm= %%B

FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ( 'echo %CDATE% ' ) DO SET dd= %%B

FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ( 'echo %CDATE% ' ) DO SET yyyy= %%B

SET date = %yyyy%%mm%%dd%

c:

cd \

md temp

cd temp

md user_ %username%_temp_%yyyy%%mm%%dd%

setenv -ud temp

setenv -ud tmp

setenv -ua temp c:\temp\user_ %username%_temp_%yyyy%%mm%%dd%

setenv -ua tmp c:\temp\user_ %username%_temp_%yyyy%%mm%%dd%

On shutdown I execute the following to purge all of my user and system temp directories:

1

2

3

c:

cd \

rd "c:\temp" /s /q

The Group Policy Editor can be called by running gpedit.msc – Drill down to the following two locations to specify the previously defined scripts to be executed at startup.

Double clicking the element displays the following dialog box. You can specify the location of each script or if you click “Show Files” you can place your script files with the GPO definition directory itself. Either is fine for local policies.

Download Setenv.exe

Download Temp Batch Files

Share this: Print

More

Reddit

