TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: John Gardeniers
from: Frank Adam
date: 1996-03-12 07:24:04
subject: File name expansion

G'Day John,
 
-=> Quoting John Gardeniers to All <=-
 JG> * Crossposted from: International C Echo

 JG> I have a need for an algorithm or a bit of code which will
 JG> accept file names, including wildcards, using exactly the same rules as
 JG> the DOS copy command. The output must be a valid path/file name.
I could ask, why in god's name you don't use system(copy
), 
but i won't:)
Most file functions within C don't support wildcards, so the only way i can 
think of doing it is using findfirst to find a file, grab the name and do the 
copying then.

#include   /* before i get flamed i know it's non ISO */
#include 
#include 

#define TRUE  0
#define FALSE !TRUE

int c_copy(char* ,char* );

main(int argc,char *argv[])  /* need to start with source and dest */
{
 int result;
 char *source = argv[1];
 char *dest = argv[2];
 result = c_copy(source,dest);
 printf( result == TRUE ? "\nCopy succesful" : "\nCopy failed" );
 return 0;
}


int c_copy(char *src,char *dst)
{
 struct ffblk f;
 char sourcepath[MAXPATH];
 char sourcefile[MAXPATH];
 char *ptr;

 if(!*src || !*dst) return FALSE; /* feeble attempt at error checking 
                                     i tend to check for the first char
                                     in paths as the NULL check may miss
                                     a leading space, which is fatal. 
                                     */
 int done;                        
 
 done = findfirst(src,&f,0);                                          
 if(!done)                   /* if found one that matches */
 {
  strcpy(sourcefile,f.ff_name);  /* save the filename */
  ptr = strrchr(src,'\\');       /* find the backslash ( if any ) */
   if(ptr != NULL)               /* if there is one .. */
    {
     *++ptr = '\0';              /* end the string there */
     strcpy(sourcepath,src);     /* copy to sourcepath   */
     strcat(sourcepath,sourcefile); /* cat the file on it */
     }
    else strcpy(sourcepath,sourcefile); /* else there was no path given */

  /* ...
     do your file copy here.
     or i would just do this:
     sprintf(command,"copy %s %s /y\0",sourcepath,dst);
     system(command);
     ...
  */
  return TRUE;  /* let's assume it copied */
  }

return FALSE;  /* if we get here wildcard didn't match any files */
}

Hope it works, i was bored anyway :-)

  L8r Frank (fadam{at}ozemail.com.au). 
                                       

--- Maximus 3.00
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 78/0 620/243 623/630 632/349 635/503 544 727 711/401 409 410
SEEN-BY: 711/413 430 510 808 809 932 934 712/515 713/888 714/906 800/1
SEEN-BY: 7877/2809
@PATH: 635/544 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.