the number n can be given as the second command line argument if omitted, the user is prompted for it (this addresses elzooilogico's comment) n defaults to 1 for empty input.the target directory can be provided as the first command line argument if omitted, the current directory is used.Exclamation marks ! in folder names are no longer lost due to toggling delayed expansion.Here is a modified script with the following improvements: If defined ITEM echo %LATEST%th-latest folder: %ITEM% Rem // Define N here to get Nth-latest folder:įor /F "eol=| delims=" %%# in ('dir /B /A:D /O:-D "C:\somewhere"') do ( Then let an index number count the iterations of the loop, and if that index equals the predefined number n, store the currently iterated item: off However, you could reverse the sort order of the output of the dir command by changing the /O option, so it returns the latest (most recent) item first. Retrieving the nth folder is simply not possible in a flexible way with that script as you would need to define another variable (say bl3, bl4., bl n) within the loop. In your approach, the latest folder is already available in variable bl1 add echo %bl1% at the end before endlocal to display it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |