GET filelists for each subfolders | Each filelist put in txt-file,which placed in folders that it describe and named like this folder

2.5k Views Asked by At

SOLVED


For each subfolder, that contains images *.jpg need to create textfile list of images names (whithout extensions). Textfiles should be named same as its directory

For example you have same directory scheme:

C:\scriptfolder\ - Parentfolder for filelist_script.ps1
C:\scriptfolder\subfolder1\ - contains some jpg-files
C:\scriptfolder\subfolder2\ - contains some jpg-files
C:\scriptfolder\subfolderN\ - contains some jpg-files

Result of script work will be:

C:\scriptfolder\subfolder1\subfolder1.txt
"subfolder1.txt" - List of all images(.jpg) in "subfolder1"
C:\scriptfolder\subfolder2\subfolder2.txt
"subfolder2.txt" - List of all images(.jpg) in "subfolder2"
C:\scriptfolder\subfolderN\subfolderN.txt
"subfolderN.txt" - LList of all images(.jpg) in "subfolderN"


SOLUTION:

$gl = Get-Location 
$folderlist = Get-ChildItem -path $gl\ -Recurse -Directory -Name  
foreach ($folder in $folderlist) {
    Get-ChildItem $gl\$folder\*.jpg -File | 
    Foreach-Object {$_.BaseName} |
    Out-file -path $gl\$folder\$folder.txt  -Force;
} 

Using "BaseName" parametr excludes file-extensions from shown data.
Change it to "Name", if you need to get files names whith extension.

1

There are 1 best solutions below

1
Owain Esau On

Your question isn't very well asked, it is hard to understand exactly what you are asking. By the looks of it you want to get a list of directories containing a single _ and then every .jpg file inside those folders appending it to a text file. This is pretty simple:

$folder = "C:\Users\user\Documents"

foreach ($subfolder in (Get-childitem -Directory $folder\*test*)) {
    Get-Childitem $subfolder -Name -filter *.csv | Out-file pctrlst.txt -append
}

There are a few issues in your code:

$subfldr = Get-childitem -Name -Directory *_*

The usage of -Name here returns only the folder name, not the full path. If you are changed to that directory in PowerShell then it won't be an issue but there is no need for that flag.

$pctrlst = Get-childitem -Name -Include *.jpg

-Include needs to be changed to -Filter

$subfldr | ForEach-Object { $pctrlst | out-file pctrlst.txt } 

Here you are piping the names of the folders (not the path just the name) to an object loop where you are simply running Out-File on the same variable which holds however many jpg files that are located in the current directory. You are just overwriting the same file with the same data multiple times here.