%COMPUTERNAME% returning upper case name

Page 1 of 2 12 LastLast

  1. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
       #1

    %COMPUTERNAME% returning upper case name


    My computers have mixed case names and I use these mixed case names in directories on some NAS devices. I wanted to use these names in some .bat files and would prefer to have the same .bat files on each computer rather than custom files on each computer. But I just discovered that %computername% returns upper case. Is there some other variable that returns the true name?

    This is on Win10 1909.
      My Computer


  2. Posts : 11,664
    Windows 10 Home x64 Version 21H2 Build 19044.1776
       #2

    The easiest workarounds would be
    1 In any If statement, use the /I switch so that the comparison is done without regard to case.
    2 At the start of your batch files you could insert an If statement that retrieves the proper computername and uses it to set a ThisComputer variable that has the format you prefer to work with within the batch file.

    Denis
      My Computer


  3. Posts : 1,513
    X
       #3

    Try3 is right. Don't depend on case to make decisions.
    But if you must get the "right" spelling, get it from the registry.
    One place is HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters.
    The "HostName" value found there will contain the proper host name.
      My Computer


  4. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
    Thread Starter
       #4

    Try3 said:
    The easiest workarounds would be
    In any If statement, use the /I switch so that the comparison is done without regard to case.
    An "If" statement is not involved. I'm using the computer name as part of a (case sensitive) path name on a NAS device:
    Synchronize Remote -Delete "F:\Test" "/Backups/%computername%/Test/"
    margrave55 said:
    But if you must get the "right" spelling, get it from the registry.
    One place is HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters.
    The "HostName" value found there will contain the proper host name.
    Uh, OK. How do I access registry records in a .bat file. Or do I need to switch to a PowerShell script? (I know little about .bat scripting and nothing about PowerShell.)
      My Computer


  5. Posts : 11,664
    Windows 10 Home x64 Version 21H2 Build 19044.1776
       #5

    So, as advised,
    Code:
    If "%ComputerName%"=="FRED" Set ThisComputerName=Fred
    Synchronize Remote -Delete "F:\Test" "/Backups/%ThisComputerName%/Test/"

    But, if you learn the For & Reg commands, you will also be able to use
    Code:
    For /F "usebackq tokens=3 " %%X in (`Reg Query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v HostName`) Do Set ThisComputerName=%%X
    Synchronize Remote -Delete "F:\Test" "/Backups/%ThisComputerName%/Test/"

    Denis
    Last edited by Try3; 10 Jun 2020 at 14:45.
      My Computer


  6. Posts : 1,513
    X
       #6

    Read up on the reg.exe command.
    pokeefe0001 said:
    How do I access registry records in a .bat file. Or do I need to switch to a PowerShell script? (I know little about .bat scripting and nothing about PowerShell.)
      My Computer


  7. Posts : 720
    Win10 x64 Pro - 2 desktops, 2 laptops
    Thread Starter
       #7

    Thanks.
      My Computer


  8. Posts : 11,664
    Windows 10 Home x64 Version 21H2 Build 19044.1776
       #8

    Useful study materials for batch file scripting & PowerShell


    Just to round things off -

    Here are links to useful study materials for batch file scripting & PowerShell
    Batch file, PowerShell guides - post #13 - TenForums
    Manipulating variables in batch files - post #14 - TenForums

    I believe that, if you are going to invest time & energy to study any form of scripting, then PowerShell is the one most likely to generate the biggest return on that investment.

    Denis
    Last edited by Try3; 27 Nov 2020 at 22:22.
      My Computer


  9. Posts : 1,513
    X
       #9

    Even better ... use bash shell under WSL.
    Real Linux, on Windows.
      My Computer


  10. Posts : 19
    Windows 10
       #10

    Thanks, is there web to discuss when to apply PowerShell vs VBscript vs VBA?
    Example of my dilemna:
    PS is powerful but cryptic & Import-Modules isn't ez. VBscript isn't suitable as modules nor Class. VBA is being replaced by Java.
    As a non-IT user, how to know when to use which.
    My superiors equates lots-of-code-line to complexity and few line of codes is best.
    I need some guidance if any! Thanks!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 05:55.
Find Us




Windows 10 Forums