Forum Discussion
powershell create a catalog file
Hi all,
I am new to powershell. I am looking for powershell script to generate a catalog file which display all the subfolders and files underneath, better to comment out the subfolders, and only files listed, but should be listed just under the sunfolders.
let say I have a folder called total_folder
underneath there are many subfolders
folder1
file1-1
fille1-2
folder2
file2-1
file2-2
file2-3
folder3
file3-1
......
Now I would like to document these info into a catalog file as below
;----folder1----
file1-1
fille1-2
;----folder2----
file2-1
file2-2
file2-3
;----folder3----
file3-1
......
Hi William,
I don’t know why you want it like this but it is possible. In the snippet below you’ll find a way to do this.
$Path = "c:\temp" $Outputfile = "c:\temp\output.txt" $allitems = Get-ChildItem -path $Path -Recurse $groups = $allitems | Group-Object -Property PSParentPath $lines = @() foreach($group in $groups){ $lines += $group.name.replace("Microsoft.PowerShell.Core\FileSystem::", "----") + "----" $items = $group.Group foreach($item in $items){ if(-NOT $item.PSIsContainer){ $lines += $item.name } } } $lines | out-file -FilePath $Outputfile
It’s also possible to export to data to a XML file for later analysis (in powershell). You can import this data later (also on a different device). You will keep all properties like size (Length), dates (Create, LastAccessTime, LastWritetime etc) and many more. Disadvantage is that the output XML file is bigger. See the snippet below:
# Get and export data $allitems = Get-ChildItem -path $Path -Recurse $allitems | Export-Clixml "c:\temp\output.xml" # Import data $importedItems = Import-Clixml "c:\temp\output.xml"
I hope it works out for you William!
Good luck!
Grtz, Manfred de Laat
- Manfred101Iron Contributor
Hi William,
I don’t know why you want it like this but it is possible. In the snippet below you’ll find a way to do this.
$Path = "c:\temp" $Outputfile = "c:\temp\output.txt" $allitems = Get-ChildItem -path $Path -Recurse $groups = $allitems | Group-Object -Property PSParentPath $lines = @() foreach($group in $groups){ $lines += $group.name.replace("Microsoft.PowerShell.Core\FileSystem::", "----") + "----" $items = $group.Group foreach($item in $items){ if(-NOT $item.PSIsContainer){ $lines += $item.name } } } $lines | out-file -FilePath $Outputfile
It’s also possible to export to data to a XML file for later analysis (in powershell). You can import this data later (also on a different device). You will keep all properties like size (Length), dates (Create, LastAccessTime, LastWritetime etc) and many more. Disadvantage is that the output XML file is bigger. See the snippet below:
# Get and export data $allitems = Get-ChildItem -path $Path -Recurse $allitems | Export-Clixml "c:\temp\output.xml" # Import data $importedItems = Import-Clixml "c:\temp\output.xml"
I hope it works out for you William!
Good luck!
Grtz, Manfred de Laat
- goodywpCopper Contributor
Manfred101 Thanks sooooo much!
It works as expected. Now a couple of enhancements as below:
1) Can we ignore the path, only folder display
2) Can we ignore the parent folder, in this case is temp folder in your code, only subfolders under temp
3) Can we comment out certain file with certain extension, i.e, *.txt file will be comment out with ;
Thanks again for your help!!