Search code examples
batch-filereversedirinverse

Get inverse list of files through dir command in batch


I'm triying to get a list of files of subfolders in a inverse order using this:

for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)

I'm getting this result folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1

And I want to get the reverse order in folders and files, not only files of folders. Something like this folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1

How can I do this?


Solution

  • @echo off
    for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
    C:\Windows\System32\sort.exe /R tempfile.123
    del tempfile.123
    

    This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html