简体   繁体   中英

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?

try this

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

run a first loop to get first level subdir then run you command on each of them

@echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)

:next
echo next
pause

:filesOf 
echo "files for  ******** %1 *********"
if ("%1"=="") goto next else(
 for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
 )

it will be difficult to handle multi level subdirs tough

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM