Browse folders and subfolders with powershell

Asked

Viewed 2,395 times

1

I recently did a data recovery from a hard drive, but the folder where the files are, is separated by subfolders with the name of each extension (eg: jpg, gif...), and each subfolder contains other subfolders separating the files into "small" quantities (e.g.: png[11001-12000]).

I wanted to build a powershell script so that I could only copy files larger than 100 KB, for example, to another folder. Doing some research, I found a script that does something similar

foreach($file in (Get-Item C:\Users\MEU_USUÁRIO\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets\*))
{
    if ((Get-Item $file).length -lt 100kb) { continue }
    Copy-Item $file.FullName "C:\Users\MEU_USUÁRIO\Pictures\PASTA_QUALQUER\$($file.Name).jpg";
}

He copies those images from windows Potlight to be able to put the background, but does not come close to what you need precisely by not browsing the subfolders and such. I have no knowledge of powershell, and I need your help to solve this little problem, and break learn a little bit.

1 answer

0


After researching and studying a little, I wrote the following code:

foreach($arq in (Get-ChildItem "DIRETORIO_DE_ORIGEM" -Recurse -Include "*.png", "*.gif", "*.bmp", "*.tif", "*.jpg"))
{
    if ((Get-Item $arq).Length -ge 200kb) {Copy-Item $arq.Fullname "DIRETORIO_DE_DESTINO\$($arq.Name)"}    
}

It copies the DIRETORIO_DE_ORIGEM files larger than or equal to 200kb to DIRETORIO_DE_DESTINO. What I had to change was the name of the folders that used characters that were not allowed (e.g., "png [ something ]"for "png", or "png(something)", for example)

You can use cmdlet Move-Item in place of Copy-Item, to "crop" the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.