You're here: Snippet Directory » C/C++ (495)
Language:

Makes a directory which name containing current time

Language: English
Programming Language: C
Published by: sugaray [not registered]
Last Update: 5/15/2006
Views: 133


Description

just as the title saysand hoping someone could enhance it.

Code

1 //////////////////////////////////////////////////////////////////////////// 2 // MKTDIR.C 3 // 4 // Copyright (C) 2002 Sugar Ray 5 // Version 0.1 - Initial revision by Sugar Ray 6 // Version 0.2 - Rewritten to support variable substitutions (Malcolm Nixon) 7 // 8 // The original version of this program took the first argument to the 9 // program and added the date and time to it. This version substitutes 10 // portions of the argument with system parameters such as date/time/ 11 // computer-name/user-name into the string. 12 // 13 // This program was written and compiled under Microsoft Visual C++ 6.0 14 // You can compile it with other possible compilers (such as Borland C/C++, 15 // GNU C Compiler) with few minor changes to the source. 16 // 17 // This program is free software; you can redistribute it and/or modify 18 // it under the terms of the GNU General Public License as published by 19 // the Free Software Foundation; either version 2 of the License or 20 // (at your option) any later version. 21 //////////////////////////////////////////////////////////////////////////// 22 23 24 #define WIN32_LEAN_AND_MEAN 25 #include <windows.h> // GetComputerName & GetUserName. 26 #include <time.h> // _strdate & _strtime 27 #include <direct.h> // _mkdir 28 #include <stdio.h> // printf 29 30 31 // Takes a character and replaces any 'dubious' characters with the safe 32 // underbar character. 33 char legal_dir_char(char ch) { 34 // Check the character. 35 if (ch == ':' || ch == '\\' || ch == '/') { 36 // Return underbar. 37 return '_'; 38 } 39 return ch; 40 } 41 42 void safe_cat(char in, char **out, int *outlen) { 43 // Make sure there's room. 44 if (in && *outlen) { 45 // Add the character. 46 *(*out)++ = legal_dir_char(in); 47 } 48 } 49 50 void safe_cat_str(char *in, char **out, int *outlen) { 51 // Loop until end of string, or no more room. 52 while (*in && *outlen) { 53 // Copy the character. 54 *(*out)++ = legal_dir_char(*in++); 55 // Decrement the room. 56 *outlen--; 57 } 58 } 59 60 void expand(char *in, char *out, int outlen) { 61 char tmp[256]; 62 DWORD tmp_len; 63 64 while (*in) { 65 // Check for an escape character. 66 if (*in == '%') { 67 // Found an escape character, skip it and test the escape. 68 in++; 69 switch (*in) { 70 case 'd': 71 // Get the date and add it to the output. 72 _strdate(tmp); 73 safe_cat_str(tmp, &out, &outlen); 74 break; 75 76 case 't': 77 // Get the time and add it to the output. 78 _strtime(tmp); 79 safe_cat_str(tmp, &out, &outlen); 80 break; 81 82 case 'c': 83 // Get the computer name and add it to the output. 84 tmp_len = sizeof(tmp); 85 GetComputerName(tmp, &tmp_len); 86 safe_cat_str(tmp, &out, &outlen); 87 break; 88 89 case 'u': 90 // Get the user name and add it to the output. 91 tmp_len = sizeof(tmp); 92 GetUserName(tmp, &tmp_len); 93 safe_cat_str(tmp, &out, &outlen); 94 break; 95 96 default: 97 // Unknown character, just append it. 98 safe_cat(*in, &out, &outlen); 99 break; 100 } 101 102 in++; 103 } else { 104 // Just copy the character 105 safe_cat(*in++, &out, &outlen); 106 } 107 } 108 109 // Null terminate the output string. 110 *out = 0; 111 } 112 113 // Program entry point. 114 int main(int argc, char **argv) { 115 char buf[256]; 116 117 // Check for the correct number of arguments. 118 if (argc != 2) { 119 printf("MKTDIR <directory name>\n"); 120 printf(" Directory name can contain the following for expansion:\n"); 121 printf(" %d - Date 'mm_dd_yyyy'\n"); 122 printf(" %t - Time 'hh_mm_ss'\n"); 123 printf(" %c - Computer name\n"); 124 printf(" %u - User name\n"); 125 printf(" %% - '%'\n"); 126 return 1; 127 } 128 129 // Expand the directory name. 130 expand(argv[1], buf, 255); 131 132 // Create the directory. 133 if (_mkdir(buf) != 0) { 134 printf("Unable to create directory %s\n", buf); 135 return 1; 136 } 137 138 return 0; 139 } 140 141 // End of file. 142

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS