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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing File extensions

Status
Not open for further replies.

riggd

Technical User
Apr 8, 2001
3
0
0
AU
My code below is supposed to change the extension of a group of text files *.txt to *.bak but when I run it I get the message:

Cannot rename file: The filename, directory name, or volume label is incorrect.

What am I doing wrong??

void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *File1="C:\\temp\\test\\*.TXT\0";
char *File2="C:\\temp\\test\\*.BAK\0";
SHFILEOPSTRUCT op;
ZeroMemory(&op, sizeof(op));
op.hwnd = Handle;
op.wFunc = FO_RENAME;
op.pFrom = File1;
op.pTo = File2;
op.fFlags=FOF_MULTIDESTFILES || FOF_FILESONLY;
SHFileOperation( &op);
Label1->Caption="Done";
}
 
I took just a quick look at your program and the one thing that sticks out is
Code:
char *File1="C:\\temp\\test\\*.TXT\0";
char *File2="C:\\temp\\test\\*.BAK\0";

Try
Code:
char *File1="C:\\temp\\test\\*.TXT";
char *File2="C:\\temp\\test\\*.BAK";




James P. Cottingham
-----------------------------------------
[sup]To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.[/sup]
 
I don't know what's happening there, but you could simply use the VCL's RenameFile function instead, and you might also have to remove the directory specifier from File2, just have "*.bak". You also don't need those '\0' characters in your strings.
 
Sorry, my mistake, RenameFile only renames a single file. However, you could use instead:
[tt]
#include <stdlib.h>
system(("ren "+File1+" "+File2).c_str());
[/tt]

Alternatively, you could iterate through the files using FindFirst/FindNext and rename each individually.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top