Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

urgent: how to move file on different disks

Posted on 2011-10-10
10
Medium Priority
?
243 Views
Last Modified: 2012-05-12
Hi there,

I have a file on disk a and i want to move it to disk b . Is there a function in "C" to move from disk a to b without having to copy the file over ?

Thanks
0
Comment
Question by:zizi21
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:NotLogical
ID: 36946310
Hi there,

Your question is missing a few details - so my answer will be general.

Since you are moving a file, the file needs to be copied and will exist briefly in both places while the operating system performs the "move" operation for you. Once the new copy is created successfully (on "b"), the original file (on "a") gets deleted.

Now, this is only true when the locations "a" and "b" are physically different disks or partitions. When a file is moved between location "a" and "b" on the same disk and partition, the operation is performed quickly without moving the file itself: only pointers within file system tables are updated to reflect the new logical location of the file.

When you include stdio.h on a Un*x system, you can use the int rename(const char *old, const char *new) call. Under Win32, you can use the BOOL WINAPI MoveFile(  __in  LPCTSTR lpExistingFileName,  __in  LPCTSTR lpNewFileName ) call from WinBase.h.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36946327
If NotLogical's answer doesn't meet your needs, please explain more about what you are trying to accomplish.  What is the context?

Otherwise, I think NotLogical's answer is very good.  You can't "move" a file between file systems without actually copying it first.  On the same filesystem, all that's needed is updating the directory information (super fast).
0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 800 total points
ID: 36946800
What do you actually mean by "move a file"?
File has a physical presence on a disc and has an entry in the file table - file might occupy many blocks on the filesystem.
Now when you want to move this file from disk a to disc b, all the data blocks related to this file on disc a will have to be copied to disc b. Also, the entry in the filetable will have to be copied.

So, move is nothing but a copy+delete operation.

And there is no standard library function in C which can do this - You will have to write your own function to do it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36949467
@ssnkumar, I agree that a copy is necessary when going from one file system to another.  However, when going within the same file system, you can use

link ( SourceFileName, DestFileName );
unlink ( SourceFileName );

There's a caveat on the main page for link in that it won't follow symbolic links.  
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 36950141
@hmccurdy: The question is about moving a file from disk a to disk b.
So, question of moving file within the same filesystem does not arise here.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36950420
I know.  I just wanted to make it very clear to the author that the same file system and different file systems are two very different issues.  He seemed to want to do a same file system operation on different file systems.

The motive behind clarification comes from adult learning theory.  (I've taken a class in it and my wife is an expert in the field. I'm not going to try to explain beyond that since this is the sort of thing where you get college credit and I can't put a semester into this box.)  In short, my concern is the author has to unlearn something and that's really difficult thus I wasn't to be very clear.
0
 

Author Comment

by:zizi21
ID: 36959805
I am trying to move a file from one disk to another disk. However, rename only works if the two files are in the same disk . One option could be to copy the file over which is going to take time.

I am trying to find a way that i could call system call in C programming so that i can use the mv function of linux which would make things easier.
0
 
LVL 13

Accepted Solution

by:
Hugh McCurdy earned 1200 total points
ID: 36959845
Let's say you want to copy file "foo" to /media/sdb1/repository

Here's a simple approach if you are confident of not overflowing the buffer.


char Command [ 80 ];
...
sprintf ( Command, "mv %s %s", "foo", "/media/sdb1/repository" );
system ( Command );

Open in new window

If you are worried about buffer overflow, you can use snprintf() instead and check the result.  Of course, you'd replace my constants "foo" and "/media/sdb1/repository" with whatever variables you are using.  (Of course, if they are constants, you don't need to worry about overflow.)

Any questions?
0
 

Author Comment

by:zizi21
ID: 36959879
Thanks a million.
Someone just informed me that moving files in unix is an expensive operation. Is this true ?
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36959895
Moving files from one filesystem to another is the same as a copy and a remove.  Expense depends on file size and speed of the drives.  (If you are writing a large file to an Android via USB, this could take a while).  Real time time used also depends on system load.

I might give a better answer if I knew the file size and had some idea of system load (and perhaps some idea about the capability of the computer).
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question