Search code examples
windowsbatch-filefor-loopwindows-7-x64datecreated

Batch script loop through specific type of files in current directory and get the latest created and next latestcreated files and so on


I have following script to loop through specific type of files and get information about date and time of their creation:

@echo off
setlocal

call :getCreationInfo
cmd /k

:getCreationInfo
for /f "skip=5 tokens=1,2,4,5* delims= " %%a in ('dir /a:-d /o:d /t:c *.bak') do (
    if "%%~c" NEQ "bytes" (
        echo(
        @echo File Name: %%~d
        @echo Absolute Path: %%~fd
        @echo Creation Date: %%~a
        @echo Creation Time: %%~b
        echo(
    )
)

But how do I order this list in descending, like latest created file first and then the next created file and then the next and store these in an array ?

I am stuck in this ordering thing, hoping anyone to help me out here.


Solution

  • Put a hyphen in the order clause as in /o:-d instead of /o:d. This worked for me in the command prompt screen.

    Try this link for putting the results in an array: Create list or arrays in Windows Batch

    @echo off
    setlocal
    
    call :getCreationInfo
    cmd /k
    
    :getCreationInfo
    for /f "skip=5 tokens=1,2,4,5* delims= " %%a in ('dir /a:-d /o:-d /t:c *.bak') do (
        if "%%~c" NEQ "bytes" (
            echo(
            @echo File Name: %%~d
            @echo Absolute Path: %%~fd
            @echo Creation Date: %%~a
            @echo Creation Time: %%~b
            echo(
        )
    )