Powershell move directory and add extension

897 Views Asked by At

I have a homework problem and I can't get to the end of it:
Write a shell that has two folder names as parameters and moves the second folder as subfolder of the first one and adds '.unu' to all filenames in that second folder.

This is what I wrote but it doesn't work:

gci D:\powershell\dir2 | foreach-object{ ren -new ($_.Name + ".unu")} 

Copy-Item -Recurse D:\powershell\dir2.unu D:\powershell\dir2

Remove-Item -Recurse D:\powershell\dir2.unu

also you can see I made in D:\powershell 2 folders and I want to move dir2 in dir1

1

There are 1 best solutions below

0
On

You are not really using pipeline here...:

Move-Item dir3 .\dir1\ -PassThru | Get-ChildItem | 
    where { ! $_.PsIsContainer } | 
    Rename-Item -NewName { $_.Name + ".unu" }

Move-item will take care of moving folder for you, with PassThru you will get new object to play if... Pipeing it to Get-ChildItem will return what's in it (may consider adding -recurse there...). Where will filter out directories (you were asked to rename files only), and Rename-Item will bind all files, and rename them as requested.

And someone wanted to write a script for that..? :o