Need help creating a batch file...

Soldato
Joined
18 Jan 2005
Posts
4,171
Location
Northants
I've never made a batch file before, so i could do with some help. I want to create a batch file to remotely turn on my server by sending a wake on lan magic packet to it when the batch file is run. I then want it to open a directory on the server.

Heres some pseudo code:
Code:
If exists [server directory\some file or just the directory]
Then open [server root directory]
Else [send wol magic packet or run wol program]

Anyone know how to do this?


This code works if the text file exists but does nothing if it doesn't. The line after the else works on its own though.
Code:
IF EXIST Z:\test.txt start Z:\ 
ELSE start C:\"Documents and Settings"\Joe\Desktop\WakeOnLan.exe
I found a cmd line program which will send a wol packet, heres the info. Presumably i can run that if the server directory does not exist.

The point of this is to be able to have my server go into standby after a time and then wake when someone tries to access it. This is the only way i can think of to do that...

Thanks
 
Last edited:
There is no ELSE unfortunatly, easy to workaround though:

Code:
IF EXIST z:\file.ext GOTO :online
goto offline	
:online
start Z:\ 
GOTO end
:offline
start C:\WakeOnLan.exe
:end
 
RobH said:
There is no ELSE unfortunatly.

I beg to differ.

Code:
IF NOT EXIST Z:\test.txt START C:\"Documents and Settings"\Joe\Desktop\WakeOnLan.exe ELSE
START Z:\

The else is there but will only work if put on the same line as the IF statement.
 
Your right,

Code:
IF [NOT] EXIST filename command

Didn't see this part to start with.

Code:
command           Specifies the command to carry out if the condition is
                  met.  Command can be followed by ELSE command which
                  will execute the command after the ELSE keyword if the
                  specified condition is FALSE
 
Thanks very much.
Else definitely does exist as i read about it on a guide on the microsoft website.
However the version with else doesn't seem to work reliably, but the version RobH posted works perfectly.

Is there a way to have it see if the server directory exists, rather than seeing if a file in the server directory exists?
That would save me having to put a file in the server directory just for this batch file. Can i simply put 'if exist Z:\'?

Is there a way to hide the batch file window, so you don't actually see it appear when you run the batch file?


I also had an idea that it would be good if when the last pc on the network is shut down, it can put the server into standby. This is pretty complicated, but basically you would need a file on the server which remains 'true' as long as more than one pc is on or something, so the batch file on the last pc can know that no other pc is on, and can then somehow send a standby command to the server. Anyone know how i might do this?
 
Last edited:
Ok i've had a bit of a think and this is what i need help with now:

-How can i put a pc into standby using a batch file?

-Is there a way to hide the batch file window when it runs?

-Do i have to have a file for 'if exists' to work, or can i just point it at a directory?

-Can i get a batch file to look for an actual pc on the network, perhaps ping it or something?


I'll do some experimenting and research and see if i can answer these myself, but if anyone can help me out that would be great.

To get the server to shut down when the last client is switched off, i've realised it would be best to run a batch file on the server to look for the clients, so thats what i'm trying to do now.
 
-How can i put a pc into standby using a batch file?
Download this neat command line tool from sysinternals http://www.microsoft.com/technet/sysinternals/utilities/psshutdown.mspx and use the -d parameter to trigger a standby.

-Is there a way to hide the batch file window when it runs?
There are several utillities out there that can do this, just do a google search and you'll find one that meets your needs.

-Do i have to have a file for 'if exists' to work, or can i just point it at a directory?
You can just point to a directory or even the root of a drive.

-Can i get a batch file to look for an actual pc on the network, perhaps ping it or something?
This will ping the specified address "192.168.1.200" and do "start c:\" if there is no response from the ip address.
Code:
for /f "delims=." %%I in ('ping -n 1 192.168.1.200') do if "%%I"=="Request timed out" (start c:\)

The above command loops over the output of the ping command, splitting the output into tokens wherever a "." ocurs. It then assigns this to the %%I variable. If the variable contains the specific string (inidicating that the ping failed) then you execute a command. This could easily be modified to execute a command if the ping was sucessful.
 
Last edited:
Thanks very much indeed for your help.

I'll have to set up my laptop shortly as a mock server to test everything out.

I found an easy way to hide it, i made a shortcut pointing to the batch file and set the properties to minimised. It still briefly appears in the taskbar but thats much better than a nasty black popup window.

For that code you posted, how would i get it to ping multiple addresses, and run my standby program only if none of them responded?
Or even better, i'd like to be able to ping all addresses in a range as my router is dhcp so there are about 10 possible addresses for 5 machines.
Also, does that code continuously loop? If so, presumably that will create a lot of unnecessary network traffic, how would i set it to do it every 30 minutes or something?
 
It only sends a single ping request so there is no traffic really, just an ICMP ping request, less traffic than a single DNS query. Basically it does 1 ping and the for loop loops over the result of that ping so there is only ever a single ping. I think there is an easy way of pinging a range, I'll have a look when I get home.
 
I set up my laptop as the 'server' and everything worked perfectly. :)

I used scheduled tasks to run the standby program every 30 minutes, so every 30 minutes it will ping an address and if it gets no response it goes into standby.

Then when someone tries to access the server it runs the wol program and sends a magic packet and wakes it. Brilliant.

So its just a case of tidying up and neatening up now.


I could have a separate batch file for each ip i want to ping, but there's got to be an easier way of just putting a number of ips in the one batch file, so thats what i need next.

Edit: Another thing i could do with is a wait command...

Edit2: Found a wait command. Could do with a way to set the window to 'Normal Window' as it runs minimised. If the server is not online i want to maximise the window and tell the user to wait while the server comes out of standby, or something like that.
 
Last edited:
Back
Top Bottom