Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wildcard name translation

Status
Not open for further replies.

noodles1

Programmer
Oct 31, 2001
58
I am looking for inspiration or a code snippet for the translating of 1 filename to another where the target filename could contain wildcard characters.

e.g file1.txt to be translated to z*.* or
file1.txt to ft1.*
file1.txt to file?t.*

Any suggestions?
 
I don't understand your question. Do you just want to rename a file? If so, it shouldn't contain wildcard characters.

Please give a little more information on what you're trying to do.
 
Yes I do wish to rename a file, however the target naming convention could contain wildcards.

Therefore, I need to translate the source name to a new name according to the target convention before calling the rename function.

e.g. source name = file1.txt, target convention = fabs?.*, result should be fabs1.txt
e.g. source name = thisfile.txt, target convention = oldthis.*, result = oldthis.txt

The difficulty, I see is that the the resulting name in any circumstance could be longer or shorter (or equal) in length to the source name.

I am looking for some form of algorithm or basis which that allows me to perform this name translation.
 
i imagine this could be simplified - but it seems to work fine:-

Code:
[b]#!/usr/bin/perl[/b]

@sources = qw( file1.txt   thisfile.txt   noNumber.xyz   thisfile123.jpg   yetAnotherFile178.abc );
@targets = qw( fabs?.*     oldthis.*      easy.backup    archived.jpeg     yaf?.*                );

for ($i=0; $i<=$#sources; $i++) {

  ($source_name, $source_num, $source_ext) = $sources["$i"] =~ m/^([^\d]+)(\d*)\.(.*)$/;
  ($target_name, $target_wcd, $target_ext) = $targets["$i"] =~ m/^([^?.]+)(\??)\.(.*)$/;
  
  $target_name .= "${source_num}.";
  
  if ($target_ext eq "*") {
    $target_name .= $source_ext;
  } else {
    $target_name .= $target_ext;
  }
  
  print "$target_name\n";

}


Kind Regards
Duncan
 
just some code yu might think about.

so sue me for the new and delete in the "C" forum.
do the translation with your malloc and free if you please.


Code:
void Shorten_File_Name (char *string, int x)
// x = the target file length.  If the extracted file name
// is large enough it will possibly exceed the target length.
// Windows filenames can get quite large and you may want to
// allow the ability to shorten that if exceeds a certain length

// input - C:\Program Files\Borland\CBuilder5\Examples\Apps\Cursors\readme.txt
// output - C:\Progr...\readme.txt
{
    int length = strlen (string);
    int a;
    int b;
   
    if (length > x)
    {
        char *buff1 = new char [60];
        char *buff2 = new char [256];

        strcpy (buff1, ExtractFileName(string).c_str ());

        a = x - strlen (buff1);

        if (a > 3) // if there is enough room.
        {
            for (b = 0; b < (a - 2); b++)
                buff2 [b] = string [b];

            buff2[b] = NULL;
        }
        else  // this just appends the file name to the drive name
              // with the "..." in the middle.
        {
            for (b = 0; b < 3; b++)
                buff2 [b] = string [b];

            buff2[b] = NULL;
        }

        strcat (buff2, "...\\");
        strcat (buff2, buff1);

        strcpy (string, buff2);
        delete buff1;
        delete buff2;
    }
}


tomcruz.net
 
Why should we have to?
Write it in C or code macro(s) to
translate new and delete for us.
 
so you want cut and paste.

no work

no sweat

no effort

life is sweet

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top