I am working on a powershell script to gather all subdirectories and file names within them.
I currently have the following for directory names.
Get-ChildItem -Path "C:\Users\UserName\Desktop\Script\Main Folder" -Recurse -Directory | Select-Object FullName
This so far works somewhat as intended however it gives me all directories even the ones that "do not" have files in them. I need it to only give me the directories with files in them such as .txt files and .jpg picture files etc. I also want the script to replace the C:\Users\UserName\Desktop part with just ... "three dots" then the rest of the path name instead of the full path name.
The Output looks like this
C:\Users\UserName\Desktop\Script\Main Folder\Folder A
C:\Users\UserName\Desktop\Script\Main Folder\Folder B
C:\Users\UserName\Desktop\Script\Main Folder\Folder A\Folder C
C:\Users\UserName\Desktop\Script\Main Folder\Folder A\Folder C\Folder D
I wish for it to look like this instead
...\Main Folder\Folder A
...\Main Folder\Folder B
...\Main Folder\Folder A\Folder C\Folder D
Here is a visual diagram Image Link
Next I need to gather all file names in the subdirectories
This is what I have so far as code
Get-ChildItem -Recurse -Name
This gives an output that looks like this
Folder A
Folder B
File 1.txt
Folder A\Folder C
Folder A\File 2.txt
Folder A\File 3.txt
Folder A\Pic1.jpg
Folder A\Folder C\Folder D
Folder A\Folder C\Folder D\File 6.txt
Folder B\File 4.txt
Folder B\File 5.txt
Folder B\Pic2.jpg
The output should instead look like this
File 1.txt
File 2.txt
File 3.txt
Pic1.jpg
File 6.txt
File 4.txt
File 5.txt
Pic2.jpg
This is essentaly the file names only with the paths fully removed and no directories at all.
Finally I need a way to merge both of these codes into one and give an output that looks like the following
...\Main Folder\Folder A
File 2.txt
File 3.txt
Pic1.jpg
...\Main Folder\Folder B
File 4.txt
File 5.txt
Pic2.jpg
...\Folder A\Folder C\Folder D
File 6.txt
I "Do Not" need spaces between the actual file names. That is only for formatting and easy reading here on this website. I would however like a space between the directory and the first file name listed in that directory. Then a space between the last file name and the next directory.
Also the code should be able to send the output to a basic text file.
I am not too sure if I should ask multiple questions to get this answer since this is really two parts of code then merging them somehow.
Any feedback given would be greatly appreciated.
Get-ChildItem -Recurse -Name | Out-File -FilePath .\filelist.txt.