PC Review


Reply
Thread Tools Rate Thread

Batch file identifies a computer

 
 
Howard Brazee
Guest
Posts: n/a
 
      2nd Nov 2006
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.
 
Reply With Quote
 
 
 
 
Tom Porterfield
Guest
Posts: n/a
 
      2nd Nov 2006
Howard Brazee wrote:
> 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%.
--
Tom Porterfield

 
Reply With Quote
 
Howard Brazee
Guest
Posts: n/a
 
      2nd Nov 2006
On Thu, 2 Nov 2006 12:56:08 -0500, "Tom Porterfield"
<(E-Mail Removed)> wrote:

>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" "."
 
Reply With Quote
 
Tom Porterfield
Guest
Posts: n/a
 
      2nd Nov 2006
Howard Brazee wrote:
> On Thu, 2 Nov 2006 12:56:08 -0500, "Tom Porterfield"
> <(E-Mail Removed)> wrote:
>
>> 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" "."


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?
--
Tom Porterfield

 
Reply With Quote
 
Howard Brazee
Guest
Posts: n/a
 
      2nd Nov 2006
On Thu, 2 Nov 2006 15:18:34 -0500, "Tom Porterfield"
<(E-Mail Removed)> wrote:

>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>
 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      2nd Nov 2006

"Howard Brazee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)


 
Reply With Quote
 
Howard Brazee
Guest
Posts: n/a
 
      3rd Nov 2006
On Thu, 2 Nov 2006 15:18:34 -0500, "Tom Porterfield"
<(E-Mail Removed)> wrote:

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).


>Howard Brazee wrote:
>> On Thu, 2 Nov 2006 12:56:08 -0500, "Tom Porterfield"
>> <(E-Mail Removed)> wrote:
>>
>>> 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" "."

>
>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?

 
Reply With Quote
 
Howard Brazee
Guest
Posts: n/a
 
      3rd Nov 2006
On Fri, 3 Nov 2006 09:33:46 +1100, "Pegasus \(MVP\)" <(E-Mail Removed)>
wrote:

>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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create a batch file to lock computer? liu Windows XP General 6 20th Feb 2009 01:52 AM
Rename computer from a batch file? Mark K Vallevand Windows XP Embedded 6 16th Dec 2005 04:54 PM
RE: Rename Computer with a Script or Batch file =?Utf-8?B?R2F5bGVu?= Windows XP Networking 0 24th Jul 2004 07:49 PM
How can I uniquely identifies a computer? Ray Microsoft Dot NET Framework 1 9th Jul 2003 10:36 AM
How can I uniquely identifies a computer? Ray Microsoft Dot NET 1 9th Jul 2003 10:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:30 AM.