Announcement

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

  • Renaming files in directories using shell commands

    Dear all

    Is it possible to rename a file without entering in its directory beforehand using cd? I cannot seem to run the following:

    Code:
    !mv "`c(sysdir_plus)'file1.do" "`c(sysdir_plus)'file2.do"
    I have found previous posts regarding similar issues but there does not seem to be a clear definitive answer on the topic. I would prefer not to use any user-written command to achieve what i want.

    Thank you.

  • #2
    Yes, it is possible.
    Code:
    . !ls -l "`c(sysdir_plus)'"test*
    
    -rw-r--r--  1 lisowskiw  staff  16570 Jul 26 09:46 /Users/lisowskiw/Library/Application Support/Stata/ado/plus/test
    
    . !mv "`c(sysdir_plus)'test" "`c(sysdir_plus)'test2"
    
    
    . !ls -l "`c(sysdir_plus)'"test*
    
    -rw-r--r--  1 lisowskiw  staff  16570 Jul 26 09:46 /Users/lisowskiw/Library/Application Support/Stata/ado/plus/test2
    Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    I will add that I am surprised at the command you are running, because most files within the sysdir_plus directory exist within a subdirectory whose name is the first letter of the filename.

    Comment


    • #3
      William i am not sure how the FAQ is relevant here. I provided the exact fragment of code that i used and it is the same as yours. Only in my case it does not work and by this i mean that it simply says that the file does not exist (when in fact it does). I also do have permissions.

      Comment


      • #4
        Originally posted by Cynthia Inglesias View Post
        Only in my case it does not work and by this i mean that it simply says that the file does not exist (when in fact it does).
        You didn't include "what Stata typed (or did) in response" in your post. That means copying and pasting the output from Stata, not explaining in your own words what you observed.

        Comment


        • #5
          In addition to Friedrich's response, let me add that in post #1 you said only that you "cannot seem to run" the command, and left it to us to guess that that means "it simply says that the file does not exist". That is why the FAQ is relevant: you did not tell us "exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!" which as Friedrich pointed out, the FAQ recommends you do by copying and pasting output from Stata so that we also can observe everything you observed exactly as Stata presented it.

          i will also point out that post #1 refers to previous posts regarding similar issues, but leaves it to the reader to imagine what posts you refer to and what precisely the issue is.

          Again, the more you help others understand your problem, the more likely others are to be able to help you solve your problem. And the FAQ, especially sections 9-12, gives guidance on how best to do so.

          Running the following code will tell you whether your file exists in the directory you tell Stata that it is in.
          Code:
          !ls "`c(sysdir_plus)"
          !ls "`c(sysdir_plus)'file1.do"

          Comment


          • #6
            For a completely-Stata solution, there is an undocumented command named _renamefile available in Stata 14 and later which does just what it says -- renames a file. It is a bit different from Unix's mv command in that it truly is just a rename -- it won't move a file onto a different filesystem as mv would.

            Example usage:

            _renamefile somenameidontlike.txt somenameilove.txt

            Comment


            • #7
              Alan Riley (StataCorp) This is very useful indeed. I think StataCorp should make it an official command like erase etc.

              Comment

              Working...
              X