-2

What is the equivalent of linux: ls | tail -1 command in windows command prompt?
Assuming dir prints 3 folders:

2019-08-13_120000  
2019-08-13_120001  
2019-08-13_120002  

I want to penetrate the last directory, here it is: cd 2019-08-13_120002

phuclv
  • 37,963
  • 15
  • 156
  • 475
stakowerflol
  • 979
  • 1
  • 11
  • 20
  • 2
    You are member long enaugh to know that the question is off topic. To get the last dir in the current folder into a variable `for /f %A in ('dir /B /ON /AD') Do @Set "LastDir=%A"` –  Aug 13 '19 at 09:47
  • Possible duplicate of [Using batch file I need to know the latest folder name in the particular location](https://stackoverflow.com/questions/6367747/using-batch-file-i-need-to-know-the-latest-folder-name-in-the-particular-locatio) – phuclv Aug 13 '19 at 13:46
  • other related/duplicates:: [Get last created directory batch command](https://stackoverflow.com/q/10519389/995714), [Windows batch file: get last folder name from path](https://stackoverflow.com/q/29327598/995714), [How do I find the most recently created file in a directory from a batch file?](https://devblogs.microsoft.com/oldnewthing/20120801-00/?p=6993), [Windows batch file scripting: how to get directory named with latest date](https://serverfault.com/q/962107/343888) They're all the same, just changing the sorting condition – phuclv Aug 13 '19 at 13:48

2 Answers2

2

From cmd you can run

powershell -com "ls | select -Last 1"

Where select is an alias of Select-Object and ls is an alias of Get-ChildItem. If you want to get just the string then use powershell -com "(ls | select -Last 1).Name powershell -com "(ls | select -Last 1).FullName

But switch completely to powershell if possible. It'll solve a lot of headaches in cmd

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • how set the result to varaible? – stakowerflol Aug 13 '19 at 12:01
  • @stakowerflol please do some research. There are already tons of questions on that: [How to set commands output as a variable in a batch file](https://stackoverflow.com/q/6359820/995714), [Assign output of a program to a variable using a MS batch file](https://stackoverflow.com/q/2323292/995714)... In fact LotPings already gave you the answer above: `for /f` – phuclv Aug 13 '19 at 13:44
0

There isn't a real tail feature in windows, but if you just want the last directory you can select it very quickly using this trick at the cli:

SET "_Folder="
FOR /F "Tokens=*" %A in ('
  DIR /AD /O-N /B
  "C:\path\to\folders\*"
') Do @(
  IF Defined _Folder (
     Dir >&0 
  )  ELSE (
    Set "_Folder=%A"
  )
)
CD "%_Folder%"

Note, tou don't have to do the CD outside the loop, you can do it inside the else condition instead and forgo setting a variable.

Further the IF logical construct isn't nessessary if only matching the first or last directory, but I left it above because I think it may look cleaner as an example when parsing the logic.

Below takes both of these changes into account

FOR /F "Tokens=*" %A in ('
  DIR /AD /O-N /B
  "C:\path\to\folders\*"
') Do @(
  CD "%A"
  Dir >&0
)
Ben Personick
  • 3,074
  • 1
  • 22
  • 29