#1 10 Jan 2011 15:23

allal
Member
Registered: 10 Jan 2011
Posts: 48

parent foldername recursive rename

how to get parent folder of current directory and rename all files recursively
with parent foldername like prefixing any file with parent folder

Last edited by allal (10 Jan 2011 16:16)

Offline

#2 04 Feb 2011 13:10

allal
Member
Registered: 10 Jan 2011
Posts: 48

Re: parent foldername recursive rename

OR do you know how to rename folders recursively only folders
i really find it harder to rename folders whereas in cmd is very easier.
i just don't understand why they made things so harder??????????

Offline

#3 04 Feb 2011 13:53

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,011
Website

Re: parent foldername recursive rename

Look at the 'Restoring zero byte files from a backup' thread
http://ss64.org/viewtopic.php?id=453

I'm sure you can adapt that by changing the copy-item to a rename-item.

Powershell really isnt any harder than CMD but it is very different, you have to think of everything as objects.

Offline

#4 07 Feb 2011 19:28

allal
Member
Registered: 10 Jan 2011
Posts: 48

Re: parent foldername recursive rename

thanks this is really helpful and tricky,but this is not what i want to do

yes getting parent foldername and  prefixing its child files with it is easier
each time you enter new directory you will have to get new parent name
and prefix its children file with it .
the problem is prefixing folder child,because the loop will stop and give a nasty error
can't continue because directory doesn't exist,because it is renamed

example for prefixing children file only

currentcontainer -->  c:\allal
children:
allal_hyper link.bat
allal_elapsedime.cmd
allal_STRNDX.bat

nextcontainer -->  c:\allal\new
children:
new_downloadlink.txt
new_Script.vbs
and so on.....

renaming so is easy but renaming folders this is really hard
but of course there will be a solution to do it

the following powershell code demonstrate how file prefixing work

filter Child-ParentPrefix {

   $c=(get-item $_).name
   $full=(get-item $_).fullname
   $current_prfxcontainer=split-path $full
   $parent=split-path $current_prfxcontainer -leaf
   $parent=("$parent" + "_") 
   "`ncurrent piped object is $_"
   if ((get-item $_).PSIsContainer -eq $true) {
       write-host -fore cyan "SKIP TO PREFIX THIS ITEM BECAUSE IT IS A DIRECTORY`" `n"

   } else {
         "yes it is a file"
         if ($current_prfxcontainer -eq $previous_prfxcontainer) {
             if ( -not ($c.startswith($parent))) {
                 "previous loop directory is same as the current loop directory"
                 write-host -fore yellow "NO NEED TO GET NEW PARENT NAME BECAUSE WE ARE STILL IN THE SAME DIRECTORY `n"
                 write-host -fore green "Applying The ParentPrefix to the Current Childname...`nfile `"$c`" Be Loyal To Your Parent."
                 rename-item $full -newname ("$parent" + "$c")
                 "`n"
             } else {
                 write-host -fore blue "NO NEED TO PREFIX FILE WITH ITS PARENT NAME BECAUSE IT IS ALREADY PREFIXED `n" 
               }   
         
         } else {
             "new directory reached"
             write-host -fore green "getting new directory parent name..."
             "`n"
             $parent=split-path $current_prfxcontainer -leaf
             $parent=("$parent" + "_")
             $previous_prfxcontainer=$current_prfxcontainer
           }
     }
}

$previous_prfxcontainer="C:\Documents and Settings\Administrateur\Bureau\bat file"
cd $previous_prfxcontainer
$parent=((get-item $previous_prfxcontainer).name + "_")

(get-childitem $previous_prfxcontainer -recurse) | Resolve-Path | Child-ParentPrefix

hope the problem now is well explained

Last edited by allal (07 Feb 2011 19:41)

Offline

#5 15 Feb 2011 12:58

allal
Member
Registered: 10 Jan 2011
Posts: 48

Re: parent foldername recursive rename

problem solved

Offline

Board footer

Powered by FluxBB