How to loop through drive letters to run my script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

How to loop through drive letters to run my script

#1 Post by kingofdos » 14 Dec 2022 23:04

I have 5 scripts in my AutoHotkey folder that I need to run as a batch in .bat. The drive letters are different on different PCs. How do I loop through drive letters to find the right files?

sample

Code: Select all

Loop, Parse,% "CDEF"
	IF FileExist(Dir := A_LoopField ":\Dropbox\AHK\")
	{
		Run, %Dir%Autohotkey.ahk
		Run, %Dir%Script2.ahk
	}

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#2 Post by miskox » 15 Dec 2022 06:35

The code below will go thru letters C..Z:

Code: Select all

@echo off
for %%f in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do echo %%f
Replace echo %%f with the code you want (for example: if exist %%f:\folder_to_file\file_to_check.tmp).

Saso

kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

Re: How to loop through drive letters to run my script

#3 Post by kingofdos » 19 Dec 2022 19:12

Sorry for the noob question, but how do I run a script? what's the command? I tried like this but it doesn't work:

Code: Select all

@echo off
for %%f in (C D E F) 
{
do %%f:\Dropbox\AHK\Autohotkey.ahk
do %%f:\Dropbox\AHK\TextExpansion.ahk
}

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#4 Post by miskox » 20 Dec 2022 11:25

I don't know how AHK works (their syntax and how you call the scripts) but I guess you could use something like this:

This code will check if both .ahk files exist. If *both* exist it will first run first .ahk file and then the second one.

Code: Select all

@for %%f in (C D E F) do if exist %%f:\Dropbox\AHK\Autohotkey.ahk if exist %%f:\Dropbox\AHK\TextExpansion.ahk %%f:\Dropbox\AHK\Autohotkey.ahk&%%f:\Dropbox\AHK\TextExpansion.ahk
The code below will not check for presence of both .ahk files.

Code: Select all

@for %%f in (C D E F) do %%f:\Dropbox\AHK\Autohotkey.ahk&%%f:\Dropbox\AHK\TextExpansion.ahk
Saso

arjunae

Re: How to loop through drive letters to run my script

#5 Post by arjunae » 20 Dec 2022 18:24

I didnt do anything for years in batch, but theres a mountvol cmd which fetches a list from the registry and should (untested) also list network drives.

Code: Select all

@echo off
setlocal enabledelayedexpansion enableextensions
for /F "delims=" %%u in ('mountvol /l^|findstr ":\\"') DO (set hd=%%u & set hd= !hd: =! & echo !hd!)
pause
Note I would prefer Sasos Code when you eg use an environment that you are sure about where to search.
have Fun!

kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

Re: How to loop through drive letters to run my script

#6 Post by kingofdos » 22 Dec 2022 06:00

miskox wrote:
20 Dec 2022 11:25
I don't know how AHK works (their syntax and how you call the scripts) but I guess you could use something like this:

This code will check if both .ahk files exist. If *both* exist it will first run first .ahk file and then the second one.

Code: Select all

@for %%f in (C D E F) do if exist %%f:\Dropbox\AHK\Autohotkey.ahk if exist %%f:\Dropbox\AHK\TextExpansion.ahk %%f:\Dropbox\AHK\Autohotkey.ahk&%%f:\Dropbox\AHK\TextExpansion.ahk
The code below will not check for presence of both .ahk files.

Code: Select all

@for %%f in (C D E F) do %%f:\Dropbox\AHK\Autohotkey.ahk&%%f:\Dropbox\AHK\TextExpansion.ahk
Saso

Hi there,

I tried

Code: Select all

@for %%f in (C D E F) do %%f:\Dropbox\AHK\Autohotkey.ahk&%%f:\Dropbox\AHK\TextExpansion.ahk
and it only launches the first script instead of both scripts.
The path is definitely correct but it shows these errors below.

Is "do" the correct command when all I want to do is to run it just like an .exe?

Code: Select all

The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#7 Post by miskox » 23 Dec 2022 00:44

'DO' is part of the FOR command and is required: enter FOR /? and you will see:

Code: Select all

Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.
.
.
.
Errors you have are caused because path you have does not exist on every device you have. So you have to use the first version which checks the existence of the files.
You could use ECHO ON to see paths .cmd tries to use.

You could run this:

Code: Select all

@for %%f in (C D E F) do echo ---&dir %%f:\Dropbox\AHK\Autohotkey.ahk&dir %%f:\Dropbox\AHK\TextExpansion.ahk

What do you get?

Next question regarding
and it only launches the first script instead of both scripts.
It is possible that .ahk runs and does not wait until it finishes? (I don't know how .ahk works) This requires another approach. How do you run .ahk files manually? This info will help.

Saso

kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

Re: How to loop through drive letters to run my script

#8 Post by kingofdos » 26 Dec 2022 05:31

Using this

Code: Select all

@for %%f in (C D E F) do echo ---&dir %%f:\Dropbox\AHK\Autohotkey.ahk&dir %%f:\Dropbox\AHK\TextExpansion.ahk
only flashes the cmd window briefly with NO text whatsoever. I also noticed no scripts are launched for that matter.

I run .ahk files by simply double clicking on them, it works exactly like a normal .exe file and the code inside doesn't really matter.

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#9 Post by miskox » 26 Dec 2022 11:03

Add PAUSE at the end (looks like you double-click on the .cmd/.bat):

Code: Select all

@for %%f in (C D E F) do echo ---&dir %%f:\Dropbox\AHK\Autohotkey.ahk&dir %%f:\Dropbox\AHK\TextExpansion.ahk&pause
If you execute (start) your .ahk file from within the command prompt: does it return to the prompt before it actually completes the job?

What you get?

Saso

kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

Re: How to loop through drive letters to run my script

#10 Post by kingofdos » 29 Dec 2022 02:23

Code: Select all

E:\Dropbox\AHK>echo ---  & dir C:\Dropbox\AHK\Autohotkey.ahk  & dir C:\Dropbox\AHK\TextExpansion.ahk  & pause
---
The system cannot find the path specified.
The system cannot find the path specified.
Press any key to continue . . .

this shows there's no looping as it only tried C: and did not try D / E / F.

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#11 Post by miskox » 29 Dec 2022 05:14

It will go thru all the letters in the FOR command and PAUSE after *each* letter.

Saso

kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

Re: How to loop through drive letters to run my script

#12 Post by kingofdos » 29 Dec 2022 21:27

understand.

so when using this below, it only launches the first script, and not the second/third script. why is that so and how do I fix it?

Code: Select all

@for %%f in (C D E) do %%f:\Dropbox\AHK\Autohotkey.ahk&%%f:\Dropbox\AHK\TextExpansion.ahk&%%f:\Dropbox\AHK\Emojis.ahk

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#13 Post by miskox » 30 Dec 2022 00:45

Again: see my previous posts:
Errors you have are caused because path you have does not exist on every device you have. So you have to use the first version which checks the existence of the files.
It is possible that .ahk runs and does not wait until it finishes? (I don't know how .ahk works) This requires another approach. How do you run .ahk files manually? This info will help.
If you execute (start) your .ahk file from within the command prompt: does it return to the prompt before it actually completes the job?
And this one for the latest command:

Code: Select all

@for %%f in (C D E F) do echo ---&dir %%f:\Dropbox\AHK\Autohotkey.ahk&dir %%f:\Dropbox\AHK\TextExpansion.ahk&pause
What you get?
With the code above we should be able to see if both your .ahk files are there - but we can't because you don't provide the output.
so when using this below, it only launches the first script, and not the second/third script. why is that so and how do I fix it?
It will launch both commands (scripts) one after the other and the PAUSE command as you already know - so all three commands were executed.

That is why I asked you what happens if you execute the .ahk file from the command prompt (or with double click):
It is possible that .ahk runs and does not wait until it finishes? (I don't know how .ahk works) This requires another approach. How do you run .ahk files manually? This info will help.
If you execute (start) your .ahk file from within the command prompt: does it return to the prompt before it actually completes the job?
If you start both .ahk files one after the other: do you have your work done as expected? If not then I guess you have to run the first .ahk file, wait for it to finish and then you have to start the second .ahk file. So what is it?

Maybe this will work:

Code: Select all

@echo off
for %%f in (C D E F) do if exist "%%f:\Dropbox\AHK\Autohotkey.ahk" if exist "%%f:\Dropbox\AHK\TextExpansion.ahk" (
	title Running %%f: Autohotkey.ahk
	start "" /wait /b cmd /c "%%f:\Dropbox\AHK\Autohotkey.ahk"
	title Running %%f: TextExpansion.ahk
	start "" /wait /b cmd /c "%%f:\Dropbox\AHK\TextExpansion.ahk"
)
echo Done.
pause
If I run test.cmd:

Code: Select all

@echo off
break>test1.tmp
break>test2.tmp
for %%f in (C D E F) do if exist "%%f:test1.tmp" if exist "%%f:test2.tmp" (
	title Running %%f: Autohotkey.ahk
	ECHO start "" /wait /b cmd /c "%%f:\Dropbox\AHK\Autohotkey.ahk"
	start "" /wait /b cmd /c "dir %windir%"
	title Running %%f: TextExpansion.ahk
	ECHO start "" /wait /b cmd /c "%%f:\Dropbox\AHK\TextExpansion.ahk"
	start "" /wait /b cmd /c "dir %windir%"
)
del test1.tmp
del test2.tmp
echo Done.
pause
It works as expected.

It might work without

Code: Select all

start "" /wait /b
Depends how .ahk files are executed.

Very important: you must check for the presence of the .ahk files to avoid getting any errors (the IF EXIST statements).

Saso

kingofdos
Posts: 8
Joined: 14 Dec 2022 23:02

Re: How to loop through drive letters to run my script

#14 Post by kingofdos » 31 Dec 2022 20:21

Hi there,

I tried the code below and it only launches the first script, but not the 2nd one. Weird part is cmd doesn't print anything at all even though it's supposed to before launching my script.

Code: Select all

@echo off
for %%f in (C D E F) do if exist "%%f:\Dropbox\AHK\Autohotkey.ahk" if exist "%%f:\Dropbox\AHK\TextExpansion.ahk" (
	title Running %%f: Autohotkey.ahk
	start "" /wait /b cmd /c "%%f:\Dropbox\AHK\Autohotkey.ahk"
	title Running %%f: TextExpansion.ahk
	start "" /wait /b cmd /c "%%f:\Dropbox\AHK\TextExpansion.ahk"
)
echo Done.
pause
It is possible that .ahk runs and does not wait until it finishes? (I don't know how .ahk works) This requires another approach. How do you run .ahk files manually? This info will help.
i simply double click it like an .exe 1 by 1.
If you execute (start) your .ahk file from within the command prompt: does it return to the prompt before it actually completes the job?
no it doesn't.
If not then I guess you have to run the first .ahk file, wait for it to finish and then you have to start the second .ahk file.
how do I reflect that in the bat file?

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: How to loop through drive letters to run my script

#15 Post by miskox » 02 Jan 2023 08:53

I suspect that your second .ahk file is not there (I guess you don't check for the existence of both .ahk files).

Please execute this and provide the output:

test.cmd:

Code: Select all

@echo off
for %%f in (C D E F) do echo ---&dir %%f:\Dropbox\AHK\Autohotkey.ahk&dir %%f:\Dropbox\AHK\TextExpansion.ahk
pause
You could run it like this:

Code: Select all

test.cmd>temporary.tmp
and then copy/paste what temporary.tmp holds in here (between the CODE tags).

Saso

Post Reply