Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » IResource.copy()
IResource.copy() [message #97121] Sat, 19 July 2003 12:00 Go to next message
Eclipse UserFriend
Hello,
I am trying to copy a selected Resource to another location inside the
project. I always get an error message, claiming that the directory, where
I would like to copy the Resource to (it is a file), does not exist. I
thought, that the copy also creates the directory. It does not seem like.
Any help therefor? I think this should be pretty easy anyway, but I cannot
make it.

See the coding from my run Method:

IPath targetPath = null;
IPath srcPath = null;
IPath projectPath = null;
IProject project = null;

IResource[] resources = this.getSelectedResources();

/* loop over given resources and determine the path and the
corresponding project */
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];

/* determine path of the resource and the project */
srcPath = resource.getProjectRelativePath();

project = resource.getProject();
projectPath = project.getFullPath();

/* build targetPath */
targetPath = new Path(projectPath + "/tmp/" + srcPath);

try {
resource.copy(targetPath, true, null);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}

Any help is greatly appreciated.
Re: IResource.copy() [message #97168 is a reply to message #97121] Sat, 19 July 2003 14:47 Go to previous messageGo to next message
Eclipse UserFriend
So you should create the directory first ...

Markus M. May wrote:
> Hello,
> I am trying to copy a selected Resource to another location inside the
> project. I always get an error message, claiming that the directory, where
> I would like to copy the Resource to (it is a file), does not exist. I
> thought, that the copy also creates the directory. It does not seem like.
> Any help therefor? I think this should be pretty easy anyway, but I cannot
> make it.
>
> See the coding from my run Method:
>
> IPath targetPath = null;
> IPath srcPath = null;
> IPath projectPath = null;
> IProject project = null;
>
> IResource[] resources = this.getSelectedResources();
>
> /* loop over given resources and determine the path and the
> corresponding project */
> for (int i = 0; i < resources.length; i++) {
> IResource resource = resources[i];
>
> /* determine path of the resource and the project */
> srcPath = resource.getProjectRelativePath();
>
> project = resource.getProject();
> projectPath = project.getFullPath();
>
> /* build targetPath */
> targetPath = new Path(projectPath + "/tmp/" + srcPath);
>
> try {
> resource.copy(targetPath, true, null);
> } catch (Exception ex) {
> System.out.println(ex.getMessage());
> }
>
> Any help is greatly appreciated.
>
Re: IResource.copy() [message #97226 is a reply to message #97168] Sat, 19 July 2003 17:39 Go to previous messageGo to next message
Eclipse UserFriend
I thought so. Now i am creating an IFolder and create the directory with
this handler. Anyway, the directory is still not created. I believe that I
have somewhere a deep misunderstanding of this stuff.

Here is now my new coding:

IPath targetPath = null;
IPath srcPath = null;
IPath projectPath = null;
IProject project = null;

IResource[] resources = this.getSelectedResources();

/* loop over given resources and determine the path and the
corresponding project */
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];

/* determine path of the resource and the project */
srcPath = resource.getProjectRelativePath();

project = resource.getProject();
projectPath = project.getFullPath();

/* build targetPath */
targetPath = new Path("tmp/" + srcPath);

try {
String targetFolder = targetPath.toFile().getParent();
IFolder folder = resource.getProject().getFolder(targetFolder);
folder.create(true, true, null);

resource.copy(folder.getLocation(), true, null);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}

what i am trying to do is to copy .java files from there original file to
another directory "tmp/..." in the project. are there any hints or
examples on how to do something like this?

regards

Genady wrote:

> So you should create the directory first ...

> Markus M. May wrote:
> > Hello,
> > I am trying to copy a selected Resource to another location inside the
> > project. I always get an error message, claiming that the directory, where
> > I would like to copy the Resource to (it is a file), does not exist. I
> > thought, that the copy also creates the directory. It does not seem like.
> > Any help therefor? I think this should be pretty easy anyway, but I cannot
> > make it.
> >
> > See the coding from my run Method:
> >
> > IPath targetPath = null;
> > IPath srcPath = null;
> > IPath projectPath = null;
> > IProject project = null;
> >
> > IResource[] resources = this.getSelectedResources();
> >
> > /* loop over given resources and determine the path and the
> > corresponding project */
> > for (int i = 0; i < resources.length; i++) {
> > IResource resource = resources[i];
> >
> > /* determine path of the resource and the project */
> > srcPath = resource.getProjectRelativePath();
> >
> > project = resource.getProject();
> > projectPath = project.getFullPath();
> >
> > /* build targetPath */
> > targetPath = new Path(projectPath + "/tmp/" + srcPath);
> >
> > try {
> > resource.copy(targetPath, true, null);
> > } catch (Exception ex) {
> > System.out.println(ex.getMessage());
> > }
> >
> > Any help is greatly appreciated.
> >
Re: IResource.copy() [message #97626 is a reply to message #97226] Tue, 22 July 2003 15:31 Go to previous message
Eclipse UserFriend
Originally posted by: John_Arthorne.oti.com_

This part is wrong:

String targetFolder = targetPath.toFile().getParent();
IFolder folder = resource.getProject().getFolder(targetFolder);

The "targetFolder" variable is holding a fully qualified file system
path for the folder. Then you pass this to getFolder, which expects a
project-relative path. You should instead use:

IContainer folder = resource.getParent();

Presumably it is possible that the parent's parent doesn't exist either,
so you probably need a recursive parent creation method... something
like this:

protected void create(IResource resource) throws CoreException {
if (resource.exists())
return;
create(resource.getParent());
switch (resource.getType()) {
case IResource.FOLDER :
((IFolder) resource).create(IResource.NONE, true, null);
break;
case IResource.PROJECT :
((IProject) resource).create(getMonitor());
((IProject) resource).open(getMonitor());
break;
}
}
--


Markus M. May wrote:
> I thought so. Now i am creating an IFolder and create the directory with
> this handler. Anyway, the directory is still not created. I believe that I
> have somewhere a deep misunderstanding of this stuff.
>
> Here is now my new coding:
>
> IPath targetPath = null;
> IPath srcPath = null;
> IPath projectPath = null;
> IProject project = null;
>
> IResource[] resources = this.getSelectedResources();
>
> /* loop over given resources and determine the path and the
> corresponding project */
> for (int i = 0; i < resources.length; i++) {
> IResource resource = resources[i];
>
> /* determine path of the resource and the project */
> srcPath = resource.getProjectRelativePath();
>
> project = resource.getProject();
> projectPath = project.getFullPath();
>
> /* build targetPath */
> targetPath = new Path("tmp/" + srcPath);
>
> try {
> String targetFolder = targetPath.toFile().getParent();
> IFolder folder = resource.getProject().getFolder(targetFolder);
> folder.create(true, true, null);
>
> resource.copy(folder.getLocation(), true, null);
> } catch (Exception ex) {
> System.out.println(ex.getMessage());
> }
>
> what i am trying to do is to copy .java files from there original file to
> another directory "tmp/..." in the project. are there any hints or
> examples on how to do something like this?
>
> regards
>
> Genady wrote:
>
>
>>So you should create the directory first ...
>
>
>>Markus M. May wrote:
>>
>>>Hello,
>>>I am trying to copy a selected Resource to another location inside the
>>>project. I always get an error message, claiming that the directory, where
>>>I would like to copy the Resource to (it is a file), does not exist. I
>>>thought, that the copy also creates the directory. It does not seem like.
>>>Any help therefor? I think this should be pretty easy anyway, but I cannot
>>>make it.
>>>
>>>See the coding from my run Method:
>>>
>>> IPath targetPath = null;
>>> IPath srcPath = null;
>>> IPath projectPath = null;
>>> IProject project = null;
>>>
>>> IResource[] resources = this.getSelectedResources();
>>>
>>> /* loop over given resources and determine the path and the
>>>corresponding project */
>>> for (int i = 0; i < resources.length; i++) {
>>> IResource resource = resources[i];
>>>
>>> /* determine path of the resource and the project */
>>> srcPath = resource.getProjectRelativePath();
>>>
>>> project = resource.getProject();
>>> projectPath = project.getFullPath();
>>>
>>> /* build targetPath */
>>> targetPath = new Path(projectPath + "/tmp/" + srcPath);
>>>
>>> try {
>>> resource.copy(targetPath, true, null);
>>> } catch (Exception ex) {
>>> System.out.println(ex.getMessage());
>>> }
>>>
>>>Any help is greatly appreciated.
>>>
>>
>
>
Previous Topic:Re: How to get an IDocument from an IResource? Need to translate lineNo to Char_start
Next Topic:Exporting features
Goto Forum:
  


Current Time: Sun May 11 11:41:11 EDT 2025

Powered by FUDForum. Page generated in 0.03505 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top