Count of Files

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Count of Files

Post by JohnTitor » Thu Mar 14, 2019 11:06 am

So I'm trying to show the count of about 40 different folders
Desktop
Documents
MozillaProfiles
Outlook
etc...

My current working theory is

$folder = "C:\users\user\desktop"
$DesktopCount = Get-ChildItem $folder -Force -Recurse -File | Measure-Object | %{$_.Count}

Then at the end of the script I do
$DesktopCount+$DocumentsCount+$DownloadsCount+$FavoritesCount+$LinksCount

Is there a better way of doing this?

jvierra
Posts: 13900
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Thu Mar 14, 2019 11:53 am

It is not clear what you are tri ng to do.

$folders = 'desktop','documents', 'outlook' … etc

$count = (Get-Childitem $folders -File -Recurse).Count

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Thu Mar 14, 2019 12:20 pm

I'm trying to provide the count of files of the folders I'm going to move through robocopy and display it to the technician
Then, once the robocopy job is done, I'm going to provide them the count on the new PC

The problem, is not all files are under the users profile so I can't just count that. They are spread throughout the PC so I have to do the individual counts.

jvierra
Posts: 13900
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Thu Mar 14, 2019 12:42 pm

RoboCopy can count the files faster than PowerShell. Just select "log only" and extract the file count.

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Thu Mar 14, 2019 12:48 pm

$folders = "C:\users\user\desktop","C:\users\user\documents"
$count = $null
foreach ($folder in $folders){

$rclog = robocopy $folder \xxx /L /E
$count += ($rclog -match 'new File' ).Count

}

$count

Looks like a winner!

jvierra
Posts: 13900
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Thu Mar 14, 2019 1:05 pm

Little trick...

Create a new folder. Symlink target folders into new folder. Run RoboCopy from new folder root and all can be copied/counted/moved in one command.

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Thu Mar 14, 2019 1:11 pm

Huh? lol

I'll give you some examples of the different folders I'm moving

"C:\users\$user\desktop"
"C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default"
"C:\Users\$user\AppData\Local\Microsoft\Feeds"
"C:\Users\$user\AppData\Local\Microsoft\Internet Explorer\EmieUserList"
"C:\Users\$user\AppData\Roaming\Mozilla\Firefox"
"C:\Users\$user\AppData\Roaming\Microsoft\Outlook"

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Fri Mar 22, 2019 10:03 am

jvierra wrote:
Thu Mar 14, 2019 1:05 pm
Little trick...

Create a new folder. Symlink target folders into new folder. Run RoboCopy from new folder root and all can be copied/counted/moved in one command.
Getting back to this as it's a side project
SymLink.png
SymLink.png (19.46 KiB) Viewed 1259 times

jvierra
Posts: 13900
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Fri Mar 22, 2019 10:32 am

You need to learn how to use symlinks and mklink.

You should not be doing this over the system folders.

Please don't post images of your code. Post code. The images do not tell us what you are doing.

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Fri Mar 22, 2019 10:48 am

C:\link is considered a system folder?

I've researched a bit this morning and it doesn't look like you can symlink multiple folders to one

Locked