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!

Rename File 2

Status
Not open for further replies.

xBaseDude

MIS
Jan 31, 2002
357
0
0
US
I'm looking for an example of using the Win API to rename a file. I know the current file name and path, and want to keep the file in the same directory but execute the REN command on it.Any issues with "long file names" iow "My data 1stQtr.dbf" Do the spaces cause any issues?

TIA - Wayne

 

I would suggest you use a combination of CopyFile():

Code:
DECLARE INTEGER CopyFile IN kernel32;
	STRING  lpExistingFileName,;
	STRING  lpNewFileName,;
	INTEGER bFailIfExists

And Deletefile()

Code:
DECLARE INTEGER DeleteFile IN kernel32;
	STRING lpFileName


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
You could use:

Code:
DECLARE INTEGER MoveFile IN Win32API STRING SOURCE, STRING destination
IF MOVEFILE("c:\myfolder\myfile.dbf","c:\myfolder\newname.dbf") = 1
  && success
ELSE
  && Failure
ENDIF

Regards

Griff
Keep [Smile]ing
 
Hi Wayne,

There is also the native FoxPro RENAME command.

Jim
 
Dooh,

I forgot that one! You would be best using character substition/variables for the new and old filenames...

Code:
m.OldFile = "c:\a long folder name\old file name.dbf"
m.NewFile = "c:\a long folder name\New file name.dbf"

rename (m.OldFile) to (m.NewFile)

Regards

Griff
Keep [Smile]ing
 
You would pobably want to check:

1) The old file exists
2) The new file doesn't exist initially
3) The new file does exist afterwards

If you are using File() to do this, be careful the files aren't in your current path - or the results can be misleading! I use a UDF called MyFile for this to make sure:

Code:
FUNCTION MYFILE
	PARAMETER m.FILENAME
	PRIVATE m.FILENAME,m.FLG
	M.FLG = .F.
	IF !EMPTY(m.FILENAME)
		IF ADIR(TMPDIRFILES,m.FILENAME) > 0
			M.FLG = .T.
		ENDIF
	ENDIF
	RETURN(m.FLG)

Regards

Griff
Keep [Smile]ing
 
Thank-You very much for all your suggestions. Not by my machine right now, but your kindness has shown me many different ways.

Take care!
Wayne

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top