Reputation: 11
I'm wanting to order files in a directory based on date of creation (or at least, last modified).
I have this code:
FOR /R "directory" %%F IN (*.filetype) DO echo "command" "%%F" > "another\directory\%%~nF.tsv"
...which runs a for loop on files in no particular order (alphabetically?).
What I want to be able to do is:
FOR /R "direcotry\*.filetype" %%F IN ('dir /o-d /b "directory"') DO echo "command" "%%F" > "another\directory\%%~nF.tsv"
The lower code doesn't work, but you may see what I mean.
I'm proficient in BASH
but not in BATCH
The key is dir /o-d /b "drive:\directory\*.filetype"
, which orders by creation date (reverse order), and then spits out only the file name -- but I don't know how to work this into a for loop for taking file names, and then running a command on them.
Upvotes: 1
Views: 3330
Reputation: 38623
Due to the limitation mentioned in the comment to the other answer, here is a potential workaround leveraging PowerShell:
@For /F "Delims=" %%A In (
'Powershell "(GCI "directory" -R *.filetype|Sort CreationTime -Des)|%%{$_.Fullname}"'
) Do @"command" "%%A">"another\directory\%%~nA.tsv"
Obviously adjusting directory
, filetype
, "command"
and another\directory
as required
Upvotes: 0
Reputation: 80023
FOR /F "delims=" %%F IN ('dir /o-d /tc /b /s "directory\*.filetype"') DO echo "command" "%%F" >> "another\directory\%%~nF.tsv"
for/f
to process a "file" of text.
"delims="
to turn tokenising off so the entire line of "output" from the dir
is assigned.
/tc
to use creation date (o/[-]d
omitted = alphabetical order from an NTFS drive, storge-order on FAT)
/s
to add subdiectories
>>
to append to output file.
OR
...DO "command" "%%F"
to execute the command directly without creating the file.
Upvotes: 1