Batch file to open windows explorer to last created file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Batch file to open windows explorer to last created file

#1 Post by gibsongk55 » 27 Dec 2013 22:36

Hello All,

Just found this forum and was hoping someone could help me out. I need to create a batch file that will open windows file explorer to the last file created (not folder but file) and also check against the owner so that the owner = user2, then highlight the file and delete with a confirmation box to come up for the user to confirm it.

Is this possible? Appreciate any help.

Thanks,

Gibs

siberia-man
Posts: 174
Joined: 26 Dec 2013 09:28
Contact:

Re: Batch file to open windows explorer to last created file

#2 Post by siberia-man » 28 Dec 2013 00:10

Hi Gibs,

Sure, it's possible.

Code: Select all

for /f %%f in ( 'dir /a-d /o-d /b "%WINDIR%"' ) do (
    explorer /select,"%WINDIR%\%%f"
    goto 1
)
:1


Little bit explanation
dir - take the listing
/a-d - everything but directories
/o-d - sort by date/time (the newest first)
/b - show filenames only

explorer /select - open a window with the specified object (a file in your case)
goto 1 - stops execution of the loop

gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Re: Batch file to open windows explorer to last created file

#3 Post by gibsongk55 » 28 Dec 2013 01:18

Thanks for the explanation. I tried it but I need it to check subfolders and only bring up the latest file not the subfolder itself. Also any idea how to match the owner of the file? For instance I would like the owner = user2 .


Thanks again,

Gibs

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch file to open windows explorer to last created file

#4 Post by foxidrive » 28 Dec 2013 08:58

What is the scope of the search?
Are you searching through a lot of files?
Do you need it to be fast?

Squashman
Expert
Posts: 4293
Joined: 23 Dec 2011 13:59

Re: Batch file to open windows explorer to last created file

#5 Post by Squashman » 28 Dec 2013 13:12

Windows Explorer helps you navigate your folder structure. It doesnt open files. So saying you want Explorer to open the last created file does not make sense. Applications open files and there has to be a file type association for it to open automatically.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Batch file to open windows explorer to last created file

#6 Post by Yury » 28 Dec 2013 16:00

Squashman wrote:It doesnt open files. <...> Applications open files <...>.

Yes, it is true, but...


Compare results of execution of these 2 batch files:

Code: Select all

@echo off
copy nul File_with_extension.txt>nul
File_with_extension.txt
exit

Code: Select all

@echo off
copy nul File_with_extension.txt>nul
explorer File_with_extension.txt
exit

There is a difference, but small.


Compare results of execution of other 2 batch files:

Code: Select all

@echo off
copy nul File_without_extension>nul
File_without_extension
exit

Code: Select all

@echo off
copy nul File_without_extension>nul
explorer File_without_extension
exit

There is a big difference.

Thus it is the Windows Explorer is responsible for file type associations.

gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Re: Batch file to open windows explorer to last created file

#7 Post by gibsongk55 » 28 Dec 2013 18:44

foxidrive wrote:What is the scope of the search?
Are you searching through a lot of files?
Do you need it to be fast?


Searching one directory with approximately 200 subfolders and one or two files in each.

Yes it has to be fast.

gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Re: Batch file to open windows explorer to last created file

#8 Post by gibsongk55 » 28 Dec 2013 18:46

Squashman wrote:Windows Explorer helps you navigate your folder structure. It doesnt open files. So saying you want Explorer to open the last created file does not make sense. Applications open files and there has to be a file type association for it to open automatically.



I don't want to open the file. I only want to find the last file created checking all subfolders and have windows explorer bring the file up and if possible delete it leaving me with a confirmation box to delete it.

If not possible at least find the last created file and bring it up in windows explorer.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Batch file to open windows explorer to last created file

#9 Post by berserker » 28 Dec 2013 19:31

gibsongk55 wrote:
Squashman wrote:Windows Explorer helps you navigate your folder structure. It doesnt open files. So saying you want Explorer to open the last created file does not make sense. Applications open files and there has to be a file type association for it to open automatically.



I don't want to open the file. I only want to find the last file created checking all subfolders and have windows explorer bring the file up and if possible delete it leaving me with a confirmation box to delete it.

If not possible at least find the last created file and bring it up in windows explorer.


you can ask for deletion confirmation using
del /p
and why do you need to open windows explorer?

gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Re: Batch file to open windows explorer to last created file

#10 Post by gibsongk55 » 28 Dec 2013 20:09

berserker wrote:
gibsongk55 wrote:
Squashman wrote:Windows Explorer helps you navigate your folder structure. It doesnt open files. So saying you want Explorer to open the last created file does not make sense. Applications open files and there has to be a file type association for it to open automatically.



I don't want to open the file. I only want to find the last file created checking all subfolders and have windows explorer bring the file up and if possible delete it leaving me with a confirmation box to delete it.

If not possible at least find the last created file and bring it up in windows explorer.


you can ask for deletion confirmation using
del /p
and why do you need to open windows explorer?


So I can run a batch file quickly with one click and have it find the last created file and display it in windows explorer. Then I can confirm by sight if it is the one I want to delete. The purpose of the batch file would be to find the last created file within subfolders and bring it up in windows explorer. So this is what I would like to set up. Thanks.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Batch file to open windows explorer to last created file

#11 Post by berserker » 28 Dec 2013 20:17

gibsongk55 wrote:So I can run a batch file quickly with one click and have it find the last created file and display it in windows explorer. Then I can confirm by sight if it is the one I want to delete. The purpose of the batch file would be to find the last created file within subfolders and bring it up in windows explorer. So this is what I would like to set up. Thanks.


go to Start , Run, type cmd
cd to your directory where you keep the batch
run the batch which will display the last created file for you
confirm delete because the batch already coded with del /P
press enter

end of story.

what's the point windows explorer?

gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Re: Batch file to open windows explorer to last created file

#12 Post by gibsongk55 » 28 Dec 2013 20:22

berserker wrote:
gibsongk55 wrote:So I can run a batch file quickly with one click and have it find the last created file and display it in windows explorer. Then I can confirm by sight if it is the one I want to delete. The purpose of the batch file would be to find the last created file within subfolders and bring it up in windows explorer. So this is what I would like to set up. Thanks.


go to Start , Run, type cmd
cd to your directory where you keep the batch
run the batch which will display the last created file for you
confirm delete because the batch already coded with del /P
press enter

end of story.

what's the point windows explorer?


Because it is easier and faster to run the batch file from a shortcut on the desktop. Not end of story. The code will not search subfolders. So the batch file does not find the last created file. Furthermore I need the batch file to check ownership. So the owner of the file must equal "user2". I need the batch file to do this as well.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch file to open windows explorer to last created file

#13 Post by foxidrive » 28 Dec 2013 20:25

gibsongk55 wrote:So I can run a batch file quickly with one click and have it find the last created file and display it in windows explorer. Then I can confirm by sight if it is the one I want to delete. The purpose of the batch file would be to find the last created file within subfolders and bring it up in windows explorer. So this is what I would like to set up. Thanks.


The workaround I had planned was to provide a VBscript message box wth the file details and a prompt for deletion.
It may be slow as the batch file has to get the file details of every file belonging to the user, arrange them in a way that can be then sorted to get the latest file, and then show
a window with the details.

You can't get Explorer to display a file for deletion AFAIK.

gibsongk55
Posts: 13
Joined: 27 Dec 2013 22:26

Re: Batch file to open windows explorer to last created file

#14 Post by gibsongk55 » 28 Dec 2013 20:33

foxidrive wrote:
gibsongk55 wrote:So I can run a batch file quickly with one click and have it find the last created file and display it in windows explorer. Then I can confirm by sight if it is the one I want to delete. The purpose of the batch file would be to find the last created file within subfolders and bring it up in windows explorer. So this is what I would like to set up. Thanks.


The workaround I had planned was to provide a VBscript message box wth the file details and a prompt for deletion.
It may be slow as the batch file has to get the file details of every file belonging to the user, arrange them in a way that can be then sorted to get the latest file, and then show
a window with the details.

You can't get Explorer to display a file for deletion AFAIK.


Okay how about skip the deletion. Is there a way to have windows explorer display the last created file and check the owner?

Squashman
Expert
Posts: 4293
Joined: 23 Dec 2011 13:59

Re: Batch file to open windows explorer to last created file

#15 Post by Squashman » 28 Dec 2013 20:43

You can have a script open up Windows Explorer to a specific folder but you would need a batch file or vbscript to display the file owner.

Post Reply