Sometimes, we need to work with numerous files located in certain location. We might need to perform some action or read some lines with those files. When the requirement is all about reading all files in directory and sub-directories, we can simply use ls in Powershell. (Run Get-Alias ls to know which cmdlet is called in background, skipping this so that you learn something more, those who know this can avoid it ;))
Every experiment starts with a small building block, so let's run ls with recursion :
ls -recurse
You see multiple directories, but this is not useful as you don't have much option to perform any task on them.
Directory: B:\tst
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5/26/2015 6:08 AM A
d---- 5/26/2015 6:09 AM B
-a--- 5/26/2015 6:09 AM 0 file4.txt
-a--- 5/26/2015 6:09 AM 0 file5.txt
Directory: B:\tst\A
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/26/2015 6:08 AM 0 file1.txt
Directory: B:\tst\B
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/26/2015 6:09 AM 0 file2.txt
-a--- 5/26/2015 6:09 AM 0 file3.txt
Now, run the same command with little twist and then you will be able to much from output :
ls -Recurse | foreach { $_.FullName }
The result is full list of files and you can perform more precise operations on them.
B:\tst\A
B:\tst\B
B:\tst\file4.txt
B:\tst\file5.txt
B:\tst\A\file1.txt
B:\tst\B\file2.txt
B:\tst\B\file3.txt
But, if you see the output above, you can see that directories are also there, but your requirement might be only related with files or directories. So, let's get separate results for them.
List all Files recursively :
ls -Recurse | where { $_.PSIsContainer -eq $false } | foreach { $_.FullName }
Output:
B:\tst\file4.txt
B:\tst\file5.txt
B:\tst\A\file1.txt
B:\tst\B\file2.txt
B:\tst\B\file3.txt
List all directories recursively :
ls -Recurse | where { $_.PSIsContainer -eq $true } | foreach { $_.FullName }
Output:
B:\tst\A
B:\tst\B
So, this concludes today's tip. Always start with small example and keep trying till you get the results. Send your mails and comments and let me know if you have questions.
Enjoy!
Every experiment starts with a small building block, so let's run ls with recursion :
ls -recurse
You see multiple directories, but this is not useful as you don't have much option to perform any task on them.
Directory: B:\tst
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5/26/2015 6:08 AM A
d---- 5/26/2015 6:09 AM B
-a--- 5/26/2015 6:09 AM 0 file4.txt
-a--- 5/26/2015 6:09 AM 0 file5.txt
Directory: B:\tst\A
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/26/2015 6:08 AM 0 file1.txt
Directory: B:\tst\B
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/26/2015 6:09 AM 0 file2.txt
-a--- 5/26/2015 6:09 AM 0 file3.txt
Now, run the same command with little twist and then you will be able to much from output :
ls -Recurse | foreach { $_.FullName }
The result is full list of files and you can perform more precise operations on them.
B:\tst\A
B:\tst\B
B:\tst\file4.txt
B:\tst\file5.txt
B:\tst\A\file1.txt
B:\tst\B\file2.txt
B:\tst\B\file3.txt
But, if you see the output above, you can see that directories are also there, but your requirement might be only related with files or directories. So, let's get separate results for them.
List all Files recursively :
ls -Recurse | where { $_.PSIsContainer -eq $false } | foreach { $_.FullName }
Output:
B:\tst\file4.txt
B:\tst\file5.txt
B:\tst\A\file1.txt
B:\tst\B\file2.txt
B:\tst\B\file3.txt
List all directories recursively :
ls -Recurse | where { $_.PSIsContainer -eq $true } | foreach { $_.FullName }
Output:
B:\tst\A
B:\tst\B
So, this concludes today's tip. Always start with small example and keep trying till you get the results. Send your mails and comments and let me know if you have questions.
Enjoy!
No comments:
Post a Comment