Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

“Process cannot access file… being used by another process”

$
0
0

Good morning everyone. I have a simple script to copy all the Windows 10 spotlight lock screen wallpapers from a Windows directory, and then rename them all to *.jpg. The following script accomplishes that perfectly:

Copy-Item C:Users[username]AppDataLocalPackagesMicrosoft.Windows.ContentDeliveryManager_cw5n1h2txyewyLocalStateAssets* C:Users[username]Desktoptest cd c:users[username]desktoptest; Dir | rename-item -newname { $_.Name +".jpg" } 

The only problem is that I only need/want the 1920×1080 images, but there are other images of different sizes mixed in. What I’d liked to do is remove any .jpg that is not 1920×1080. When I run the following code to do this:

$(Get-ChildItem).FullName | ForEach-Object { $img = [Drawing.Image]::FromFile($_); $dimensions = "$($img.Width) x $($img.Height)" If ($dimensions -lt "1920 x 1080") { Remove-Item $_ } } 

I get this error:

Remove-Item : Cannot remove item C:users[username]desktoptestfb0a7fbb35c3e0651d37b94ec549de12e897735cacb0b406ffbc472124bc9c78.jpg: The process cannot access the file ‘C:users[username]desktoptestfb0a7fbb35c3e0651d37b94ec549de12e897735cacb0b406ffbc472124bc9c78.jpg’ because it is being used by another process. At line:6 char:9

I’m a novice when it comes to Powershell, so I didn’t write these myself; I just found them and edited what I needed. I learned about dispose(), but don’t know where I would put it in the script to get it to work.

Any help would be greatly appreciated, thanks!

submitted by /u/Ancient_Unknown
[link] [comments]

The post “Process cannot access file… being used by another process” appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793