sravan008 has asked for the wisdom of the Perl Monks concerning the following question:

Hello every one , can any one tell me how to rename a file in a directory , after renaming a the file we have to move this file in another directory ?? example : c:\ksk\vista\sra.txt after renaming ne_sra.txt_new place in different directory :c:\home\ hopefully u understand my requirement guys: i wrote sample code but its not working , if u want i will paste it here , but please respond on my query Thanks ksk
  • Comment on renaming a file and moving into another folder

Replies are listed 'Best First'.
Re: renaming a file and moving into another folder
by choroba (Cardinal) on Sep 21, 2011 at 16:30 UTC
    See rename for both renaming and moving, or File::Copy for moving to a greater distance.
Re: renaming a file and moving into another folder
by ikegami (Patriarch) on Sep 21, 2011 at 19:49 UTC
    rename("c:\\ksk\\vista\\sra.txt", "c:\\home\\ne_sra.txt") or die $!;
Re: renaming a file and moving into another folder
by zentara (Archbishop) on Sep 21, 2011 at 17:57 UTC
    I am sorry that I got long winded here

    But all the schoolbooks show the hard drive to be a big file cabinet, with multiple drawers, labeled A- Z. Did my teachers lie to me ? :-)

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      But all the schoolbooks show the hard drive to be a big file cabinet, with multiple drawers, labeled A- Z. Did my teachers lie to me ? :-)
      No. I wouldn't say that they "lied". I would say that they simplified things at the beginning and you may have missed the reality checkpoints later in the curricula. Please do not get offended. This misconception is common. In Unix you should look at the link count associated with a file.

      Basically I am saying that the file system directory associates a text name with essentially a starting byte number on the hard drive. There can be more than one text name associated with the same byte number on the hard drive. These are called "hard links".

      So, I would say that it is a pretty good approximation to say that the hard disk gets divided up into blocks. A file is just a name in the directory that points to a sequence of these blocks. More than one file name can point to the same sequence of blocks. These blocks on the hard drive are not reused until the reference count to them drops to zero (this just like Perl reference counting).

      So, a more accurate description would be that there are file cabinets with drawers, folders and documents. You have a spiral bound book that associates a "text name" and a file/folder in cabinet1, drawer2, folder3,document5. There may be more than one name in your spiral bound book that points to exactly the same place! Your spiral bound book is what a directory entry is.

Re: renaming a file and moving into another folder
by ramlight (Friar) on Sep 21, 2011 at 18:28 UTC
    Have you looked at using the Windows command "Move" or File::Copy? These might solve your problem. (I'm at work on a Linux box and can't try them out right now.)
Re: renaming a file and moving into another folder
by Marshall (Canon) on Sep 21, 2011 at 17:31 UTC
    Update: I see that this post has gotten a down vote for no apparent reason from the voter (no reply to my post).
    That is unfortunate.
    If that happened because my tone may offend the OP, it was not my intent at all to offend the OP.
    I see the problem of not understanding the idea of "text of filename"->(pointer) to data often with Windows students.
    If there is a way to explain it better than I did, then great! Please post!

    ---original post continues---

    Deleted perhaps inflammatory statement.

    Your question demonstrates a fundamental lack of understanding of how a file system works.A file does not "exist in a directory" - NO! - that is not what happens!

    As a simplification, lets say that a file is just a "block of contiguous bits/bytes on the disk". The disk is a huge block of contiguous bytes from say byte 0 to byte 2,000,000,000. A file may exist from byte 10356 to byte 22356 on the disk.

    The directory entry for "c:\ksk\vista\sra.txt" would be a data structure with an association between that text name and a pointer to say byte 10356 as the "start" and this text name would also have a length of 22356-10356+1 bytes associated with it. There would be other properties associated with that text name - this is called a "directory entry" - like the telephone "white pages": name, address, etc.

    So if we "rename" sra.txt to SRA_new.txt:
    That means that this directory entry text name that associates "c:\ksk\vista\sra.txt" to byte 10356 on the disk changes to "c:\ksk\vista\SRA_new.txt". No data on the disk moves.

    If we want to "move" the file to a different directory on the same physical disk drive/file system, no bits in the file are actually "moved"!! NO!! We just create a new entry: "C:\home\sra.txt" to point to byte 10356 and delete the entry for "c:\ksk\vista\sra.txt".
    So "moving a file to a different directory on the same disk" is the same as "renaming it"!

    Moving a file from one physical disk to another is different. We have to create a directory entry on the other disk, copy the bits from disk1 to disk2, delete the directory entry on disk1, etc.

    I am sorry that I got long winded here. But there is NO difference in what happens to the bits on the disk between "changing the name associated with those bits" and "moving those bits to a different directory" - this is just changing the text name associated with the starting byte of the data.

      It was downvoted because it is entirely irrelavent and unhelpful to the questioner.

      No, you do not move the contents of the file. But you DO move its directory entry. And as the only way to access the contents is via its directory entry, the effect is the same.

      Picking out one commonly used but slightly misnomeric term and then using as a stick with which to beat the newbie is the very worst kind of elitism.

        Sorry that my post appeared to be unhelpful to you.

        I try to be solidly on the side of "anti-elitism". I try not to use tricky Perl syntax when writing code for new folks. I try to explain how what I write works. In this case, I agree that I sounded too "high almighty". There are limitations in this textual format and sometimes what is heard is not what I intended.

        However I have seen this misconception about a file "lives in a folder" versus "a file is just a set of bits and a pointer to those bits lives in the directory" so often that it provoked a reflex response. This misconception happens a lot even at the university level. This is not a minor point. It is the same idea like Perl reference counting.

        Hard disk I/O isn't covered very well in school. I guess I got carried away.