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