How can I add an existing folder structure to my visual studio project (python tools)?

Miebster picture Miebster · Oct 2, 2012 · Viewed 11.7k times · Source

This question is essentially the same as these:

How do I add an existing directory tree to a project in Visual Studio? How to "Add Existing Item" an entire directory structure in Visual Studio?

Except the solutions don't work for me.

It looks like another user had the exact same problem

http://pytools.codeplex.com/discussions/249455

But http://xkcd.com/979/ has struck again.

I am using Visual Studio 2010 with Python Tools for Visual Studio.

In this project users create new folders and code and commit them to SVN. Another user will update SVN and the new files and folders will show up in windows explorer. The user then needs an easy way to add these folders and files to the solution.

Putting the solution in SVN so added folders can be added to the solution before committing is not an option. The solution is controlled in a separate area from the source.

Suggested Solution:

https://stackoverflow.com/a/392477/606660

Won't work because:

The "Show all files" button in the solution explorer does not show up.

Suggested Solution:

https://stackoverflow.com/a/57778/606660

Won't work because:

When I drag the folder onto the solution explorer pane the location the folder is dropped affects where in the solution it is nested. If it gets dropped in the wrong folder, it shows up as a folder with the expected name and the expected contents. This is really close to what we want, except its in the wrong folder (since I dropped it on the wrong folder, intentionally). If the folder is dragged to the correct location, it appears as a file with an exclamation point. When you double click the "file" it says

"The item <folder name> does not exist in the project directory.  It may have been moved, renamed, or deleted"

I believe this is because VS will try to create a copy of the folder in the directory that you drag it to. If I move the folder out of my project entirely (say to the desktop) and then drag it into the solution explorer at the proper location, it shows up as a folder in the project. A copy of the folder is also created on disk at the location specified by the drop, with the same name and contents.

So it appears that dragging and dropping a folder onto the solution explorer will create a copy of the folder on the disk in the location that you targeted in your solution when you dropped it. If that location already has a folder of that name, the folder gets imported as a file.

My Solution

I'm using PyCharm instead, its much better.

Answer

Markus Jarderot picture Markus Jarderot · Oct 2, 2012

If nothing else works, you could add the files and folders manually in the .pyproj-file. The format is simple:

<ItemGroup>
    <Compile Include="File1.py" /> <!-- List of files relative to .pyproj folder -->
    <Compile Include="test\file2.py" />
</ItemGroup>
<ItemGroup>
    <Folder Include="test\" /> <!-- List of folders -->
</ItemGroup>

You can add more <ItemGroup> elements if you want, and you can mix files and folder if you wish.

Script to generate the XML:

import os

def visit(folder):
    for fn in os.listdir(folder):
        filename = os.path.join(folder, fn)
        _, ext = os.path.splitext(fn)
        if os.isdir(filename):
            folders.append(filename)
            visit(filename)
        elif ext.lower() == '.py':
            files.append(filename)

files = []
folders = []

visit('.')

print '<ItemGroup>'
for fn in files:
    print '  <Compile Include="' + fn + '"/>'
print '</ItemGroup>'

if folders:
    print '<ItemGroup>'
    for fn in folders:
        print '  <Folder Include="' + fn + '\\"/>'
    print '</ItemGroup>'