Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Identifying computer based on host name and set path to specific directory

    Dear all,


    Probably a silly question for many of you. Currently I manage three different computers in three different places. I want something on the top of my *do files which will identify the computer based on the computer name `c(hostname)' and then set path to certain directory. Currently I write the paths on the top of each *.do file and write a comment to identify which path is for which computer (say, home-pc, work-psy, work-bio etc) . The problem is if I am to work with more number of computers (say 7 or 8, I am going to ), under current practice, I will have to eyeball and identify the name of the computer in the dofile-comments to find out the computer in order to set the directory path. I am wondering of writing a loop which will do the work for me based on the computer name. I tried with the following code and no change is made to the path. I might be doing something silly and there might be more efficient way of achieving this. Have a look please my codes below:


    Code:
     pwd // Current directory
    
    /Users/Rom
    
    // Change path based on the computer name:
    
        if "`c(hostname)'" == "Roms-MBP" {
                cd "/Users/Rom/Desktop/MY_FILES/1.PROFESSIONAL/UNI/EB/REE"
                }
        if "`c(hostname)'" == "CZC6058HTY" {
                cd "C:/Users/mm597/UNI/EB/REE"
                }
        if  "`c(hostname)'" == "bio-bs20895" {
                cd "/Users/mm597/UNI/EB/REE"  
                }
    
     pwd
    
    /Users/Rom //No change is made to the directory path

    The middle one is a Windows (OS-Windows 7, 64bit) computer and the two others (first and the third) are (Mac-OSX, version 10.10.5). Any suggestion is appreciated.

    All the best
    Roman

  • #2
    I like your idea, and it works fine on my machine (Win 7), for what it's worth. -Mike

    Comment


    • #3
      Intersting Mike. It is not working in my computer . I am on a mac now and as I mentioned after I run the commands, no changes appear in the directory paths. I am wondering, I perhaps have not made it explicit in the original post that codes are not working in my computer !!
      Roman

      Comment


      • #4
        Just a wild guess, here, Roman. Perhaps the pathnames on Mac are case sensitive?

        Comment


        • #5
          But I use the same path with simple one line command and that work (still working with the same onel line command) i.e.

          Code:
           
           cd "/Users/Rom/Desktop/MY_FILES/1.PROFESSIONAL/UNI/EB/REE"
          It is just when they are in this loop with the `c(hostname)' commands in a loop, nothing happens. You can see the last line, -pwd- still showing the directory where I was. The same thing happend earlier at work when the codes fail me to change from server directory to hard drive directory where I have admin right. My first thought was there is something wrong in my codes or defining the host name (thought it is pretty simple and straight).Since none of you have raised any issue with my codes , I now feel confident that the codes are perhaps okay. Problem is somewhere else. Thanks for jumping in and providing me the confidence with my codes.
          Roman

          Comment


          • #6
            Have you confirmed that c(hostname) returns what you expect it to on your OS X systems? On mine, running Stata 14.1, I get the following:
            Code:
            . about
            
            Stata/SE 14.1 for Mac (64-bit Intel)
            Revision 03 Mar 2016
            Copyright 1985-2015 StataCorp LP
            
            . display "`c(hostname)'"
            MacBook-Air-B.local
            
            .

            Comment


            • #7
              Thanks William. Please ee the current one I am on:

              Code:
              di "`c(hostname)'"
              Roms-MBP.default
              I guess they are okay unless counter intuitively that ".default" forms part of the host name which is not included in my code.
              Roman

              Comment


              • #8
                Code:
                if `"`c(username)'"' == "mm597" cd `"`: env HOME'/UNI/EB/REE/"'
                else cd `"~/Desktop/MY_FILES/1.PROFESSIONAL/UNI/EB/REE/"'
                The extended macro function : environment will allow you to access environmental variables on the system which should point to either C:/Users/mm597, /Users/mm597, or /home/mm597 for non-*nix based OS, OSX, and/or Linux/Unix.

                Comment


                • #9
                  Since c(hostname) returns "Roms-MBP.default" then your code must be
                  Code:
                  if "`c(hostname)'" == "Roms-MBP.default" {
                  Be sure to check the value returned by c(hostname) on your other OS X system; it probably has a domain other than ".default".

                  In the System Preferences for Sharing, you can see at the top of the dialog box the "Computer Name" for your system, and below that you can see a note that says "Computers on your local network can access your computer at:" followed by the hostname.
                  Last edited by William Lisowski; 07 Apr 2016, 14:10.

                  Comment


                  • #10
                    Ah !!! How silly I was to think that ".default'' is not part of the name !! Thanks William the problem is resolved. Wbuchanan, thanks for you comment too. Have not tried it but definitely another way to go ahead.

                    Many thanks everyone for providing your thoughts.
                    All the best,
                    Roman

                    Comment


                    • #11
                      I have a similar question to Roman's. I am trying to create code that identifies the current user and plugs that into a directory path.

                      For example, at the beginning of a do-file, I set the directory
                      Code:
                      cd "C:\Users\Laura\Google Drive\BRFSS\2015 BRFSS"
                      I want to Stata to identify the username, Laura, save it in a macro and then insert it into the above path.

                      I have been able to do this successfully in separate steps but is there a way to get it into one line?
                      Code:
                      cd "C:\Users"
                      cd `c(username)'
                      cd "Google Drive\BRFSS\2015 BRFSS"
                      I'd like to make this also work with the code below.
                      Code:
                      import sasxport "C:\Users\Laura\Google Drive\BRFSS\XPT Files\LLCP2015.XPT", clear

                      Comment


                      • #12
                        Have you tried the solutions proposed above? Does something like:

                        Code:
                        loc path `"C:/Users/`c(username)'/Google Drive/BRFSS/XPT Files"'
                        import sasxport `"`path'/LLCP2015.XPT"', clear
                        not work for your purposes?

                        Comment


                        • #13
                          I would go with :environment as mentioned in #8 by william.
                          Code:
                          cd "`:environment USERPROFILE'/Google Drive/BRFSS/XPT Files"
                          import sasxport "LLCP2015.XPT", clear

                          Comment


                          • #14
                            Thank you. Oded and William. I am not sure what the advantages are of either method compared to the other, but both approaches work!

                            Comment


                            • #15
                              Originally posted by Laura Andrews View Post
                              I have a similar question to Roman's. I am trying to create code that identifies the current user and plugs that into a directory path.

                              For example, at the beginning of a do-file, I set the directory
                              Code:
                              cd "C:\Users\Laura\Google Drive\BRFSS\2015 BRFSS"
                              I want to Stata to identify the username, Laura, save it in a macro and then insert it into the above path.

                              I have been able to do this successfully in separate steps but is there a way to get it into one line?
                              Code:
                              cd "C:\Users"
                              cd `c(username)'
                              cd "Google Drive\BRFSS\2015 BRFSS"
                              I'd like to make this also work with the code below.
                              Code:
                              import sasxport "C:\Users\Laura\Google Drive\BRFSS\XPT Files\LLCP2015.XPT", clear
                              capture confirm file "C:/Users/`c(username)'/Downloads/COVID booking form.xlsx"

                              in short: switch to forward slashes

                              Comment

                              Working...
                              X