Batch file identifies a computer

H

Howard Brazee

I have a batch program that copies some files from my work computer to
a memory stick. At home, I have a batch file that copies them to
one computer, and another batch file that copies them to another
computer. (unfortunately the directory names are different).

I'd like to combine these - with something like this:

IF %computer name% = "Howard's Old Computer"
Destination = "Directory 1"


Actually, I see a work-around. All I need to do is create different
system variables for each computer. But it seems cleaner to use the
computer name.
 
T

Tom Porterfield

Howard said:
I have a batch program that copies some files from my work computer to
a memory stick. At home, I have a batch file that copies them to
one computer, and another batch file that copies them to another
computer. (unfortunately the directory names are different).

I'd like to combine these - with something like this:

IF %computer name% = "Howard's Old Computer"
Destination = "Directory 1"


Actually, I see a work-around. All I need to do is create different
system variables for each computer. But it seems cleaner to use the
computer name.

Take ths space out and you have it. The variable from batch is
%COMPUTERNAME%.
 
H

Howard Brazee

Take ths space out and you have it. The variable from batch is
%COMPUTERNAME%.

That worked. But do you know what's wrong here?
set MYTEST="xxx"
if "%COMPUTERNAME%"=="BRAZEEXHP" set MYTEST="A"
if %COMPUTERNAME%==BRAZEEXHP set MYTEST="B"
if "%COMPUTERNAME%"==BRAZEEXHP set MYTEST="C"
if %COMPUTERNAME%=="BRAZEEXHP" set MYTEST="D"
if %COMPUTERNAME%==BRAZEEXHP MYTEST="E"
echo %COMPUTERNAME% "MYTEST=" %MYTEST% "."

produces:
O:\>set MYTEST="xxx"

O:\>if "BRAZEEHXP" == "BRAZEEXHP" set MYTEST="A"

O:\>if BRAZEEHXP == BRAZEEXHP set MYTEST="B"

O:\>if "BRAZEEHXP" == BRAZEEXHP set MYTEST="C"

O:\>if BRAZEEHXP == "BRAZEEXHP" set MYTEST="D"

O:\>if BRAZEEHXP == BRAZEEXHP MYTEST="E"

O:\>echo BRAZEEHXP "MYTEST=" "xxx" "."
BRAZEEHXP "MYTEST=" "xxx" "."
 
T

Tom Porterfield

Howard said:
That worked. But do you know what's wrong here?
set MYTEST="xxx"
if "%COMPUTERNAME%"=="BRAZEEXHP" set MYTEST="A"
if %COMPUTERNAME%==BRAZEEXHP set MYTEST="B"
if "%COMPUTERNAME%"==BRAZEEXHP set MYTEST="C"
if %COMPUTERNAME%=="BRAZEEXHP" set MYTEST="D"
if %COMPUTERNAME%==BRAZEEXHP MYTEST="E"
echo %COMPUTERNAME% "MYTEST=" %MYTEST% "."

produces:
O:\>set MYTEST="xxx"

O:\>if "BRAZEEHXP" == "BRAZEEXHP" set MYTEST="A"

O:\>if BRAZEEHXP == BRAZEEXHP set MYTEST="B"

O:\>if "BRAZEEHXP" == BRAZEEXHP set MYTEST="C"

O:\>if BRAZEEHXP == "BRAZEEXHP" set MYTEST="D"

O:\>if BRAZEEHXP == BRAZEEXHP MYTEST="E"

O:\>echo BRAZEEHXP "MYTEST=" "xxx" "."
BRAZEEHXP "MYTEST=" "xxx" "."

Curious. It runs fine on my machine (changed to match my machinename of
course) with one exception. if %COMPUTERNAME%==BRAZEEXHP MYTEST="E" has
incorrect syntax and produces the following error:

'MYTEST' is not recognized as an internal or external command, operable
program or batch file.

Interesting that you are not seeing that error. I assume the above is in a
batch file executed via cmd.exe. Is that correct?
 
H

Howard Brazee

Interesting that you are not seeing that error. I assume the above is in a
batch file executed via cmd.exe. Is that correct?

Yep. Actually, sometimes I ran the .BAT file by clicking on it.

Let me try with a simplified TEST.BAT

echo on
if "%COMPUTERNAME%"=="BRAZEEXHP" goto :past
if %COMPUTERNAME%==BRAZEEXHP goto :past
if "%COMPUTERNAME%"==BRAZEEXHP goto :past
if %COMPUTERNAME%=="BRAZEEXHP" goto :past
if %COMPUTERNAME%==BRAZEEXHP goto :past
echo "not past"
:past
echo "past"
=============
Testing from a CMD window:

P:\>c:

C:\>cd belfry

C:\BELFRY>test

C:\BELFRY>echo on

C:\BELFRY>if "BRAZEEHXP" == "BRAZEEXHP" goto :past

C:\BELFRY>if BRAZEEHXP == BRAZEEXHP goto :past

C:\BELFRY>if "BRAZEEHXP" == BRAZEEXHP goto :past

C:\BELFRY>if BRAZEEHXP == "BRAZEEXHP" goto :past

C:\BELFRY>if BRAZEEHXP == BRAZEEXHP goto :past

C:\BELFRY>echo "not past"
"not past"

C:\BELFRY>echo "past"
"past"

C:\BELFRY>
 
P

Pegasus \(MVP\)

Howard Brazee said:
I have a batch program that copies some files from my work computer to
a memory stick. At home, I have a batch file that copies them to
one computer, and another batch file that copies them to another
computer. (unfortunately the directory names are different).

I'd like to combine these - with something like this:

IF %computer name% = "Howard's Old Computer"
Destination = "Directory 1"


Actually, I see a work-around. All I need to do is create different
system variables for each computer. But it seems cleaner to use the
computer name.

The usual way to make these tests more robust requires the /i switch:

if /i "%COMPUTERNAME%"=="BRAZEEXHP" goto :past

The double quotes are required if the variables include embedded
spaces. Always put them there if unsure.

If you're going to use this code on machines whose names are
known then you can simplify it like so:

@echo off
goto %ComputerName%

:BRAZEEXHP
(your code goes here)
goto :eof

:BRAZEEXBP
(some other code here)
 
H

Howard Brazee

On Thu, 2 Nov 2006 15:18:34 -0500, "Tom Porterfield"

Interesting thing, I took that same test home, changed the computer
name and the test worked. Something is different between the two
computers (besides the name).
 
H

Howard Brazee

The usual way to make these tests more robust requires the /i switch:

if /i "%COMPUTERNAME%"=="BRAZEEXHP" goto :past

How does this make it more robust? I'm not familiar with that
switch.

It didn't work on my work machine, it wasn't necessary on my home
machines.
The double quotes are required if the variables include embedded
spaces. Always put them there if unsure.

If you're going to use this code on machines whose names are
known then you can simplify it like so:

@echo off
goto %ComputerName%

:BRAZEEXHP
(your code goes here)
goto :eof

:BRAZEEXBP
(some other code here)

This worked. There is a downside with this code in that if the goto
destination is not there, my test batch job failed. The IF
statement would be preferable for error checking - provided the IF
statement was reliable on all of my machines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top