0

I'm trying to figure out on how to get the most recent file that has a specific text in the file name, like it containing 'fstp'. I than need to be able to rename that file.
I haven't used batch much but I think I have the later part figured out.
The problem is getting the most recent file with certain characters in it.
Any thoughts? Thanks

for /f %%i in ('dir /b/a-d/od/t:c C:\theFolder') do set LAST=%%i
echo The most recently created file is %LAST%
pause
aschipfl
  • 31,767
  • 12
  • 51
  • 89
Brandon Reale
  • 17
  • 1
  • 10
  • 2
    `dir /b/a-d/od/t:c "C:\the Folder\*fstp*"` – Stephan Jun 19 '20 at 19:45
  • @Stephan Oops, I misread `/a-d` as `/a:d` – Nico Nekoru Jun 19 '20 at 20:50
  • let's clean up then ... – Stephan Jun 19 '20 at 20:53
  • That can be done with one command line: `@for /F "eol=| delims=" %%I in ('dir "C:\theFolder\*fstp*" /A-D /B /O-D 2^>nul') do @ren "C:\theFolder\%%I" "newname.txt" & goto :EOF`. __DIR__ outputs with `/O-D` the file name of the file with newest last modification date (or with `/TC` newest creation date) first. This file is renamed and then the batch file processing is exited with `goto :EOF`. There can be used also `goto RenameDone` with `:RenameDone` below this line to just exit the loop after newest file is renamed to whatever you want. – Mofi Jun 20 '20 at 11:51
  • Hint: The command `move /Y "C:\theFolder\%%I" "C:\theFolder\newname.txt"` could be also used to rename a file in same directory which has the advantage that this works even with a file `"C:\theFolder\newname.txt"` already existing which is deleted first, before newest file is renamed by using the command __MOVE__. As source and destination directory are on same drive (even in same directory), the command __MOVE__ makes just an update in file system without really moving data on storage media. So this is very fast even for a huge file. – Mofi Jun 20 '20 at 11:54

1 Answers1

0

You can use powershell for this like

powershell -command "(dir <path> | sort 'LastWriteTime' -Descending | select -first 1).name"

And to set it to a variable:

for /f "tokens=*" %%i in ('powershell -command "(dir <path> | sort 'LastWriteTime' -Descending | select -first 1).name"') do set var=%%i

And of a specific name (like your question asks)

powershell -command "(dir '<path>' | sort 'LastWriteTime' -Descending | ? {$_.name -match 'name'}| select -first 1).name"

and to set it to a variable

for /f "tokens=*" %%i in ('powershell -command "(dir '<path>' | sort 'LastWriteTime' -Descending | ? {$_.name -match 'name'}| select -first 1).name"') do set var=%%i

This will get the most recently edited file/folder with name in them (i.g. 123name, 1name2, name123 etc.).

For "true-batch" solution, you could do this:

dir "<path>" /o:d /t:w /b

To set it to a variable:

for /f "tokens=*" %%i in ('dir "<path>" /o:d /t:w /b') do set var=%%i

To find files in the directory with name in them:

dir "<path>" /o:d /t:w /b | findstr /i "name"

To set it to a variable:

for /f "tokens=*" %%i in ('dir "<path>" /o:d /t:w /b | findstr /i "name"') do set var=%%i

and with the variable (applies to all examples which defined a variable) you can rename the file like so:

ren %var% "newname"

Testing

My most recently edited folder in my user folder is Desktop

06/18/2020  01:15 PM    <DIR>          .
06/18/2020  01:15 PM    <DIR>          ..
04/15/2020  01:09 PM    <DIR>          .android
06/12/2020  03:23 PM    <DIR>          .atom
06/02/2020  04:44 PM    <DIR>          .config
06/06/2020  12:57 PM    <DIR>          .dotnet
03/14/2020  08:51 PM                16 .emulator_console_auth_token
06/03/2020  08:45 AM               275 .gitconfig
06/06/2020  12:51 PM    <DIR>          .omnisharp
05/26/2020  09:44 AM    <DIR>          .tooling
04/17/2020  12:31 PM    <DIR>          .VirtualBox
05/26/2020  09:27 AM    <DIR>          .vs
05/26/2020  09:43 AM    <DIR>          .vscode
06/12/2020  08:16 PM    <DIR>          3D Objects
06/12/2020  08:16 PM    <DIR>          Contacts
06/18/2020  10:54 PM    <DIR>          Desktop
06/12/2020  08:16 PM    <DIR>          Documents
06/18/2020  05:34 PM    <DIR>          Downloads
06/12/2020  08:16 PM    <DIR>          Favorites
06/12/2020  08:16 PM    <DIR>          Links
06/12/2020  08:16 PM    <DIR>          Music
04/27/2020  08:59 PM                 0 null
03/16/2020  01:50 PM    <DIR>          OneDrive
05/23/2020  01:55 PM    <DIR>          OpenVPN
06/12/2020  08:16 PM    <DIR>          Pictures
06/12/2020  08:16 PM    <DIR>          Saved Games
06/12/2020  08:16 PM    <DIR>          Searches
05/15/2020  05:10 PM    <DIR>          source
06/12/2020  08:16 PM    <DIR>          Videos
04/17/2020  12:19 PM    <DIR>          VirtualBox VMs

