Renaming files with Python doesn't show in Windows Explorer without refreshing (only changing capitalization)

108 Views Asked by At

I have a Python script to change the capitalization of multiple files in a directory. The problem is that I run my script but Windows Explorer doesn't show the new names unless I hit "refresh" with F5. I know Windows Explorer is essentially case insensitive so I think it doesn't recognize that there were changes to the directory if the only changes were to the capitalization.

For what it's worth, I run the script with that directory open in Windows Explorer so I expect to see the change in that opened window.

In the script, I have tried renaming the files to something intermediate (like file_important_stuff.txt to test.txt and then to the desired File_Important_Stuff.txt) but it seems that Windows 10 just compares the "before" and "after" names in the directory to decide whether to "refresh" or not. Same with moving the files to a different directory and then back within the script.

I can force the desired behavior if I use open() to open each renamed file for writing in Python but then the date modified changes, which I don't want.

Edit: Some new learning. If you rename one to four files, Windows Explorer will update. If you update 5 or more files, it doesn't refresh the window. Here is some same code:

import os

for x in range(1,6):
    fileNameOld = "file_important_stuff_" + str(x) + ".txt"
    fileNameNew = "File_Important_Stuff_" + str(x) + ".txt"
    os.rename(fileNameOld,fileNameNew)

Here's a picture of the directory with the files.

2

There are 2 best solutions below

1
Vishnu Balaji On

import os module and add os.system('gpupdate/force') to force a system refresh

0
Paul K. On

Got help on the SuperUser site:

https://superuser.com/questions/1781550/windows-explorer-doesnt-show-updated-names-when-changing-capitalization-multip/1781580#1781580

This is the Powershell script that worked for me:

$windows = $(New-Object -ComObject Shell.Application).Windows()
$windows  | ? { $_.FullName -eq "C:\Windows\explorer.exe" -and $_.LocationURL -match "$path$" } | % { $_.Refresh() }