-1
@echo off
for /f %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
  set LastFileCreated=%%i
)
echo  The Last File Created is %LastFileCreated%
pause
:: ren %LastFileCreated% "decs365.pdf"
pause

all I have is this script, but unfortunately it needs to be copied in each and every sub folder, thus defeating the purpose, and it wont even rename the files. Is there any way to add a specified path "D:\Monthly Report" and have it rename the most recently created file in all of the sub folders to "decs365"

  • The answer to your question is yes there is. Run `help for` and look at various options, specifically the `for /R` variant. You'll need nested loops (yuk) or loops that call subroutines that have loops in them (always my preference). – jwdonahue Dec 21 '20 at 19:13

1 Answers1

0

I'd suggest you leverage powershell.exe, from your for this task:

@%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "Get-ChildItem -Filter '*.pdf' -File -Recurse | Sort-Object CreationTime -Descending | Select-Object -First 1 | Rename-Item -NewName 'Decs365.pdf'"

Obviously, if you're not running the command in the same working directory, as you are in your question, you'd need to add that directroy to the command above, e.g. Get-ChildItem -Path 'C:\MyDirectoryPath' -Filter …

Compo
  • 36,585
  • 5
  • 27
  • 39