note I am running these form command line

First example:

powershell -command "(dir <path> | sort 'LastWriteTime' -Descending | select -first 1).name"
Desktop

Second:

for /f "tokens=*" %i in ('powershell -command "(dir | sort 'LastWriteTime' -Descending | select -first 1).name"') do set var=%i
set var=Desktop

Thrid:

powershell -command "(dir | sort 'LastWriteTime' -Descending | ? {$_.name -match 'D'}| select -first 1).name"
Desktop

Fourth:

for /f "tokens=*" %i in ('powershell -command "(dir | sort 'LastWriteTime' -Descending | ? {$_.name -match 'D'}| select -first 1).name"') do set var=%i
set var=Desktop

True-batch solutions:

dir /o:d /t:w /b
.emulator_console_auth_token
OneDrive
.android
VirtualBox VMs
.VirtualBox
null
source
OpenVPN
.vs
.vscode
.tooling
.config
.gitconfig
.omnisharp
.dotnet
.atom
3D Objects
Contacts
Pictures
Favorites
Videos
Searches
Music
Documents
Saved Games
Links
Downloads
Desktop

Second:

for /f "tokens=*" %i in ('dir /o:d /t:w /b') do set var=%i
set var=.emulator_console_auth_token
set var=OneDrive
set var=.android
set var=VirtualBox VMs
set var=.VirtualBox
set var=null
set var=source
set var=OpenVPN
set var=.vs
set var=.vscode
set var=.tooling
set var=.config
set var=.gitconfig
set var=.omnisharp
set var=.dotnet
set var=.atom
set var=3D Objects
set var=Contacts
set var=Pictures
set var=Favorites
set var=Videos
set var=Searches
set var=Music
set var=Documents
set var=Saved Games
set var=Links
set var=Downloads
set var=Desktop

Which would end up setting %var% to Desktop

Third:

dir /o:d /t:w /b | findstr /i "D"
OneDrive
.android
.vscode
.dotnet
3D Objects
Videos
Documents
Saved Games
Downloads
Desktop

Fourth:

for /f "tokens=*" %i in ('dir "<path>" /o:d /t:w /b | findstr /i "name"') do set var=%i
set var=OneDrive
set var=.android
set var=.vscode
set var=.dotnet
set var=3D Objects
set var=Videos
set var=Documents
set var=Saved Games
set var=Downloads
set var=Desktop

If you only want directories you can do this:

True-Batch

dir "<path>" /a:d /o:d /t:w /b | findstr /i "name"
REM Set to Variable
for /f "tokens=*" %%i in ('dir "<path>" /a:d /o:d /t:w /b | findstr /i "name"') do set var=%%i

Part Powershell

powershell -command "(dir '<path>' -directory | sort 'LastWriteTime' -Descending | ? {$_.name -match 'name'}| select -first 1).name
REM Set to variable
for /f "tokens=*" %%i in ('powershell -command "(dir '<path>' -directory | sort 'LastWriteTime' -Descending | ? {$_.name -match 'name'}| select -first 1).name"') do set var=%%i

If you only want files:

True-Batch

dir "<path>" /a:-d /o:d /t:w /b | findstr /i "name"
REM Set to Variable
for /f "tokens=*" %%i in ('dir "<path>" /a:-d /o:d /t:w /b | findstr /i "name"') do set var=%%i

Part Powershell

powershell -command "(dir '<path>' -file | sort 'LastWriteTime' -Descending | ? {$_.name -match 'name'}| select -first 1).name
REM Set to variable
for /f "tokens=*" %%i in ('powershell -command "(dir '<path>' -file | sort 'LastWriteTime' -Descending | ? {$_.name -match 'name'}| select -first 1).name"') do set var=%%i
Nico Nekoru
  • 2,402
  • 1
  • 15
  • 32