Here is a simple script:
$srcpth = "C:\Users\Mark\Desktop\dummy\"
$files = Get-ChildItem -Path $srcpth -File -Recurse
foreach ($f in $files) {
$filen = $f.Name
$filesize = $f.Length
Write-Output "$filen $filesize"
}
This will correctly loop through all subfolders in C:\Users\Mark\Desktop\dummy
and output file name with file size, but it will not show relative path. How do I resolve the relative path? Thanks.
EDIT: added below for clarification of desired output:
For example, under C:\Users\Mark\Desktop\dummy
are subfolders with files
C:\Users\Mark\Desktop\dummy\file00.txt
C:\Users\Mark\Desktop\dummy\folder01\file01_01.txt
C:\Users\Mark\Desktop\dummy\folder01\file01_02.txt
C:\Users\Mark\Desktop\dummy\folder01\file01_03.txt
C:\Users\Mark\Desktop\dummy\folder02\file02_01.txt
C:\Users\Mark\Desktop\dummy\folder02\file02_01.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_01.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_02.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_03.txt
C:\Users\Mark\Desktop\dummy\folder03\file03_04.txt
Output with above code produces:
file00.txt 9
file01_01.txt 10
file01_02.txt 12
file01_03.txt 12
file02_01.txt 15
file02_01.txt 14
file03_01.txt 11
file03_02.txt 15
file03_03.txt 13
file03_04.txt 12
But what I want is:
file00.txt 9
\folder01\file01_01.txt 10
\folder01\file01_02.txt 12
\folder01\file01_03.txt 12
\folder02\file02_01.txt 15
\folder02\file02_01.txt 14
\folder03\file03_01.txt 11
\folder03\file03_02.txt 15
\folder03\file03_03.txt 13
\folder03\file03_04.txt 12
preceeding \
, no slash, or .\
are fine.
$pwd.Path
previously because commonly one wants to see the path relative to the current location. I agreeConvert-Path
would be the best choice – Daphene