Reputation: 259
I have this code that looking all the files in the directory and process it into a exe. And there's a bunch of files there and different file type/extensions. What I need to do is just to get the .txt files from the directory and just the .txt files will be process in .exe. Is this possible? What is the best solution for my problem? Exclude the other file extension? Because I need to get 2-3 file extension in a directory and .txt is one of them. Thanks in advance. Im stucked here.
$Allfiles = Get-ChildItem "C:\SCRIPTS\tester\TESTALL\" -Recurse | Where {!$_.PSIsContainer -and ($_.CreationTime -gt $oldday) -and !$_.FullName.StartsWith('C:\SCRIPTS\tester\TESTALL\excludethisfolder\') } | Select-Object FullName
foreach ($file in $Allfiles) #check for each file in variable $Allfiles)
{
$file1 = $file.Fullname
$foldername = Get-ChildItem -Path $file.FullName
$input = $foldername.DirectoryName
$Mothername = Get-Item -Path $input
$output = $Mothername.Parent.Fullname
$output = "$output\sc"
Write-Host "The Filename: $file1"
Write-Host "The FolderName: $input"
Write-Host "$output"
Write-Host "$Excludefolder"
$argument = "$input $output $input"
Start-Process -FilePath "C:\SCRIPTS\process.exe" -ArgumentList $argument #execute the process
}
Clear-variable -Name Allfiles
I already tried some other script that excluding or including txtfile but it's not working
I tried this one and yes it's working for me but it's just excluding the .txt in output but not in the process of FOREACH.
$Allfiles = Get-ChildItem "C:\SCRIPTS\tester\TESTALL\" -Recurse -exclude "*.txt"| Where {!$_.PSIsContainer -and ($_.CreationTime -gt $oldday) -and !$_.FullName.StartsWith('C:\SCRIPTS\tester\TESTALL\excludethisfolder\') } | Select-Object FullName
I'm really stuck with this one, I already tried include/exclude functions but its not working.
Upvotes: 0
Views: 109
Reputation: 4081
I'm not sure where you would be getting extra files in the foreach. Try replaceing your foreach with the following and see if it still occurs. I commented out the executable and simply echo out the $argument variable but it seems to be matching what you are asking for.
foreach ($file in $Allfiles) #check for each file in variable $Allfiles
{
$file1 = $file.Fullname
$input = Split-Path $file -Parent
$output = $(Split-Path $input -Parent) + "\sc"
Write-Host "The Filename: $file1"
Write-Host "The FolderName: $input"
Write-Host "$output"
$argument = "$input $output $input"
#Start-Process -FilePath "C:\SCRIPTS\process.exe" -ArgumentList
$argument #execute the process
}
Upvotes: 1