3

If I have 5 or more files in a Windows Explorer folder and I rename them with a script that only changes capitalization, Explorer doesn't automatically "refresh" and show the new names in my open Explorer window. It will refresh automatically if I only rename 1 to 4 files or if I change the letters in the name to something else.

I have to manually press "F5" with the open Explorer window to see the new names. Is there a way I can make Explorer or Windows 10 refresh the directory automatically in a script so I don't have to press F5?

Here is an example batch file (rename.bat)

ren  file_important_stuff_1.txt  File_Important_Stuff_1.txt
ren  file_important_stuff_2.txt  File_Important_Stuff_2.txt
ren  file_important_stuff_3.txt  File_Important_Stuff_3.txt
ren  file_important_stuff_4.txt  File_Important_Stuff_4.txt
ren  file_important_stuff_5.txt  File_Important_Stuff_5.txt

It also fails to work with a simple Python script:

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 files:

enter image description here

4
  • Explorer is not modifiable - you will need to keep on pressing F5.
    – harrymc
    Apr 29 at 19:49
  • I'm hoping for a workaround solution--some way of telling Explorer to refresh programmatically (it can be Powershell or batch or something). I can get Explorer to refresh if I move the files to another directory, put a wait for 2 seconds in the script, and then move them back, for example. But that's not really feasible for my problem (I don't want to have a 2-second delay).
    – Paul K.
    Apr 29 at 19:54
  • @PaulK.Doesn't renaming "foo" to "bar" and (without any delay) from "bar" to "Foo" work? Apr 30 at 13:09
  • It does not unfortunately. You can take any steps in between renaming foo to Foo and it won't recognize the change. You can move foo to another folder, change to bar and then to Foo and move it back and as long as it's relatively "instant" (<2 seconds), Explorer will not see the change.
    – Paul K.
    Apr 30 at 16:15

1 Answer 1

6

A script like this could help you:

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

Breakdown:

  • $(New-Object -ComObject Shell.Application).Windows() - creates COM object for Explorer/IE and gets their active windows
  • $_.FullName -eq "C:\Windows\explorer.exe" - condition to filter only explorer.exe windows in Where-Object (?) - this can very likely be skipped by almost all Windows 10+ users
  • $_.LocationURL -match "$path$" - matches your path (that you can pass e.g. as a script param) with location URL. Some paths may need to be processed before getting matched as from what I noticed e.g. spaces are converted to %20 in that field, I couldn't quickly find what the full encoding method is though. $ at the end makes sure the URL ends with your path. You could skip this and just refresh all the Explorer windows.
  • $_.Refresh() - refreshes filtered windows in Foreach-Object (%)

This will only work with default behaviour of Explorer though from what I see (it doesn't e.g. see additional QTTabBar tabs).

5
  • 1
    +1 for not offering a SendKeys solution! :D Apr 29 at 22:54
  • 1
    Actually, with the demise of IE, you don't need to filter on explorer.exe anymore.... Apr 30 at 0:22
  • I doubt anyone uses it even if it's launchable in Win 10, so yea, the condition can likely be skipped.
    – Destroy666
    Apr 30 at 0:57
  • Yep, this worked beautifully! Exactly what I was looking for. Thank you so much. I appreciate the detailed explanation as well.
    – Paul K.
    Apr 30 at 4:07
  • @KeithMiller Unless you're some corporation that has some old internal software that works with IE but neither Egde, Firefox, or Chrome. Yes, those exist. Guess how I know. Apr 30 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .