I've done some digging and can't seem to find a simple answer anywhere, maybe I'm looking in the wrong places. I simply want to sort files by their modification time, oldest to newest. Is this possible?
for %%i in (Input/*.json) do (
)
I've done some digging and can't seem to find a simple answer anywhere, maybe I'm looking in the wrong places. I simply want to sort files by their modification time, oldest to newest. Is this possible?
for %%i in (Input/*.json) do (
)
If you only want them sorted then DIR
is going to be the fastest Route, you can parse that output using a FOR /F
Loop.
This Code will give you Ascending Date:
FOR /F "tokens=*" %%A IN ('
DIR /B
/O:D
/A:-D
Input\*.json
') DO (
ECHO(Found: "%%~A"
)
The /O
option changes the order. /O:D
or /OD
Orders Date Ascending (Oldest First) /O:-D
or /O-D
Orders Date Descending (Newest First)
The /A
option changes the type of items selected. /A:-D
or /A-D
Selects File Items, /A:D
or /AD
Selects Directory items.
The /B
option Outputs Only File Names unless the /S
switch is used along with it to recurse the directory tree, in which case it will output file paths.
The only issue with this (without /S
)is:
As you are looking into a subdirectory, instead of the same directory, you can not pull any of the other FOR variable info from the output.
IE: You can not use say %%~tA
to print the Date/Time, and %%~fA
Will give the wrong file path.
If those are required to be returned you will have to find the information separately or scrape info needed from the DIR cmd, or use the /S
option which recurses sub directories.
Thanks @Michel de Ruiter for noting that, and drawing my attention ack to update this a bit , and now I will expand on your expanded bit.
If you would like to get the path or timestamp etc. from the for loop, but you don't want to waste time or get results form sub directories you can amend to the following version.
this uses Findstr
to remove any subdirectories below the initial search directory using a basic regular expression.
FOR /F "tokens=*" %%A IN ('
DIR /B
/O:D
/A:-D
Input\*.json
^| findstr /i /v "\\input\\.*\\.*"
') DO (
ECHO(Found: "%%~A"
)
in this usage the findstr
command is using these switches to match a regular expression which matches any sub directories where files might be matched (directoties without files are skipped due otthe options on the dir
cmd as explained above)
The /i
option means to match case-insensitive
The /v
option means to exclude results that do match the pattern
The pattern appears within the double quotes "\\input\\.*\\.*"
here this is a basic regex where the period .
matches anything and the astrix *
says to mathc the previou character any tmber of times so .*
means match any caracter zero or more times.
\\
matches a directory path's backslash \
is an escape character in normal regex, in findstr
we could o it either as \
or \\
to match correctly, I chose \\
because that is consistent with the defined syntax for regex across all platforms.
Finally we have to have a trailing `.\." to make sure we ignore all entries related to the sub directories.
Nice to note, that by using the findstr
within the FOR
loop's command portion which allows this to execute much more quickly.
To improve on Ben's answer, just
/S
to DIR
to make %%~tA
and %%~fA
work (full paths), and"tokens=*"
with /F
to support spaces in filenames (check FOR /?
)In other words:
FOR /F "tokens=*" %%a IN ('DIR /B /S /O:D /A:-D Input\*.json') DO (
ECHO "%%a"
)