Published on :

Windows Temporary Directory Cleanup

This code is also available in: Español Français
Attention : This code requires administrator privileges.
This SAS© macro, named `ClearTEMPWORK`, is designed to check for the existence of a specific directory (`c:\tempwork`) on a Windows operating system. If the directory exists, it attempts to release any potential `tempwork` libname (if assigned to this path) then uses the system commands `del` and `rd` via `%sysexec` to recursively delete all files and subdirectories contained in `c:\tempwork`, as well as the directory itself. Although the code can technically be executed on SAS© Viya 4 if `%sysexec` is enabled and if Viya is deployed on Windows, it is highly dependent on the Windows environment and the use of `%sysexec` is generally restricted for security reasons in Viya production environments.
Data Analysis

Type : N/A


This macro does not directly process SAS data but interacts with the server's file system for managing a temporary directory. No SAS data sources are read or written by this macro.

1 Code Block
MACRO
Explanation :
This block defines the `ClearTEMPWORK` macro. It declares a local variable `rc`. It uses `%sysfunc(fileexist)` to check if the path `c:\tempwork` exists. If the directory exists (`&rc ne 0`), the code attempts to free a potential SAS libname named `tempwork` using `%sysfunc(libname(tempwork))`. Then, it uses `%sysexec` to execute Windows operating system commands: `del /Q "c:\tempwork\*.*"` to silently delete all files and `rd /Q "c:\tempwork"` to silently delete the empty directory. It is crucial to note that this macro is Windows-specific and that the use of `%sysexec` is often disabled or subject to strict restrictions in SAS Viya 4 environments for security and portability reasons.
Copied!
1%macro ClearTEMPWORK;
2%local rc;
3%let rc=%sysfunc(fileexist("c:\tempwork"));
4%IF &rc ne 0 %THEN %DO;
5 %let rc=%sysfunc(LIBNAME(tempwork));
6 %sysexec del /Q "c:\tempwork\*.*";
7 %sysexec rd /Q "c:\tempwork";
8%END;
9%mend cleartempwork;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.