Reply to comment

Copy, Move, Rename File Preserving Destination Name Case

The CopyFile() and MoveFile() WIN API functions preserve the case of the name for the destination file. Alternatively, WSH can be used.
The source and destination file names should include the directory name in both cases.

Note 1 A Copy operation will not change file name case when destination file already exists (overwritten).

Note 2 The WinApiErrMsg is used to retrieve Windows API error message in case of error.

This is sample code. Add error handling and adjust to your requirements as necessary.

lcFileName = <...>
lcCopyFileName = <...>
 
IF CopyFile(lcFileName, lcCopyFileName, 0) = 0
  ? "Copy unsuccessful:" + WinApiErrMsg(GetLastError() ) 
ENDIF
 
lcFileName2 = <...>
lcNewFileName =  <...>
 
* Move/Rename file
IF MoveFile(lcFileName2, lcNewFileName) = 0
  ? "Move/Rename unsuccessful"
ENDIF
 
RETURN
 
FUNCTION CopyFile(tcFileName, tcCopyFileName, tnFailIfExists)
DECLARE Long CopyFile IN WIN32API String SourceFileName, String DestFileName, Long bFailIfExists
RETURN CopyFile(tcFileName, tcCopyFileName, tnFailIfExists)
 
FUNCTION MoveFile(tcFileName, tcNewFileName) 
DECLARE Long MoveFile IN WIN32API String SourceFileName, String DestFileName
RETURN MoveFile(tcFileName, tcNewFileName)
* WSH
* Copy file
* The file name case will not be changed for existing destination file.
oFSO = CreateObject("Scripting.FileSystemObject")
llOverWrite = .T.
TRY 
	oFSO.CopyFile(lcFileName, lcCopyFileName, llOverWrite)
CATCH 
	? "Copy unsuccessful"
ENDTRY
* Move/Rename file
oFSO = CreateObject("Scripting.FileSystemObject")
TRY 
	oFSO.MoveFile(lcFileName, lcNewFileName )
CATCH 
	? "Move/Rename unsuccessful"
ENDTRY

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.