Moving Files into a Real Folder in Xcode
Asked Answered
A

12

181

When I started my project I was happy to use Groups in Xcode rather than literal folders: Since I'm using the browser in Xcode to access everything, stuff was nicely organized and I was happy.

However, now that the project is about to be shared for version control, the project folder itself is a horror show for those trying to scan it via a terminal, about 300 files, over half of which are graphics.

I'm trying to now reorganize things, creating real folders and importing them into Xcode. Unfortunately Xcode doesn't let me work with them the way it does with groups. For example, if I right-click on an actual added folder (blue, not yellow) and choose to add existing files, it doesn't actually put them in that folder, it puts them in its root.

Similarly, I can't move a file from a faux-folder (a group) into a real folder: Xcode doesn't consider the real folders to be valid places to move stuff to.

What am I missing? How can I convince Xcode to let me use the folders the way I use groups? There's an answer here to a somewhat similar question, but it doesn't actually solve my problem since I'm working with existing files.

Affettuoso answered 10/12, 2010 at 23:24 Comment(0)
A
122

FURTHER EDITED JUNE 2017: Xcode 9 does this automatically, no special effort required. This answer and Brandon's only apply to Xcode 8 and earlier.

EDITED DECEMBER 2016: Brandon's answer below is a better solution now. Back when this answer was created in 2010 this was the only option I could find. I now suggest Brandon's answer, below.


It turns out that moving files into real folders is certainly possible, though not as simple as it should be.

I got the basic information from a question here, Xcode organising files and folders (core data model objects - iPhone), but learned important things along the way.

##The Process

Moving the files is a two-step process with multiple sub-steps:

  1. Tell Xcode where you want the files to be:
  • Right/Control-click on the file or file group that you'd like to move and choose Get Info from the contextual menu that appears. The Group Info or File Info window appears.

  • Click the Choose button on the far right side of the window in the Path area. A dialog box appears.

  • Navigate to the folder you want the files to be moved to. Create a New Folder if needed. Click the Choose button in the bottom-right corner of the dialog box, then close the Group Info/File Info window.

  • The names of the file/files in the group will turn red to indicate that Xcode can't find them in the place you specified.

  1. Move the actual files
  • In the Finder (or Git) move the files you selected in step 1 into the actual folders you want them in.

  • Switch back to Xcode. The files/groups should all have turned black again. If any are still red then you've missed moving something to the right folder.

##Tips I learned a couple of important things while adjusting the ~300 files in this project:

  • Some files refuse to move this way; that is, when you navigate to the new destination the Choose button is disabled, as is the New Folder button. The solution, though I don't know why it makes a difference, is to first use Xcode to put those files in a Group (right/control-click the files and choose Group, and give the group a name) and then move the group to the new location. After you're actually moved the files in the Finder you can remove them from the group (by dragging them into the new parent group/folder and deleting the group).

  • Stop and build every few minutes, after completing step 2 for a number of files. The build will tell you if you've screwed anything up so far, making it easier to go back and fix it before you've done too much damage.

  • If the files won't move to where you want them to — I had a devil of a time moving some files that had been created early-on in the Classes folder — you can simply drag them out of their old place in the finder to someplace handy like the desktop, delete references to them in Xcode, and then re-import them via the right/control-click Add Existing Files option.

  • If in the past you've used Xcode to delete references to files without also moving them to the trash in this project, you'll find files that don't have to be moved but at just sitting there. Be careful that you don't do what I did, confusing the names of a current group of files I was moving and the older, no-longer-linked files, insisting that Xcode import them because you thought it was being dense.
