1

I'm trying to make a batch file run a .exe file after I've downloaded it. The problem is, the name of the downloaded file is randomized upon download (done so from the webpage it's downloaded from). I have no way of guessing the name of the file, so I want the batch file to run the .exe file regardless of what name it may have.

I want it to:

1: download the file - which it does perfectly.

2: then run the .exe file regardless of the name - which doesn't work as intended. (Error; Windows cannot find '*.exe'. Make sure you've typed the name correctly, then try again) - I have tried several solutions provided around the web from a mashup of questions - nothing works.

3: delete the file - which it does perfectly.

This is what I got so far:

echo Downloading file...
start "" https://TheWebPagesName.com
PING localhost -n 10 >NUL
echo Running file...
start "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername\*.exe"
PING localhost -n 10 >NUL
echo Deleting File...
del "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername\*.exe"
PING localhost -n 2 >NUL
echo Done!
PING localhost -n 6 >NUL
echo Exiting...
PING localhost -n 4 >NUL

Any suggestions?

  • Do you have any control over where the file is downloaded to? What directory? – LPChip May 8 '18 at 18:47
  • Yes. It is downloaded to "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername" It is also the ONLY .exe file in that directory, in case that makes any difference. – H4yw1r3_88 May 8 '18 at 18:50
  • Yes it does make a difference, because then you can search for it using forfiles or for, and store its result in a variable. I don't have time to write you an answer right now, but with this information, others will be able to do so. – LPChip May 8 '18 at 18:55
  • In case you want to give it a crack yourself, open a command prompt and type forfiles /?. its not that hard really. – LPChip May 8 '18 at 18:56
  • Would this work?: forfiles /p "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername" /m *.exe /c start – H4yw1r3_88 May 8 '18 at 18:58
0

You can accomplish this by using forfiles.

Forfiles can search for files, and then execute a command and use the files it finds as a variable, or do some checks. Forfiles supports filemasks, and given that you explained that the exefile is the only file in the folder, forfiles can be used.

Your script would look like this:

echo Downloading file...
start "" https://TheWebPagesName.com
PING localhost -n 10 >NUL
echo Running file...

:: comment out old row for easy reading    
:: start "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername\*.exe"

:: insert new comamnd instead:
forfiles /p "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername" /m *.exe /c "@file"

:: if you were not using executables, but would want to use start file.txt, you would use:
::forfiles /p "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername" /m *.txt /c "cmd /c @file"

PING localhost -n 10 >NUL
echo Deleting File...
del "C:\Users\MyUsername\OneDrive\Dokumenter\Foldername\*.exe"
PING localhost -n 2 >NUL
echo Done!
PING localhost -n 6 >NUL
echo Exiting...
PING localhost -n 4 >NUL
|improve this answer|||||
  • Thank you! It works perfectly. Much appreciated! Have a great day :) Cheers! – H4yw1r3_88 May 9 '18 at 14:09
0

Use aria2 to download that file. Then run it. Use this batch script:

aria2c.exe --out="FileName.exe" --check-certificate=false --dir="C:\folder\path" "download_link"
"C:\folder\path\FileName.exe"
timeout /t 10
del "C:\folder\path\FileName.exe"
pause
|improve this answer|||||
  • Thank you for your reply! However, this only came up with "aria2c.exe' is not recognized as an internal or external command, operable program or batch file." - LPChip's reply above works just perfectly, however. Thanks for your time & reply, once more. Cheers - have a great day :) – H4yw1r3_88 May 9 '18 at 14:13
  • @H4yw1r3_88 you have to open command prompt where you've downloaded aria2 executable file. Or enter the full path of aria2c in command prompt. – Biswapriyo May 9 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.