Searching files with last time modification date

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
raf65tuir
Posts: 4
Last visit: Thu Aug 10, 2023 5:11 am

Searching files with last time modification date

Post by raf65tuir »

Welcome,
In powershell ise that working

$DaysBack="-3650"
$CurrentDate=Get-Date
$DatetoDelete=$CurrentDate.AddDays($DaysBack)
Get-ChildItem -File -Path "My-unc-path" -Recurse -Force | where { ($_.LastWriteTime -lt $DatetoDelete) } | Out-GridView

I was build forms with two textbox fields and one button.
textbox1 will be write my unc path ex. \\myserver1\myFolder1
textbox2 days from past ex. -3650

Forms look like

$form1_Load={
#TODO: Initialize Form Controls here

}

$button1_Click={
#TODO: Place custom script here
$DaysBack = ($textbox2.Text)
$DatetoDelete = (Get-Date).AddDays($DaysBack)
$FolderPath = Get-ChildItem -File -Path $textbox1.Text -Recurse -Force | where { $_.LastWriteTime -lt $DatetoDelete } | Out-GridView
}

This search show my all files in my unc path. Why not filtering on LastWriteTime? What I done wrong?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Searching files with last time modification date

Post by jvierra »

Why make it so hard. Just run this at a prompt to see how it works.
  1. Get-ChildItem -File -Path My-unc-path -Recurse -Force |
  2.      Where-Object {$_.LastWriteTime -lt [datetime]::Now.AddDays(-3650)} |
  3.      Out-GridView
Do not put quotes around numbers. Strings are not numbers.
raf65tuir
Posts: 4
Last visit: Thu Aug 10, 2023 5:11 am

Re: Searching files with last time modification date

Post by raf65tuir »

Thank you for response.
Thats is not working. I cant put information how many days will be insert becasuse that will be do user. Therefore I want to user variable. In first example I was decided how long in the past i want search. Moreover, that i was created exe file, and this file working on one pc but not working on others. I think that not will be solusion, becasue can be depend from pc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Searching files with last time modification date

Post by jvierra »

It is not possible to understand what you are asking. What is it that doesn't work? How do you know it doesn't work?

The code I posted is standard and works on all versions of PowerShell and Windows.

Whether you put the number in a variable or not does not make any difference. DO NOT put quotes around numbers.
raf65tuir
Posts: 4
Last visit: Thu Aug 10, 2023 5:11 am

Re: Searching files with last time modification date

Post by raf65tuir »

Thank you for answer.

Ok, explain again.
Started Powershell ISE on computer name "abc"
run this
$DaysBack="-3650"
$CurrentDate=Get-Date
$DatetoDelete=$CurrentDate.AddDays($DaysBack)
Get-ChildItem -File -Path "My-unc-path" -Recurse -Force | where { ($_.LastWriteTime -lt $DatetoDelete) } | Out-GridView

In this case myself writed how many days from past I want receive files.
Thats working on computer "abc"

On this same computer "abc" started Sapien Powershell Studio.
I was crated form where was created two fields and one button. Action in button is:
$form1_Load={
#TODO: Initialize Form Controls here

}

$button1_Click={
#TODO: Place custom script here
$DaysBack = ($textbox2.Text)
$DatetoDelete = (Get-Date).AddDays($DaysBack)
$FolderPath = Get-ChildItem -File -Path $textbox1.Text -Recurse -Force | where { $_.LastWriteTime -lt $DatetoDelete } | Out-GridView
}

Thats NOT working.

($textbox2.Text) is variable writed to one field which will be write over user which will be searching files, therfore not write number.
the second line is $datetoDelete where is calculated date. Result this date is depend from value writed over user.
In third line $textbox1.Text is field with unc-path writed over user. Condition where { $_.LastWriteTime -lt $DatetoDelete } is not check.
Not relevant that is variable $datetoDelete or value.

After writed post in this forum I was done the same on computer "xyz"
Started Powershell ISE like in computer "abc" and run script writed on top. Not will be write again.
Reuslts: Working

On computer "xyz" start Sapien Powershell Studio, created forms and writed the same code like in form on computer "abc".
When I was run form on computer "xyz" in results I was receive files older that value writed in $textbox2.text.
Results: Working

I don't know what is the difference between computer configurations, surely a lot of elements are different, but which are important for powershell? Ise works on both powershell computers. On one only in the form. I can't compile an exe form and let users know if one works and the other doesn't.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Searching files with last time modification date

Post by jvierra »

Without access to your computer there is no way for us to know what is wrong with the computer. You will need to do some debugging.

Unfortunately, you will have to spend time debugging both computers. If you think it is PSS, then post in customer support forum for PSS.
This topic is 1 year and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked