0

This is my current folder structure in a Windows 7 machine:

---myfoolder
   |    
   +---nope              
   |   \---nope          
   |           nope.txt  
   |                     
   +---wtf               
   |   \---wtf           
   |           wtf.txt   
   |                     
   +---yep               
   |   \---yep           
   |           yep.txt   
   |                     
   \---zomg              
       \---zomg          
               zomg.txt  

In this structure there is a duplicate folder at each layer like nope\nope.

I'd like to move each file in each folder up a level to eliminate this duplicate (note that for the sake of simplicity I've listed 1 file in each directory but there could be multiple files with different extensions).

This is what I've tried with powershell from this answer:

gci -R | ?{!$_.PSIsContainer} | %{mv $_.fullname $_.directory.parent}

Unfortunately I'm getting this error:

mv : Cannot create a file when that file already exists.
At line:1 char:35
+ gci -R | ?{!$_.PSIsContainer} | %{mv $_.fullname $_.directory.parent}

I think something fishy is happening because there is a duplicate name between parent and child folder, I've tried also providing a -Force in the mv statement but it only managed to actually erase the files leaving the folders as they are (luckly enough I had setup a test environment before doing this!).

What am I missing? Unfortunately I'm really a novice in PS scripting, thank you.

2

Move-Item takes paths as strings, and when .directory.parent casts to a string, it's either an empty string or just one directory name, it doesn't seem to be the full path to the parent folder.

Try:

$_.directory.parent.FullName

or

|%{ mv $_.FullName (Split-Path -Parent $_.Directory) }
1
  • Yea I figured it out myself a bit after I posted my question, but you explained pretty well thank you!
    – Gruber
    Oct 21 '16 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.