2

In Windows 11, if I open file explorer to c:\temp\, is there a programmatic way to identify that c:\temp is open in file explorer?

I want to execute this batch:

xcopy "c:\templates\*.*" to %destination%

Whereby %destination% is defined by the currently opened folder in file explorer.

2
  • 2
    well, since you can have multiple explorer windows open, and there are now explorer tabs as well, no probably not. you will need some additional constraints at a minimum Commented Aug 15, 2025 at 21:49
  • I mean... Open the folder in Explorer also programatically and save it to variable? I don't know what use case you have in mind where it'd need to be done manually through GUI. Commented Aug 15, 2025 at 22:52

2 Answers 2

6

Your question is a little vague, I am assuming you are looking for a batch script or simple solution that doesn't require actual programming.

Use the "Open in Terminal" context menu item to open the command prompt in your current folder. The %CD% variable will contain the directory.

You can then create a batch script "C:\Windows\copytemplate.cmd" (it doesn't have to be C:\Windows, just any folder accessible to %PATH%) with the following code

xcopy C:\template\*.* "%CD%" /E /I /Y

Now inside the command prompt you opened, you can type copytemplate.cmd and it should execute the command.

You could also create a context menu item for your command, I don't know how to do it for Windows 11 and the new explorer UI, but you can use the older method and use Shift + Right Click to view the legacy context menu.

modifying the registry can cause issues if done wrong so proceed with caution

The following is a .reg file used to modify the registry adding a "Copy Template Here"

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\CopyTemplate]
@="Copy Template Here"

[HKEY_CLASSES_ROOT\Directory\Background\shell\CopyTemplate\command]
@="cmd.exe /c xcopy \"C:\\templates\\*\" \"%V\" /E /I /Y"

example of menu item

1
  • Thank you NickSlash! I apologize for the lack of detail. On another forum I got into a tug-of-war between moderators demanding less and moderators demanding more detail. I will investigate your suggestion to create a custom context menu item. Believe it or not, your suggestion will save me daily time from navigating file explorer. Daily I copy template files and paste to a particular drive location delineated by event description. Thanks again. Commented Aug 16, 2025 at 13:27
2

I'm assuming you need a portable solution than can run on Windows 11 without installing additional programs. If you need to run it only on one PC/laptop and you have admin rights, then creating a custom context menu (from NickSlash's answer) is easier.

The easiest way to get the folder is to use PowerShell which is built-in into Windows since Windows 7. Here's an example and another one which gets path from the topmost Explorer window.

However, it might be better to ask the user where they want to save files using a graphical dialog. PowerShell can do it.

1
  • @MfgEng, if you need to copy files only on one PC/laptop and you have admin rights, then creating a custom context menu (from NickSlash's answer) is easier. My answer is more suitable if you need to do it on different PCs where you might not have admin rights. I'll update my answer to note this. Commented Aug 16, 2025 at 13:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.