Announcement

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

  • Unrecognized characters in filename when importing using: "local myfiles: dir "directory" files "*.csv"

    Hello!

    I have a file named "abc+def+mm+dd+yyyy.csv" in directory "$path/survey responses". The file is being downloaded directly from Qualtrics and I want to not have to change the file name.

    However, when I run the following code:
    Code:
    local myfilelist : dir "$path/survey responses" files "*.csv"
    foreach file of local myfilelist {
        
        di in red "`file'"
        import delimited using "`file'" , clear varnames(1)
    }
    the code is unable to find the correct file to import because this line of code "local myfilelist : dir "$path/survey responses" files "*.csv"" removes the plus signs and interprets it as "abc def mm dd yyyy.csv".
    Is there anything I can do to solve this?
    Perhaps, is there another way I can import an arbitrary file name from a specific directory?

    Thanks for the help everyone!



  • #2
    I cannot replicate the problem you are having in my setup:
    Code:
    . dir *.csv
       0.3k   1/02/23 15:53  abc+def+mm+dd+yyyy.csv
       1.6k   4/09/16 10:37  abortions_by_state_2009-2012.csv
       0.0k  11/19/14  4:20  as_strings.csv    
       0.1k   9/23/17 10:35  commatest.csv     
      42.9k  12/30/21  9:39  dca.csv           
       0.0k  11/19/14  4:20  formatted.csv     
      10.3k   1/26/18  4:50  SAT.2004.csv      
       6.6k  11/30/15  6:36  state_table.csv   
       0.3k  12/10/18  7:55  test.csv          
       0.0k  11/19/14  4:20  unformatted.csv   
    
    .
    . local myfilelist: dir "." files "*.csv"
    
    . display `"`filelist'"'
    
    
    .
    . foreach file of local myfilelist {
      2.     display in red "`file'"
      3.     import delimited using "`file'", clear varnames(1)
      4. }
    abc+def+mm+dd+yyyy.csv
    (encoding automatically selected: UTF-8)
    (15 vars, 4 obs)
    abortions_by_state_2009-2012.csv
    (encoding automatically selected: ISO-8859-1)
    (6 vars, 56 obs)
    as_strings.csv
    (encoding automatically selected: UTF-8)
    (1 var, 3 obs)
    commatest.csv
    (encoding automatically selected: ISO-8859-1)
    (5 vars, 2 obs)
    dca.csv
    (encoding automatically selected: ISO-8859-1)
    (10 vars, 750 obs)
    formatted.csv
    (encoding automatically selected: UTF-8)
    (1 var, 3 obs)
    sat.2004.csv
    (encoding automatically selected: ISO-8859-1)
    (51 vars, 61 obs)
    state_table.csv
    (encoding automatically selected: ISO-8859-1)
    (17 vars, 56 obs)
    test.csv
    (encoding automatically selected: UTF-8)
    (15 vars, 4 obs)
    unformatted.csv
    (encoding automatically selected: UTF-8)
    (1 var, 3 obs)
    I am running Stata version 17 on Windows 10.

    Comment


    • #3
      There is a difference between post #1 and post #2.

      The dir returns only the filename portion, you need to include $path in your import command. The example in post #2 worked because the data was in the current working directory.
      Code:
      . local myfilelist : dir "~/Downloads" files "*.png"
      
      . macro list _myfilelist
      _myfilelist:    "ugreen.png" "Better World Books.png"

      Comment


      • #4
        Hello! Thank you Clyde. This is very interesting-
        This is the output I received at first --
        Code:
        . local myfilelist : dir "$path/survey responses" files "*.csv"
        
        . foreach file of local myfilelist {
          2.         di in red "`file'"
          3.         import delimited using "`file'" , clear varnames(1) 
          4. }
        abc def mm dd yyyy.csv
        file abc def mm dd yyyy.csv not found
        Then, when I made these changes to make my code look more like yours:
        Code:
        . version 17
        . cap cd "$path/survey responses"
        
        . dir "*.csv"
           2.2k   1/02/23 14:11  ABC+def+Mm+dd+yyyy.csv
        
        . local myfilelist : dir . files "*.csv"
        
        . foreach file of local myfilelist {
          2.         di in red "`file'"
          3. 
          4.      import delimited using "`file'" , clear varnames(1) 
          5. }
        abc+def+mm+dd+yyyy.csv
        (encoding automatically selected: ISO-8859-1)
        (22 vars, 4 obs)
        Not quite sure why it works now, but it seems to be. Thank you. I'm also working on version 17 windows 10. It is possible that earlier I was working on windows 14/15 since I may have ran a do-file wit the "version 14" command.

        Thank you for your help @ClydeSchechter

        Comment

        Working...
        X