Find all files with parentheses?

I recently got a new laptop, and for some reason DropBox decided to duplicate everything. I like to learn a little bit of PoSH often, so I can be more proficient at it, so I decided that it might be a good time, but still I’m out of luck. I'm not a complete noob, but definitely still a bit.

Basically, all dupe files have (1) at the end (for example, the file name is (1) .txt). I was able to pinpoint those who:

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

So far, good, but then I want to move them to the "dupes" directory and keep the subfolder structure. For some reason, it seems like it should be simple, PoSH makes it super complex. I searched high and low and found some close examples, but they also include many other parameters that just confuse me. I believe that I am behind this:

* Find items with the specified command
* Pipe to Move-Item
* Somehow include a new element-Directory name -force
* Also make sure this directory does not exist yet

I 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!

Ps Although I stopped dropbox and tried several different files, currently I get:

 Move-Item : Cannot move item because the item at 'C:\users\jkelly.MC\Dropbox' is in use. At line:2 char:1 + Move-Item $from $to -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand 
+4
source share
1 answer

You can do something like 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 basically replaces the base source path with the base destination path in files to preserve the directory structure. You can customize it for your needs.

+2
source

Source: https://habr.com/ru/post/1496343/


All Articles