system(), renaming files and long filenames

P

Pegboy

I am trying to create a DOS utility that will extract data from a file and
use it to form a new filename for that same file. I can successfully open
the file, get the data I need and form the new name, but it truncates and
renames it in the 8.3 format. If the newly formed name is
"LongFilename.ext", the file gets renamed to "LONGFILE.EXT".

I'm currently using the following statements to rename the file:

sprintf( buf, "ren %s %s", old_name, new_name );
system( buf );

I even tried placing quotes around the new_name, but the system() function
reports an error message.

I'm working with old tools, so that might be part of the problem, but maybe
there is a simple solution to this. I would appreciate any help. Thanks.

I'm using Borland Turbo C++ 3.0 for DOS.
 
J

Jens.Toerring

Pegboy said:
I am trying to create a DOS utility that will extract data from a file and
use it to form a new filename for that same file. I can successfully open
the file, get the data I need and form the new name, but it truncates and
renames it in the 8.3 format. If the newly formed name is
"LongFilename.ext", the file gets renamed to "LONGFILE.EXT".
I'm currently using the following statements to rename the file:
sprintf( buf, "ren %s %s", old_name, new_name );
system( buf );
I even tried placing quotes around the new_name, but the system() function
reports an error message.

If this would work you would try to rename a (probably non-existent) file
named "old_name" to a file named "new_name".
I'm working with old tools, so that might be part of the problem, but maybe
there is a simple solution to this. I would appreciate any help. Thanks.

This isn't really a C question but is a problem with the operating system
you're using, so you better ask in a DOS related newsgroup like
comp.msdos.programmer.

To get this back on-topic, why don't you use system() when there's a
standard C function for the purpose, appropriately named rename()?
From K&R2:

int rename( const char *oldname, const char *newname )

rename changes the name of a file; it returns non-zero if the attempt fails.

Of course, it might fail if you try to rename a file to something that
your OS does not like...
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
M

Mike Wahler

Pegboy said:
I am trying to create a DOS utility that will extract data from a file and
use it to form a new filename for that same file. I can successfully open
the file, get the data I need and form the new name, but it truncates and
renames it in the 8.3 format. If the newly formed name is
"LongFilename.ext", the file gets renamed to "LONGFILE.EXT".

I'm currently using the following statements to rename the file:

sprintf( buf, "ren %s %s", old_name, new_name );
system( buf );

I even tried placing quotes around the new_name, but the system() function
reports an error message.

I'm working with old tools, so that might be part of the problem, but maybe
there is a simple solution to this. I would appreciate any help. Thanks.

I'm using Borland Turbo C++ 3.0 for DOS.

sprintf(buf, "ren \"%s\" \"%s\"", old_name, new_name);

Note that you can also use the standard library function
'rename()' to rename a file, without needing to call
'system()'.


-Mike
 
P

Pegboy

Thanks guys. I will check with with a DOS related group.

I am using rename()now and still limited to 8.3 but now can use lower-case
letters.
 
C

CBFalconer

Pegboy said:
I am trying to create a DOS utility that will extract data from
a file and use it to form a new filename for that same file. I
can successfully open the file, get the data I need and form the
new name, but it truncates and renames it in the 8.3 format. If
the newly formed name is "LongFilename.ext", the file gets
renamed to "LONGFILE.EXT".

The Dos filesystem has no place for more than the 8.3 name. Later
versions added a set of separate calls for the so-called LFN
extensions, but nothing of the sort existed when TC 3 was
created. So simply stick within the rules.
 
P

Paul Emmons

I don't know if it will help you, but Horst Schaeffer's very useful
batch-related program VARSET10 gives the ability to get either the
short or the long file name into an environment variable (among many
other abilities). Since this is such a tiny program obviously written
in assembler, I was curious a couple years ago as to how it
accomplished this task and fired up DEBUG to look at the code.

As I suspected, there is a DOS function call (7160h) to do this. So I
wrote a little assembler function for use with Turbo C that gives the
long file name corresponding to a short file name.

I'll e-mail you Schaeffer's program and my assembler code in case you
find them instructive.
 
P

Paul Emmons

Assuming that you are running under Windows or some version of DOS
that supports long file names in the first place....

If nothing else works, try having your C program write a little batch
file that does the renaming, and then calling that batch file with
system().

If you are looking at a whole series of files, you can even build the
batch file one line at a time and finally call system() just once to
rename all the files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top