-1

I'm trying to open a number of files in Chrome, in the order they appear in the folder. So far, I've managed to open them in Chrome but in a random order, and echo the file names in the correct order.

@echo off
set args1=%1
for /f "tokens=*" %%s in ('dir /b %args1%\* ^| sort') do start C:\"Program Files"\Google\Chrome\Application\chrome.exe file:///Z:/son/%args1%/%%s

Gives me the former result, which is close, but I really need them in the correct order.

@echo off
set args1=%1
for /f "tokens=*" %%s in ('dir /b %args1%\* ^| sort') do echo %%s

Gives me the filenames in the desired order.

What gives? Isn't it logical that they should therefore open in alphabetical order?

@echo off
set args1=%1
for /f "tokens=*" %%s in ('dir %args1%\* /b /ON') do start C:\"Program Files"\Google\Chrome\Application\chrome.exe file:///Z:/son/%args1%/%%s

Did not work either. Maybe it's Chrome?

SKbird
  • 1
  • A 'folder' does not have an order, it is simply a named pointer to items within the file syatem. I can only presume that by using the terms "they appear", you mean the sort order configured for your chosen file manager. As we don't know which file manager you are using, or the sort order configured within it, it is not possible for us to guess, and therefore solve your issue. – Compo Oct 26 '22 at 21:07
  • `C:\"Program Files"\Google\Chrome\Application\chrome.exe` is syntactically wrong and works only because of automatic error correction made by `cmd.exe`. The entire fully qualified file name must be enclosed in `"` and not just parts of it. Open a command prompt window, type `C:\Pro` and press key __TAB__. `cmd.exe` completes to `"C:\Program Files"` - note the double quotes. Now press slowly __TAB__ a few more times and see what `cmd` suggests. When is shown again `"C:\Program Files"` continue typing with `\Goo` resulting in displayed is `"C:\Program Files"\Goo` and press __TAB__ once again. – Mofi Oct 27 '22 at 05:13
  • There is displayed now `"C:\Program Files\Google"`. Do you see what happened with the second `"`? It was moved by `cmd` to end of the full folder name. You have learned now from `cmd` how to reference a file or folder (or other argument strings passed to an executable) containing a space or one of these characters ``&()[]{}^=;!'+,`~`` (and `<>|` like in a password string) using the file/folder name completion feature described by the usage help of `cmd` output on running `cmd /?` in command prompt window. – Mofi Oct 28 '22 at 05:55

1 Answers1

-1

I added a timeout in the for loop, and that worked, but that was limited to 1 second increments. Then I found that you can use ping instead, but that was still not fast enough. I've yet to figure out how to go below 500 ms.

SKbird
  • 1
  • Take a look at the `pathping` command, it will allow you to shorten the delay to less than a second. – Compo Oct 26 '22 at 21:12
  • That worked! I added "pathping -p 30 -q 2 localhost >nul" to the loop, and it was real fast, and in the correct order. I found that anything less than 30 was too quick for it, but 30 is good enough. – SKbird Oct 26 '22 at 21:27
  • That is a very awful solution. It is awful because of starting multiple instances of Google Chrome which all have to find out first if another Chrome process is already running and if that is the case and the user configured Chrome to use by default just one instance, pass the URL using a pipe to the already running Chrome instance and then close itself. It would be much better to concatenate first all URLs together using an environment variable and finally start just once Chrome process with all the URLs as command line options because of Chrome supports multiple URLs too. – Mofi Oct 27 '22 at 05:20
  • Please see the code in chapter __Running application with multiple files__ (in your case file URLs instead of file names) in my answer on [How to write all files found in one directory into one command line for calling an executable?](https://stackoverflow.com/a/62359278/3074564) You can use that code as template and adapt it to your needs. When rewriting the batch file was done by you and it works as expected by you, it would be good to edit your answer and post the finally used code and explain why that code solves the problem and is better than starting delayed `chrome.exe` multiple times. – Mofi Oct 27 '22 at 05:47