Affettuoso answered 13/12, 2010 at 1:21 Comment(10)
Having all the files in the same directory was really bugging me. Thank you for this!Garbe
you can also: rearrange the directory structure at will in the Finder, remove all the "red files" in the project, and then add them all back to the project using the "group in folders" optionConvertiplane
@Convertiplane Very true, and if all of the files in the main directory need to be in the project then that can save a lot of time. Unfortunately with projects involving multiple people or that have had a lot of releases over time, there can be any number of files there that shouldn't be added to the project. In that case you'd have to track which ones you removed in order to restore them, or something similar.Affettuoso
in xcode 4 i had to do something different - didn't see any contextual 'get info' links when right clicking. instead i had to display the identity and type view (probably a name for this that i don't know). it's the right-most button in the view options in the xcode4 header bar. i move the file on the filesystem first, and then click the weird icon next to the path, and tell xcode where the new file is.Egestion
The problem with moving files is that when it's already under SVN control, you need to make sure you use an app such as Versions or Cornerstone to move the files around... Or you'll end up with terrible version control issues! I wish Apple would just throw the "groups" idea away and have everything in real folders, a-la Eclipse...Caravette
@Caravette Great point about version control, doing it this way will likely completely blow just about any of them. In my case I was doing all of the reshuffling right before putting under shared version control so that things made more sense, but if you're using version control from the start -- which I do now -- this'll be useless.Affettuoso
Had to downvote this because the approach stopped working in XCode 4 and later versionsMohandas
Its worth mentioning that you will loose git history if you move files this way.Eastern
This works for all files except for storyboards... The folder selection is grayed out :(Isadoraisadore
I am working on both android studio and xcode for my product app development(Android and swift) and when I see the growth of both IDE in last few years xcode is seriously very slow and missed many common basic features that's really frustating .Popp
M
179

A modern (and dead simple!) approach for 2017 (Xcode 6, 7, 8, and sometimes 9, since it does it automagically some of the time):

If you're moving a bunch of files into a new folder and are keeping the child hierarchy, it's actually a lot easier than moving each file individually:

  1. Create new groups in the Xcode folder tree and organize your files into them however you like.
  2. Create a matching physical folder tree in Finder and organize your physical files into them to match what you did in step 1.
  3. All the references in Xcode should now be red (that's OK!).
  4. From the Identity and Type manager, select the Group in Xcode that you want to relocate, then click the folder icon from the info pane:

  5. In the Finder selection dialog, locate the equivalent new folder you created for this group in step 2. All the files inside that group will now be automagically rediscovered!

Isn't that nice? At most you'll have to repeat these 5 steps once for each new group you've created (which beats relocating each file individually!)

Bonus Points!

Say you accidentally screwed up the move and now a bunch of your files are red and can't be found: select multiple files that are broken, and using the same folder icon in the screenshot from step 4, find the correct folder that contains these files and they'll automatically resolve the missing paths.

Mcclendon answered 19/11, 2014 at 18:59 Comment(9)
Doing this however makes the files untracked in Git. Of course I can always readd the file but am I missing something?Causalgia
@jeraldo not sure what flavour of git you're using but I've never had that problem. You can, however, easily do git add ..Mcclendon
I'm missing something and I'm not sure what it is. Maybe I'm not understanding the meaning of the words "organize your files into them ..." in step (1)?? I assume this means "Add the files to the group by drag-and-drop or whatever". But if I do this, then of course Xcode recognizes the files and doesn't show them in red, as is stated in step (3). Any idea what I'm doing wrong?Etruscan
Did you move the physical files as in step 2?Mcclendon
Ah, I figured it out! Step (2) was just a bit too cryptic for me. If I were rewriting it, I'd say something to the effect of "Create a matching physical folder tree, then use Xcode's Show in Finder command to locate the files you added to the new Group in Step (1) and physically move those files into the folder you've just created." Something like that ...Etruscan
wha? i have a shared library and i don't want to move it each xcode app. it will not get updates to code and i'm not about to individually go through lib. there must be a way to force xcode to find the lib. anyone have a batch file that goes and copies the references?Prog
Just move the physical parent folder and relocate it within Xcode then?Mcclendon
I did this, but I'm still getting warnings 'users/directory/File.* is missing'. I could ignore the errors, since the project still builds and runs fine, but should I be getting these errors?Liddell
Try cleaning it?Mcclendon
A
122

FURTHER EDITED JUNE 2017: Xcode 9 does this automatically, no special effort required. This answer and Brandon's only apply to Xcode 8 and earlier.

EDITED DECEMBER 2016: Brandon's answer below is a better solution now. Back when this answer was created in 2010 this was the only option I could find. I now suggest Brandon's answer, below.


It turns out that moving files into real folders is certainly possible, though not as simple as it should be.

I got the basic information from a question here, Xcode organising files and folders (core data model objects - iPhone), but learned important things along the way.

##The Process

Moving the files is a two-step process with multiple sub-steps:

  1. Tell Xcode where you want the files to be:
  • Right/Control-click on the file or file group that you'd like to move and choose Get Info from the contextual menu that appears. The Group Info or File Info window appears.

  • Click the Choose button on the far right side of the window in the Path area. A dialog box appears.

  • Navigate to the folder you want the files to be moved to. Create a New Folder if needed. Click the Choose button in the bottom-right corner of the dialog box, then close the Group Info/File Info window.

  • The names of the file/files in the group will turn red to indicate that Xcode can't find them in the place you specified.

  1. Move the actual files
  • In the Finder (or Git) move the files you selected in step 1 into the actual folders you want them in.

  • Switch back to Xcode. The files/groups should all have turned black again. If any are still red then you've missed moving something to the right folder.

##Tips I learned a couple of important things while adjusting the ~300 files in this project:

  • Some files refuse to move this way; that is, when you navigate to the new destination the Choose button is disabled, as is the New Folder button. The solution, though I don't know why it makes a difference, is to first use Xcode to put those files in a Group (right/control-click the files and choose Group, and give the group a name) and then move the group to the new location. After you're actually moved the files in the Finder you can remove them from the group (by dragging them into the new parent group/folder and deleting the group).

  • Stop and build every few minutes, after completing step 2 for a number of files. The build will tell you if you've screwed anything up so far, making it easier to go back and fix it before you've done too much damage.

  • If the files won't move to where you want them to — I had a devil of a time moving some files that had been created early-on in the Classes folder — you can simply drag them out of their old place in the finder to someplace handy like the desktop, delete references to them in Xcode, and then re-import them via the right/control-click Add Existing Files option.

  • If in the past you've used Xcode to delete references to files without also moving them to the trash in this project, you'll find files that don't have to be moved but at just sitting there. Be careful that you don't do what I did, confusing the names of a current group of files I was moving and the older, no-longer-linked files, insisting that Xcode import them because you thought it was being dense.
Affettuoso answered 13/12, 2010 at 1:21 Comment(10)
Having all the files in the same directory was really bugging me. Thank you for this!Garbe
you can also: rearrange the directory structure at will in the Finder, remove all the "red files" in the project, and then add them all back to the project using the "group in folders" optionConvertiplane
@Convertiplane Very true, and if all of the files in the main directory need to be in the project then that can save a lot of time. Unfortunately with projects involving multiple people or that have had a lot of releases over time, there can be any number of files there that shouldn't be added to the project. In that case you'd have to track which ones you removed in order to restore them, or something similar.Affettuoso
in xcode 4 i had to do something different - didn't see any contextual 'get info' links when right clicking. instead i had to display the identity and type view (probably a name for this that i don't know). it's the right-most button in the view options in the xcode4 header bar. i move the file on the filesystem first, and then click the weird icon next to the path, and tell xcode where the new file is.Egestion
The problem with moving files is that when it's already under SVN control, you need to make sure you use an app such as Versions or Cornerstone to move the files around... Or you'll end up with terrible version control issues! I wish Apple would just throw the "groups" idea away and have everything in real folders, a-la Eclipse...Caravette
@Caravette Great point about version control, doing it this way will likely completely blow just about any of them. In my case I was doing all of the reshuffling right before putting under shared version control so that things made more sense, but if you're using version control from the start -- which I do now -- this'll be useless.Affettuoso
Had to downvote this because the approach stopped working in XCode 4 and later versionsMohandas
Its worth mentioning that you will loose git history if you move files this way.Eastern
This works for all files except for storyboards... The folder selection is grayed out :(Isadoraisadore
I am working on both android studio and xcode for my product app development(Android and swift) and when I see the growth of both IDE in last few years xcode is seriously very slow and missed many common basic features that's really frustating .Popp
I
104

In Xcode 5 or Xcode 6:

  1. Create the folders that map to your Groups in Finder
  2. Move the files into those folders in Finder
  3. Select each file that is red in the Xcode sidebar on the left
  4. Click the button "Show/Hide Utilities" to reveal the right sidebar (see figure)
  5. In "Identity and Type", click the tiny button and select the file location (see figure)

enter image description here

Cheers.

Invest answered 21/10, 2011 at 19:34 Comment(2)
Note to some users, if you're getting an error that changes were made after the header information was precompiled, just do a Clean of the product and things will work out!Wiseacre
I followed this approach in Xcode 5.1.1 to move a header file into a physical folder. I am able to move the file. But Xcode says "file not found" while building the project. Could you please help.Connett
D
22

I've found the most reliable way to work around XCode's appalling design here is to organize ALL your files in Finder, for two reasons:

  1. Finder doesn't make "mistakes", unlike XCode's GUI
  2. Once things are organized in Finder, you can drag/drop entire Finder folders into XCode, and ... it does exactly what you wanted, with no effort

Even when moving existing XCode files around, it is quicker to do this:

  1. Select the files in Finder
  2. Create a folder in Finder for them
  3. Drag/drop them to the folder (automatically Moves them)
  4. Drag/drop the Finder folder into Xcode (automatically: creates the Xcode folder, adds every file in the Finder folder)
  5. cmd-select every "red" file you now see in Xcode (because you moved them) and hit the delete key

NB: I never use the "create dragged folders with sub-groups" option, because in XCode 3 that was often hopelessly buggy and could corrupt projects. Apple clearly (IMHO) does NOT use this feature internally, otherwise it would never have been allowed to be so buggy; if Apple doesn't use a thing, it's generally not safe to use it either - they aren't good at testing :)

Diplostemonous answered 7/7, 2011 at 22:53 Comment(2)
I'm trying this tomorrow.. this was the simple solution that I've been looking for. Why did they make it so hard? If it just mirrored the file system it would have been far less confusing. Thanks!Chinaware
This was the fourth solution I tried and the ONLY one I could get to work with xcode 5.1.1. Also, for some reason this wouldn't work for any of my categories. I had to literally move them to Trash, take them back out of Trash, and then re-add them to xcode and finally it worked.Orv
H
21

There is a simple to setup and use Command Line Tool - "synx" available in github that do exactly what is needed here.

It reorganizes Xcode project folder in finder to match Xcode groups in project.

You can find it here: https://github.com/venmo/synx

UPDATE: XCode 9 supports this feature by default. So, no need to use other tools anymore!

Hardware answered 22/4, 2015 at 6:31 Comment(1)
Great tool but gets extremely slow in bigger projects (may take few minutes to complete the job) and seems to be not supported anymore.Desiccated
D
6

Xcode 9

It seems Xcode 9 now supports it by default. When you move files from a group to another, the file will also be moved from the old folder to the new folder. This was announced in WWDC 2017.

Defense answered 6/6, 2017 at 10:17 Comment(0)
C
3

I used the following tool to achieve it.Organize Folders in Groups Xcode

--no-default-exclusions
Caerleon answered 9/3, 2015 at 12:24 Comment(0)
A
2

I'm usually move files to the directory directly in the Finder, then fix the files with red-color names in XCode in their "Get Info" dialog: click the "Choose..." button and select file's new locatoin. That's the first response when I want to move files in XCode, and it works.

Allveta answered 28/12, 2010 at 15:20 Comment(0)
M
1

As of Xcode 4.5 when you drag in a folder structure it is automatically turned into nested groups in the Project Navigator. Then, if you look on disk (e.g. right click and choose Show in Finder), the folder structure has been retained.

I found trying to fix things from an older project was just a pain. It turned out much easier to just delete these files and drag them in again.

Mikey answered 27/11, 2012 at 8:35 Comment(0)
O
1

To move a folder in xcode 4.5 I just...

  1. Delete the files/groups from xcode and select "Remove Reference".
  2. Go into finder and move the folder/files as needed.
  3. Once done I go back into Xcode and choose File->Add Files to {ProjectName}.
  4. Make sure "Copy Items into Destination Group's folder (If needed)" is checked
  5. Make sure "Create Group for any added folder" is checked
  6. Make sure "Add to Target" is checked for your project
Oldster answered 13/6, 2013 at 11:32 Comment(0)
M
1

Easiest technique for XCode 8....

Assuming you have files A,B,C in a logical group, but want them moved into a folder on your hard drive.

  1. Create a destination folder via Finder
  2. Add the folder in XCode (File -> Add Files ) using the Options pane to select a Folder Reference (not a group)
  3. Drag files A,B,C from the (old) group to the (new) folder reference in the project navigator. XCode will move the files into the folder, both in the the project, and on disk.

Done.

Mohandas answered 27/7, 2017 at 17:49 Comment(0)
K
0

On XCode 11 (I just validated this on XCode 11.6), you can select the files you would like to move under the project navigator, right click on the selected files, and click "New Group From Selection". This will create a folder on disk and move the files appropriately.

Kwangchowan answered 8/8, 2020 at 5:58 Comment(1)
It's again make group of group!Oralee

© 2022 - 2024 — McMap. All rights reserved.