powershell - Find All Files With Parentheses? -


i got new laptop , reason dropbox decided duplicate in there. teach myself bit of posh can can more adept @ it, figured might time, far, no luck mucking about. i'm not total noob, still bit of one.

basically dupe files have (1) @ end (e.g. filename (1).txt). able pinpoint with:

gci -recurse | ? { $_.name -like "*(1)*" } 

good far, want move them "dupes" directory , keep subfolder structure. whatever reason, seems should simple, posh makes super hard. i've searched high , low , found few close examples, include bunch of other parameters end confusing me. believe i'm after is:

*find items above command
*pipe move-item
*somehow include new-item -itemtype directory -force
*also check see that dir doesn't exist

currently have:

$from = "c:\users\xxx\dropbox" $to = "c:\users\xxx\downloads\dropbox dupes" gci | ? { $_.name -like "*(1)*" } | new-item -itemtype directory -path $to -force move-item $from $to -force 

any pointers/help/examples?

thanks!

p.s. though i've stopped dropbox , tried few different files, i'm getting:

move-item : cannot move item because item @ 'c:\users\jkelly.mc\dropbox' in use. @ line:2 char:1 + move-item $from $to -force + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo          : invalidoperation: (:) [move-item],     psinvalidoperationexception     + fullyqualifiederrorid : invalidoperation,microsoft.powershell.commands.moveitemcommand 

you can this:

$source = "c:\dropbox" $destination = "c:\dropboxdupes" gci .\dropbox -recurse -file | ?{ $_.basename -match ".*\(\d+\)$"} | % {      $destination_filename = $_.fullname.replace($source, $destination)     $destination_dir = split-path $destination_filename -parent     if(-not (test-path $destination_dir -pathtype container)) {         mkdir $destination_dir | out-null     }     move-item $_.fullname $destination_filename } 

it replaces source base path destination base path in files preserve directory structure. can fine tune needs


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -