Published on :

File Backup to H: with Timestamp

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
This macro uses system commands (Windows specific) to copy a file. It relies on external utility macros (%AHGfiledt, %AHGmkdir, %AHGpm) to get the file date, create the destination directory structure, and debug. The destination path is calculated by modifying the original path via regular expressions.
Data Analysis

Type : EXTERNE


Direct interaction with the file system via the X 'copy' command. No SAS tables are read or created.

1 Code Block
MACRO / SYSTEM COMMAND
Explanation :
Retrieves the source file date, constructs the destination path by removing ':', creates the target directory, and executes the DOS 'copy' command to back up the file with a dated suffix.
Copied!
1%macro backuptoh(file,h=h:);
2%local dt hfile hdir;
3%AHGfiledt(&file,into=dt,dtfmt=mmddyy10.);
4 
5%let hfile=&h\%sysfunc(PRXCHANGE(s/(\\\\+)?(:)?//,-1,&file));
6%let hdir=%sysfunc(PRXCHANGE(s/(.*)\\*.*/\1/,-1,&hfile));
7 
8%AHGmkdir(&hdir);
9x "copy &file &hfile..&dt..txt /y";
10
11%AHGpm(hdir hfile dt);
12%mend;
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.