Batch file to open windows explorer to last created file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#16 Post by foxidrive » 28 Dec 2013 21:10

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


Would it be workable to find the latest file, and then check if the owner was the user2?
So would user2 be creating files at the current point in time, and then run the batch file to find the most recent file?

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

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

#17 Post by gibsongk55 » 28 Dec 2013 21:17

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


Would it be workable to find the latest file, and then check if the owner was the user2?
So would user2 be creating files at the current point in time, and then run the batch file to find the most recent file?



Yes that would be workable. User2 would be creating the files same user running the batch file but there are also other users creating files in the same directory. That is the reason to check the owner of the file.

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

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

#18 Post by foxidrive » 28 Dec 2013 21:24

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


Would it be workable to find the latest file, and then check if the owner was the user2?
So would user2 be creating files at the current point in time, and then run the batch file to find the most recent file?



Yes that would be workable. User2 would be creating the files same user running the batch file but there are also other users creating files in the same directory. That is the reason to check the owner of the file.


I'm trying to get the mechanics of the procedure sorted out. It's still not possible to show a single file in explorer.

In this situation then you want to find the most recent file and see if it is owned by user2 - does that cover your aim?

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

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

#19 Post by Squashman » 28 Dec 2013 21:44

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

All three of these scenarios will do the same thing.

Code: Select all

start "" foo.txt
foo.txt
explorer foo.txt

How do you come to the conclusion that Explorer is responsible for file type associations.

File type associations are stored in HKEY CLASSES ROOT.

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

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

#20 Post by berserker » 28 Dec 2013 23:12

foxidrive wrote:I'm trying to get the mechanics of the procedure sorted out. It's still not possible to show a single file in explorer.


for the simplest case, i thought

Code: Select all

dir /O:d /T:C /q /n

and parsing the output would suffice. If TS wants to double click on the shortcut and show the file properties, a "pause" statement would have allowed the command prompt to show. Still i don't see why explorer is need. maybe i missed something.

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

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

#21 Post by gibsongk55 » 29 Dec 2013 01:21

berserker wrote:
foxidrive wrote:I'm trying to get the mechanics of the procedure sorted out. It's still not possible to show a single file in explorer.


for the simplest case, i thought

Code: Select all

dir /O:d /T:C /q /n

and parsing the output would suffice. If TS wants to double click on the shortcut and show the file properties, a "pause" statement would have allowed the command prompt to show. Still i don't see why explorer is need. maybe i missed something.


I suppose a command window would do if it could do the results I need and bring up the latest file and the owner equal to user2 and delete with a confirm ready for me to choose delete or cancel. If that is possible. Otherwise it would take too much time if I had to type extra commands in the window to delete the file. That is the only reason I chose to use the windows file explorer.

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

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

#22 Post by foxidrive » 29 Dec 2013 06:32

berserker wrote:for the simplest case, i thought

Code: Select all

dir /O:d /T:C /q /n

and parsing the output would suffice.


No. It has to descend through a folder tree and get the latest file amongst them all.

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

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

#23 Post by gibsongk55 » 29 Dec 2013 07:04

foxidrive wrote:
berserker wrote:for the simplest case, i thought

Code: Select all

dir /O:d /T:C /q /n

and parsing the output would suffice.


No. It has to descend through a folder tree and get the latest file amongst them all.



No the above code does not work for me. It does not even go to the right folder. It ends up on the my documents folder which is not in the start path.

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

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

#24 Post by foxidrive » 29 Dec 2013 07:45

penpen has written a batch file that is very quick to get the latest file.
It would also be quick to figure out if the file belongs to user2.

Is that what you need to do? Detect if the latest file belongs to user2 or not?
Or do you really need to find the latest file that user2 has edited?

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

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

#25 Post by gibsongk55 » 29 Dec 2013 08:15

foxidrive wrote:penpen has written a batch file that is very quick to get the latest file.
It would also be quick to figure out if the file belongs to user2.

Is that what you need to do? Detect if the latest file belongs to user2 or not?
Or do you really need to find the latest file that user2 has edited?


I need to find the last created file that belongs to user2. Not edited, just created. I have a start folder to search but the file could be in a subfolder so it needs to search subfolders.

Thanks

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

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

#26 Post by foxidrive » 29 Dec 2013 08:48

Ok, creation date.

Plain batch is going to be rather slow to process a lot of files.

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

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

#27 Post by gibsongk55 » 29 Dec 2013 08:58

foxidrive wrote:Ok, creation date.

Plain batch is going to be rather slow to process a lot of files.



yea ok so how is it done?

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

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

#28 Post by foxidrive » 29 Dec 2013 09:58

Try this on some sample files:

When it prompts you to delete the file, pressing Enter will bypass the deletion. Typing y and enter will delete the file.

The first three lines contain items for you to set.

Code: Select all

@echo off
 set "user=user2"
 pushd "d:\folder"

 del "%temp%\file2.tmp" 2>nul
 for /f "delims=" %%a in ('dir /s /b /a-d 2^>nul ') do call :getcreationdate "%%~fa"
 sort /r <"%temp%\file2.tmp" >"%temp%\file.tmp"
 set "file="
 for /f "tokens=1,*" %%a in ('type "%temp%\file.tmp"') do (
   dir "%%~b" /q |findstr "^[0-9]" |find /i "\%user% ">nul && set "file=%%~b"&goto :gotuser
 )
 :gotuser
 set "delfile="
 if defined file (
    echo latest file by %user% is "%file%"
    set /p "delfile=Delete %file% ? Enter=N or Y "
  ) else (
    echo No files were found by %user%
  )
 if /i "%delfile%"=="Y" del "%file%" & echo "%file%" file deleted

del "%temp%\file.tmp" "%temp%\file2.tmp"
popd
pause
goto :EOF
 :getcreationdate
 set "file=%~1"
 set "file=%file:\=\\%"
 WMIC DATAFILE WHERE name="%file%" get creationdate | find "." >"%temp%\file.tmp"
 for /f %%b in ('type "%temp%\file.tmp"') do set dt=%%b
 set dt=%dt:~0,8%_%dt:~8,6%
 del "%temp%\file.tmp"
 >>"%temp%\file2.tmp" echo %dt% "%~1"

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

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

#29 Post by berserker » 29 Dec 2013 19:29

foxidrive wrote:

Code: Select all

 for /f "delims=" %%a in ('dir /s /b /a-d 2^>nul ') do call :getcreationdate "%%~fa"
 sort /r <"%temp%\file2.tmp" >"%temp%\file.tmp"
 set "file="
...


why don't you use the switches /T and /O from dir? /T can show creation. /O can sort.

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

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

#30 Post by foxidrive » 29 Dec 2013 21:05

berserker wrote:
foxidrive wrote:

Code: Select all

 for /f "delims=" %%a in ('dir /s /b /a-d 2^>nul ') do call :getcreationdate "%%~fa"
 sort /r <"%temp%\file2.tmp" >"%temp%\file.tmp"
 set "file="
...


why don't you use the switches /T and /O from dir? /T can show creation. /O can sort.


/O will sort only within a folder. It doesn't sort a folder tree.

/T will give you an undefined format in %%~ta and the code to parse it has to be customised for every locale and %date% setting.

Post Reply