I have a folder full of config files called config#.json, where the # is a 2 digit base 15 number. I need some way to select one of the files, and put its name in a variable
I have been thinking about this for a while, but I'm not a very advanced batch programmer. There may be a way by using the results from a dir command, for example, if I get the last entry from the 8th line or something. I can get rid of the space in the name which may make it easier:
Volume in drive C is Local Disk
Volume Serial Number is 1A97-93E9
Directory of C:\Users\hazzj\Google Drive\Home\Stuff\Apache-Server\htdocs\launcher_profiles
12/10/2016 05:44 PM <DIR> .
12/10/2016 05:44 PM <DIR> ..
12/10/2016 05:44 PM 707 config 11.json <- this bit here
12/10/2016 05:44 PM 707 config 12.json
12/10/2016 05:44 PM 707 config 13.json
12/10/2016 05:44 PM 707 config 14.json
12/10/2016 05:44 PM 707 config 15.json
12/10/2016 05:44 PM 707 config 16.json
12/10/2016 05:44 PM 707 config 17.json
12/10/2016 05:44 PM 707 config 18.json
12/10/2016 05:44 PM 707 config 19.json
204 File(s) 144,228 bytes
2 Dir(s) 344,802,287,616 bytes free
any help would me greatly appreciated.
for /f "delims=" %%a in ('dir /b /a-d "yourdirectoryname" ') do set "filename=%%a"
is the standard method to get that filename - if you want the last in directory
Modify the dir command by adding /o:d to find the last-by-date, /o:-d first-by-date, /o:n to find the last-by-name, /o:-n first-by-name - and there are other options - see dir /? from the prompt.
eg
for /f "delims=" %%a in ('dir /b /a-d /o-n "yourdirectoryname" ') do set "filename=%%a"
(there are other ways)
This is probably the simplest answer.
@echo off
for /f "delims=" %%a in ('dir /b /a-d /on config*.json') do set lastone=%%a
echo %lastone%
Basically, you're getting the bare (/b) directory listing (dir) of files (/a-d) in descending name order (/on), processing each line of the output of the command in for /f and lastone ends up set to, well, the last one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With