Announcement

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

  • Getting operating system for the if command

    Hi,

    I would like to identify operating system and use the value for the if command. So my pseudocode would look like that:

    PHP Code:
    if operating_system == "Mac OS X" then
     begin
       
    use "path to the data file on mac"
       
    cd "working directory on mac"
     
    end
    else
     
    begin
       
    use "path to the data file on windows"
       
    cd "working directory on windows"
     
    end 

    I keep on using same do files at work (Windows) and at home (Mac) and I have different paths for working directories and data files. I would like to solve it once instead of selecting appropriate files or running separate lines of code here and there. Ideally, I would develop one do file where I would define number of paths as global macros and then used them (like for spmap (ssc) support files, etc.)
    Last edited by Konrad Zdeb; 17 Aug 2014, 14:15.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    use "c(os)" which can be found by typing -h creturn- and clicking on "system values"

    Comment


    • #3
      Also, a lazy/sloppy approach would be simply to put "capture" in front of your use and cd commands. If you're on the wrong OS, it will fail, but fail gracefully and move on to the next; whichever works, will work. Doing it right is nice, but so is keeping it simple.

      Comment


      • #4
        Originally posted by ben earnhart View Post
        Also, a lazy/sloppy approach would be simply to put "capture" in front of your use and cd commands. If you're on the wrong OS, it will fail, but fail gracefully and move on to the next; whichever works, will work. Doing it right is nice, but so is keeping it simple.
        Thanks, this would be actually quite neat. In effect, I often use the same files stored in my Dropbox folder. Just paths to the Dropbox folder will differ depending on the laptop/OS I'm using at the moment. On the side, using PHP tag in post made me realise that it would nice to have similar syntax highlighting for the chunks with Stata code.
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          Originally posted by Konrad Zdeb View Post

          Thanks, this would be actually quite neat. In effect, I often use the same files stored in my Dropbox folder. Just paths to the Dropbox folder will differ depending on the laptop/OS I'm using at the moment. On the side, using PHP tag in post made me realise that it would nice to have similar syntax highlighting for the chunks with Stata code.

          I had the exact problem as you.

          In your profile.do , just set a global $DROPBOX = `" ... "' where ... is the dropbox path. Do this for every computer, and use that global whenever you load a dataset (or logfile, dofile, etc)

          Comment


          • #6
            Sergio,

            thanks for your useful suggestion. It suits me perfectly
            Kind regards,
            Konrad
            Version: Stata/IC 13.1

            Comment


            • #7
              It also crosses my mind, that it would be need if the one could use some reference for special folders in the path. In effect, I would be happy with: "$user_folder\Dropbox\Analysis on This and That\Stata".
              Kind regards,
              Konrad
              Version: Stata/IC 13.1

              Comment


              • #8
                Originally posted by Konrad Zdeb View Post
                It also crosses my mind, that it would be need if the one could use some reference for special folders in the path. In effect, I would be happy with: "$user_folder\Dropbox\Analysis on This and That\Stata".
                That's called USERPROFILE on Windows systems:
                Code:
                . display `"`:environment USERPROFILE'"'
                C:\Users\Mightyuser
                Best, Sergiy Radyakin

                Comment


                • #9
                  Shame that it doesn't work on Mac. I would like to return different paths depending on the OS where do file is executed. But suggestion with global macro works for me fine.
                  Kind regards,
                  Konrad
                  Version: Stata/IC 13.1

                  Comment


                  • #10
                    I would be surprised if Mac/Unix didn't have something similar. You should be able to get the user name, which can be used to build the full path using some platform-specific template. We have recently ran a platform survey. There were quite a few users on Macs, perhaps they can help you with platform-specific issues.

                    PS:Lack of facilities to test do/ado files for cross-platform compatibility is impeding development of more automated tools.

                    Best, Sergiy Radyakin

                    Comment


                    • #11
                      Originally posted by Konrad Zdeb View Post
                      Shame that it doesn't work on Mac.
                      On OS X/Linux, your Dropbox folder is (by default) located at
                      ~/Dropbox

                      Comment


                      • #12
                        Going on from Rich's suggestion:
                        Code:
                        if c(os)=="MacOSX" {
                        do something
                        }
                        Steve Samuels
                        Statistical Consulting
                        [email protected]

                        Stata 14.2

                        Comment

                        Working...
                        X