[futurebasic] RE: [FB] Rename a file using CFURLRef

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2013 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 28 Jun 2013 09:53:06 -0400
Here's a little contribution.

Ken


compile as "Objective-C"
include "ConsoleWindow"

BeginCDeclaration
CFStringRef RenameFile( CFStringRef filePath, CFStringRef newFileName );
EndC

BeginCFunction
CFStringRef RenameFile( CFStringRef filePath, CFStringRef newFileName ) {
NSString *destPath;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString* extension = [(NSString*)filePath pathExtension];

if ( [extension length] > 0 )
{
  destPath = [NSString stringWithFormat:@"%@/%@.%@", [(NSString *)filePath stringByDeletingLastPathComponent], (NSString *)newFileName, extension];
}
else
{
  destPath = [NSString stringWithFormat:@"%@/%@", [(NSString *)filePath stringByDeletingLastPathComponent], (NSString *)newFileName];
}
[fileMgr movePath:(NSString *)filePath toPath:destPath handler:nil];
return (CFStringRef)destPath;
}
EndC
toolbox fn RenameFile( CFStringRef fileURL, CFStringRef newFileName ) = CFStringRef

dim as CFURLRef    path
dim as CFStringRef result

if ( files$( _CFURLRefOpen,,, @path ) )
result = fn RenameFile( fn CFURLCopyFileSystemPath( path, _kCFURLPOSIXPathStyle ), @"Renamed File" )
end if

fn HIViewSetText( sConsoleHITextView, result )