Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Parsing sub-directories into csv

$
0
0

I have a directory with thousands of files all in an ordered sub-directory structure. I’m trying to export those files, along with their respective file paths, delimited, into a csv. Hierarchical order should be static in the csv: in the below example, ‘file.jpg’ should always appear in the fifth column of the csv, with any preceding missing values returning as null.

c:\master\sub1\sub2\sub3\sub4\file.jpg > in csv becomes > sub1, sub2, sub3, sub4, file.jpg

c:\master\sub1\sub2\sub3\file.jpg > in csv becomes > sub1, sub2, sub3, - , file.jpg

c:\master\sub1\sub2\file.jpg > in csv becomes > sub1, sub2, - , - , file.jpg

 

Before I had Powershell, I did something similar to this in the cmd prompt using this:

 

(for /r %f in (*) do @echo “%~dpf”, “%~nxf”, %~zf) >filename.csv

 

This exported the entire file path as a string, but without 1) delimiting the sub-directories, or 2) holding their positions static in the csv. So how do I include these two elements in the script?

 

I’ve found some Powershell commands that would also get close, but again, can’t figure out in Powershell how to perform actions 1 and 2.

 

Thanks.



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>