Best way to send contents of text file to the DIR command?

Microsoft Windows
Post Reply
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Best way to send contents of text file to the DIR command?

Post by Rekrul »

I have a text file containing just filenames, one per line.I want to send those filenames to the DIR command so that they can be sorted by date and output to a new text file in that order.

The best I could come up with is this;

Code: Select all

@echo off
set Names=

for /f %%F in (List.txt) do (
set Name=%%F
call :AddNames
)
dir /b /od %Names% >NewList.txt
exit /b

:AddNames
set Names=%Names% %Name%
exit /b
Is there a more elegant way to do this? I tried various methods to redirect the contents of the file to DIR with pipes and redirection commands, but nothing seemed to work. Or I was using them wrong.
bluesxman
Posts: 10
Joined: 2021-Jul-26, 3:41 pm

Re: Best way to send contents of text file to the DIR command?

Post by bluesxman »

I don't think there is a more elegant native way.

My preferred syntax is:

Code: Select all

for /f "usebackq tokens=*" %%F in ("List.txt") do (
...
)
Incidentally, you probably want to protect your file names, vars with quotes -- in case of spaces/special characters.
e.g. (untested)

Code: Select all

@echo off
set "Names="

for /f "usebackq tokens=*" %%F in ("List.txt") do (
  call :AddNames "%%~F"
)
dir /b /od %Names% >NewList.txt
exit /b

:AddNames
set "Names=%Names% %1"
exit /b
Also there is a hard limit to the length of a variable, so if you have a lot of entries a pseudo-array would probably be a better choice, rather than a single string variable. But I'll leave that as an exercise for the reader.
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Re: Best way to send contents of text file to the DIR command?

Post by Rekrul »

bluesxman wrote: 2023-Jul-11, 12:59 pm I don't think there is a more elegant native way.
OK, thanks.
bluesxman wrote: 2023-Jul-11, 12:59 pmIncidentally, you probably want to protect your file names, vars with quotes -- in case of spaces/special characters.
bluesxman wrote: 2023-Jul-11, 12:59 pmAlso there is a hard limit to the length of a variable, so if you have a lot of entries a pseudo-array would probably be a better choice, rather than a single string variable. But I'll leave that as an exercise for the reader.
I'm a big believer in accounting for all possible problems, but in this case neither one should be an issue.

I'm making this to perform a very specific task. It will be part of a script for renaming files from one specific website. When people post images there, sometimes they post related images and I like to add something to the name, as well as a number so that they're grouped together and kept in sequence. I already have a script to do this, but as it's most convenient to have my file manager sort them with the newest listed first, I always have to make sure to save the images in the reverse order of how they should be numbered. Or I have to stop, reverse the sort order, invoke my script, then reverse the sort again.

Being able to take the given list of files and automatically sort them according to date, oldest first, before renaming them will be more convenient.

And since the files are all named in a particular way from the site, they never contain any problematic characters or spaces. I've also tested it with 100 filenames of this type and the variable was able to hold them all. I don't think I've ever saved more than 20-30 in one sequence, so that should be enough.

Plus my script does other tricks, like using a third-party command to bring the existing rename script window to the front, so that I can use the command history to recall previous entries in case I want to alter one I used previously. It also creates an Undo script in case I make a typo and don't notice it before hitting Enter. In which case, I can undo the changes, select the files again, use the command history to recall my previous entry and then fix the typo. :)
Post Reply