Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically rename open file on Windows

I am porting a Unix C application to Windows. This application renames files while they are open, which is perfectly fine on Unix but apparently it does not work on Windows. Tracing all the renames to make sure I close the file, then reopen and seek again would be painful.

Given that Windows Explorer allows to rename a file while is in use, I wonder why I cannot get this to work. I have tried with rename and MoveFile in C, and System.IO.File.Move in C#. It fails in all cases with a "Permission denied" error (specifically, the error returned by GetLastError() is "The process cannot access the file because it is being used by another process")

Tips?

I have also tried to open the file for sharing with _sopen. It didn't work either (same error).

Working C# code thanks to Stefan:

string orig_filename = "testrenamesharp-123456";
string dest_filename = "fancynewname.txt";
Byte[] info = new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");
var fs = new FileStream(orig_filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete);
fs.Write(info, 0, info.Length);
File.Move(orig_filename, dest_filename);
fs.Close();

Working C sample:

const char* filename = "testrename-XXXXXX";
const char* dest_filename = "fancynewname.txt";

/* The normal POSIX C functions lock the file */
/* int fd = open(filename, O_RDWR | O_CREAT, _S_IREAD | _S_IWRITE); */ /* Fails */
/* int fd = _sopen(filename, O_RDWR | O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); */ /* Also fails */

/* We need to use WINAPI + _open_osfhandle to be able to use 
   file descriptors (instead of WINAPI handles) */
HANDLE hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL );
if( INVALID_HANDLE_VALUE == hFile) {
    ErrorExit(TEXT("CreateFile"));
}

int fd = _open_osfhandle((long int)hFile, _O_CREAT | _O_RDWR | _O_TEMPORARY);
if( -1 == fd ) {
    perror("open");
}

int resw = write(fd, buf, strlen(buf));
if(-1 == resw) {
    perror("write");
}

if( 0 == access(dest_filename, F_OK)) {
    perror("access");
}

/* Now try to rename it - On Windows, this fails */
int resr = rename(filename, dest_filename);
if( -1 == resr) {
    perror("rename");
}

int resc = close(fd);
if( -1 == resc ) {
    perror("close");
}
like image 539
pgquiles Avatar asked Aug 22 '11 12:08

pgquiles


People also ask

How do you rename a file that's open?

Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.

How do I automatically rename a file in Windows?

To batch rename files, just select all the files you want to rename, press F2 (alternatively, right-click and select rename), then enter the name you want on the first file. Press Enter to change the names for all other selected files.


1 Answers

Renaming requires that the file in question was opened with FileShare.Delete sharing. If that share flag is missing, you can not rename/move the file while it is still open.

like image 56
Stefan Avatar answered Sep 19 '22 18:09

Stefan