Zachary Wight
Zachary Wight

Reputation: 55

Batch file Start program

M'kay, so I've written a few batch files before, so I'm not completely new to them, but this is stumping me. What I'm trying to do is run a .exe file from a batch file. Here's the Batch script:

@echo off
:start
setlocal EnableDelayedExpansion
cd "C:\Users\Zac\Dropbox\SoundCloud"
set n=0
for %%f in (*.html*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
move "!file[%rand%]!" C:\Users\Zac\Temp
start "~dp0Link_Open.exe"
echo %time%
timeout 70 > NUL
echo %time%
goto start

So from my understand, this moves a random .html file from one directory to another, this works, I've used it a lot, the only issue is the "Start" command, I don't use this very often. the "Link_Open.exe" is in the same folder as my .bat, but I've tried running it with the full directory written in, I've tried quotations, no quotations, brackets, no brackets, START, start, Start, Call, CALL, call, and none of them work, I'm always getting the same error "Link_Open.exe cannot be found, have you written it correctly"

The only reason I can possibly think of that would be why it wouldn't work, is that the .exe was written in AutoIT and then compiled...but that shouldn't effect it should it?

Running the batch file will result in a random file being moved, and then an error coming up, and then repeating.

What am I doing wrong?

Ps: Running the Link_Open.exe does what it's supposed to do, so there's no errors there, the only issue I'm having is opening it with .bat.

I'm still very new to Autoit, but if someone could show me a script to move a random .html file with Autoit, I could just combine the two scripts together couldn't I?

Upvotes: 0

Views: 1544

Answers (2)

kay27
kay27

Reputation: 909

  1. You change directory to C:\Users\Zac\Dropbox\SoundCloud and then try to start Link_Open.exe which is left in script directory.

  2. So next step you use ~dp0 variable to get folder from script executed, but there is missed leading %: you must write %~dp0

  3. Even though you'll fix it, maybe Link_Open.exe also depend on current working directory? start allows to set up CWD: start "window header" /D "%~dp0" "%~dp0Link_Open.exe"

To debug: add echo before start and add pause next line. start command will be printed with all variables expanded. You'll see if the something is wrong - e. g. you may copy and test via Start -> Run.

Upvotes: 0

clarity123
clarity123

Reputation: 2046

Provided that your bat file and exe file are in the same directory as you said, try changing:

START Link_Open.exe

to:

start "%~dp0Link_Open.exe"
  • %~dp0 expands to the current batch script's path
  • start equivalent to START, it doesn't matter
  • enclosed in quotes in case your path has spaces like C:\My Documents, thus keeps the whole path as one argument, instead of being misinterpreted as being multiple arguments separated by space

Error Explanation

Your error: Link_Open.exe cannot be found... is precisely that, batch can't find Link_Open.exe. Normally it would, because when you just give a program without full absolute path, it checks the current working directory. This defaults to where the batch script is. But notice you actually changed the current working directory with this line:

cd "C:\Users\Zac\Dropbox\SoundCloud"

Thus, unless you happen to have Link_Open.exe in that SoundCloud directory, or unless you had any code to cd back to the batch script's directory containing the exe, or unless you happen to have Link_Open.exe in %PATH%, then there is no reason batch would automatically know to check the original directory that the script was run from.

Thus we try to refer to Link_Open.exe in a way that batch will know how to find it. One way is if we did absolute path C:\path\to\Link_Open.exe however to keep things portable, I recommend %~dp0 which expands to whatever path of the current batch script, and thus batch is able to find Link_open.exe again.

Upvotes: 1

Related Questions