Batch file to eject USB External Drive that is "in use" - batch-file

I have a batch file that copies files from and to an external hard drive. I wanted to add a section at the end of the script that would automatically eject the hard drive so it could be safely removed. I did some research and found that Remove Drive is my best option. I have it set up to use and working. I can write a bat file
#echo off
RemoveDrive :E -L
and it works perfectly!
However, I've hit a snag. When I add this exact line (well without the #echo off) to the end of my bat file, it fails to eject. My guess is that its the same problem as when you've got files open from a usb drive and try to eject it. It won't work because the system is using it. I tested this by creating a seperate bat file that ran just the RemoveDrive named it "RemoveDrive.bat". I then added to the end of my batch file:
pause
call "RemoveDrive.bat"
When the script hit the pause command. I tried to run the batch file by simply clicking the icon. The eject attempt failed. I pressed any key to continue and the called bat file failed and the script (and cmd window) closed. I then double clicked the icon for "RemoveDrive.bat" and it worked.
So after that long explanation, I was wondering if anyone had any suggestions for how to work around this. I was thinking if there was a way I could use the call function with some sort of delay and could close the cmd window. If I could get the cmd window to close and then have the bat file run automatically, I'm betting that would work.

Related

Run multiple lines from .bat file

I'm trying to get a bat file together and not sure how to run multiple cmd lines through the .bat.
Objective: I want to be able to click on this .bat file to open cmd prompt, then to find a "folder" in the directory, rename the "Folder" then move the location of the folder then find a existing Folder and put it in the same directory.
Problem: i know how to run these cmds in the prompt without an issues.
So far this is what i have:
#echo off
start cmd.exe /k cd %AppData%\Microsoft\Network\Connections
#echo off
and then just write your commands its that simple
A .bat file is executed in a terminal window automatically, you don't need to call cmd.exe or anything like that.
Every single line you write in the .bat file is going to be executed one by one, so you just need to write multiple lines with all the instructions and then you can double click on your new file to run the script.
If you are not familiar with Windows commands for the terminal, you can find more info on the web about how to find a folder, rename it, etc. There are many things you can do by command line and this is not the right place to explain how to use them.

Batch file works when run from command prompt, but not when clicked on

I have a simple one-line batch script to map a network drive drive. I'd like to place this in the startup folder so that I have the drive available whenever I turn my computer on.
The file looks like this:
#echo off
pushd \\path.to.network\drive
When I open up command prompt and run map_drive.bat, it works as intended and I am now able to access my drive. However, when I double click on the file or place it in the startup folder and turn my machine on, nothing happens.
]I've tried running as an administrator, putting the network path in quotes, and adding a "pushd %~dp0" line to the start of the file, but none of these so far have made a difference.

How to fix my batch file to open the appropriate program?

I'm attempting to write a batch file that will move to the specified directory and then run the command to open my desired program. Specifically I want it to run the command HardwareSimulator so it will open the software nand2tetris provides.
I've gotten it to move to the directory I want, but the opening is my issue. Code is displayed below. I'm guessing start isn't the correct command since when I run, it just runs an infinite loop of opening cmd prompts.
My second question would be: can I only go into sub-directories of where my batch file is already stored? It would be easier to store it in my desktop, so I can just click it whenever, but I can't seem to make it back out of a directory and then go down into another.
start cmd
pushd \nand2tetris\projects\P1Codes
start HardwareSimulator
pause
You can use ..\ to go back a directory.
For instance, if you had nand2tetris in your downloads folder you could get to it from the desktop with this script. Also make sure to to include the file extension.
pushd ..\Downloads\nand2tetris\projects\P1Codes
start HardwareSimulator.exe
pause

Starting a batch file from another batch file only opens CMD

When I start a batch file from another batch file, it just opens a new CMD window named just "TEST.bat", and doesn't run the actual batch. Running it manually works fine.
cd %~dp0\Colours\TEST.bat
start "TEST.bat"
I have tried many different ways to run the batch, but it all does the same thing. I've also tried to run the batch as administrator but same result again.
Full code(not finished): http://pastebin.com/GE8yJP0J
To run another batch file, use call not start. Also: cd expects a directory, not a filename.
cd "%~dp0\Colours"
call TEST.bat

Program opened with batch file won't work

At work I use a bunch of different programs that are located in many different folders that I have to open when I get here. So I've created a batch file that opens all of them for me.
I've run into an issue with one program that's located on a shared network. In my batch file I put
START "" "\\server\path\program.exe"
It loads the program but when I try to navigate the program is doesn't work. I run into an exception error that says it can't find a file on the C drive. But when I load the same program from the folder without the batch file, it works just fine. Any idea what's causing this?
Try specifying the working folder:
START "" /D "\\server\path" "\\server\path\program.exe"
It's documented in the output of start /?.

Resources