Is it possible, using only cmd commands, to go to the last subdirectory of a directory without knowing its name? For example, the directory structure:

-----Главный каталог ---Подкаталог 1 ---Подкаталог 2

How is it possible to go to "Subdirectory 2" without using his name during the transition? Those. I always have to get into the last subdirectory of the main directory.

  • Last - by what criteria? - Qwertiy
  • Well, by the way the folders themselves are located in the system. Those. as they now lie, so must be taken. Apparently in alphabetical order. - Michael

1 answer 1

 FOR /D %%x IN (*.*) DO SET MyFolder="%%x" CD %MyFolder% SET MyFolder= 

If you need a different order than the default "alphabetically", instead of a template, you should use the output of the DIR command with the output only of directories and the required sorting:

 FOR /F "delims=*" %%x IN ('dir /A:D /B /O:N') do SET MyFolder="%%x" CD %MyFolder% SET MyFolder= 
  • In fact, no one guarantees order with such a detour, although in many cases it will coincide with the alphabetical one. - Qwertiy
  • @Qwertiy no one guarantees order with such a detour. It happens in Windows. If we are talking about the NTFS file system, the names will be returned in the order in which they appear in the index of filesystems - i.e. in alphabetic. If the file system is FATx, then the names are returned in the order of their physical location in (under) the directory. For other filesystems (including network ones), the order will be determined by the implementation of the file system access driver. - Akina
  • In alphabetical case-sensitive or not? As far as I remember, I also found deviations from the alphabetic on ntfs. And on ext generally went to random (from Windows). Well, fat is perfectly alive on flash drives. - Qwertiy 1:21 pm
  • @Qwertiy In alphabetical case-sensitive or not? Determined by encoding. If more strictly - not in alphabetical, but in lexicographical. For Windows, NTFS and Latin - case-insensitive. I and ntfs met deviations from the alphabetic Probably the result of the presence of visually similar characters. on ext in general went in random Questions to the driver. - Akina
  • Thank you for my needs. I have only a few folders there, and their name is in numbers. So there are hardly any problems here =) - Michael