Skip to content
⌘ NSIS Forum Archive

Rename command fails although there's no lock

8 posts

Mircea M#

Rename command fails although there's no lock

Hi,

I noticed a while ago that in case I am trying to rename a folder that is currently open in Windows Explorer, the operation sometimes fails. In order to try and figure out what is happening, I created a small script that uses the LockesList plugin (to see if anything is really locked):
Name `LockedList Test`
OutFile LockedListTest.exe
RequestExecutionLevel admin

!define origFolder "C:\Program Files (x86)\Original"
!define newFolder "C:\Program Files (x86)\Original_Renamed"

Page Custom LockedListShow
Page instfiles

Function LockedListShow
LockedList::AddFolder "${origFolder}"
LockedList:😁ialog /autonext /searching "Searching for locked files in ${origFolder}"
Pop $R0
MessageBox MB_OK "$R0"
FunctionEnd

Function renameFolder
ClearErrors
MessageBox MB_OK "Renaming ${origFolder} to ${newFolder}"
Rename "${origFolder}" "${newFolder}"
IfErrors 0 done
MessageBox MB_OK "Failed to rename"
done:
ClearErrors
FunctionEnd

Section
Call renameFolder
SectionEnd
I also have the following folder structure:
\Original
\config
\test.txt

I then execute LockedList.exe in different scenarios.
  1. All Windows Explorer windows closed
  2. Windows Explorer open with folder "Original" displayed
  3. Notepad open with Test.txt
  4. Windows Explorer open with folder Original\config displayed


Here are the results:
Case 1: LockedList returns "next" as no locks found, folder is renamed
Case 2: LockedList returns "next" as no locks found, folder is renamed
Case 3: LockedList displays Notepad.exe as a lock. I close it and it then returns "next". Rename failes (as folder Original\config displayed in Windows Explorer - since I opened the txt file)
Case 4: LockedList returns "next" as no locks found, rename failes.

Now I found this a bit "weird". Rename works ok if only the folder to be renamed is displayed in Windows Explorer but fails as soon as any subfolders thereof are displayed. Also, LockedList finds no locks and manual renaming also works.

Any idea why this might happen?

Thanks,
Mircea
Anders#
I think Explorer might be using oplocks (opportunistic lock) (if they are supported on directories) and I'm not sure if LockedList supports those.

Have you tried calling AddFolder for all subfolders? (I don't think this should be necessary but maybe the plugin has a bug?)
Mircea M#
Hi,

yes, I tried that too but it doesn't change anything.
So, do you know of any way to Rename / Move such folders? My idea was to "kill" all Windows Explorer processes before performing the move, just to be sure, but this is not an elegant solution...
Mircea M#
Hi Anders,

ok, will do that but the problem isn't actually with the plugin, I believe. As far as I can tell, the plugin does find the locks when the files are actually "really" locked. Since at the time when my test installer says it can not rename the folder, by using any other methods, it is actually possible to perform the operation, I would think there's an issue with NSIS maybe.

Mircea
Anders#
Originally Posted by Mircea M View Post
Since at the time when my test installer says it can not rename the folder, by using any other methods, it is actually possible to perform the operation, I would think there's an issue with NSIS maybe.
NSIS just calls MoveFile ( https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx )
kalverson#
Years ago I added a native program to do this for me which has proven reliable ever since

// chkfolder.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

#define MAXPATH 1024

int result=0; // 0 = good - folder renames worked, not zero = bad - folder rename failed
wchar_t buf[MAXPATH]; // maxpath

int _tmain(int argc, _TCHAR* argv[])
{

// argv[1] = product folder

if (wcsnlen(argv[1], MAXPATH) > 0) {

HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);

result = _wrename(argv[1], argv[2]);

} else {
wprintf(L"product folder name must be at least one character in length.");
}

return result;
}
Anders#
Originally Posted by kalverson View Post
Years ago I added a native program to do this for me which has proven reliable ever since
_rename just calls MoveFile like NSIS.

Also, why would you use a console program if you just try to hide the console window anyway, it is going to flash up on the screen for a second.