Powershell Script to archive files over 1 day - powershell

I am looking for a script that can ignore the timing and utilise just the date to move files after 1 day, so yesterday, to an archive folder.
My knowledge of powershell is not great so any advice on how i can do this would be great.
Everyday i run a script that generates a .txt report which has a filename .....2022 01 02 (The filename ends with the date) so would like to add some extra lines that archives the .txts that were created yesterday to an archive folder.

The [datetime] type has a Date property that gives you the date at midnight, thereby allowing you to compare dates without taking the time component into account:
# Construct datetime value describing yesterday at midnight
$yesterday = (Get-Date).AddDays(-1).Date
# Filter files based on date of the `CreationTime` property
$filesCreatedYesterday = Get-ChildItem -Path .\path\to\folder -File |Where-Object { $_.CreationTime.Date -eq $yesterday }
$filesCreatedYesterday will now contain the files created yesterday

Related

Passing a parameter to each line in a file

I'm trying to pass yesterday's date value as a parameter to a list of filenames that i have placed in files.txt . The filenames come with the timestamp and i need to copy yesterday's files to a network share drive. I have passed $odate as the date in filename in files.txt, i need to replace the $odate with yesterday's date and copy the file from 1 network share drive to another on a daily basis.
i tried passing the parameter($odate) in the filenames for each line and defined as in the code snippet below
foreach($line in Get-Content .\Desktop\files.txt){
$odate = (get-date (get-date).AddDays(-1) -UFormat "%Y%m%d")
echo $line}
PB724_SSNTXN_D110A01_FPRS_$odate*.DAT.gz
PB724_SSNTXN_D110A02_FKEN_$odate*.DAT.gz
I'm getting the list of the filenames as i have put in the files.txt and the $odate is not getting replaced with yesterday's date.
You can do it like this:
Get-Content -Path .\Desktop\files.txt |
ForEach-Object {$odate = (Get-Date).AddDays(-1).ToString('yyyyMMdd')}{
$ExecutionContext.InvokeCommand.ExpandString($_)
}
So, for a file with content:
PB724_SSNTXN_D110A01_FPRS_$odate.DAT.gz
PB724_SSNTXN_D110A02_FKEN_$odate.DAT.gz
You will get output:
PB724_SSNTXN_D110A01_FPRS_20190617.DAT.gz
PB724_SSNTXN_D110A02_FKEN_20190617.DAT.gz

Power shell Script to update CSV file date time fields from UTC to local time

We have an automatic scheduled export of Salesforce data into CSV files in specific folder(everyday at 10pm).
In each CSV file there are two datetime columns which are in UTC time format [2018-01-30T05:27:26.000Z].
My requirement is to create a script to read those CSV files and update date time columns into local time zone and format (dd/mm/yyyy hh:mm:ss).
And I need to schedule this script to run everyday (everyday at 10.30pm).
(we know the folder path, file name, column name).
Please help with the script sample.
Thank you
Ditto to what Robert Cotterman has stated.
However, with This well could be considered a duplicate of this discussion.
Convert list of UTC to Current TimeZone
$UTCTime = GC "C:\Scripts\UTC.txt"
$results = '' | Select UTCTime,PST
Foreach ($newtime in $UTCTime){
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($newtime, $TZ)
$results.UTCTime = $newtime
$results.PST = $LocalTime
$results
}
This answer will get you started, and that last part is just use the script with a scheduled task.

Powershell - Find & Replace Dates

I'm looking for a bit of assistance here. I currently have a Powershell script which adjusts the dates within a file. I'm looking to stop myself having to manually adjust these dates every time. What I need is to replace the date two days ago, with the date from yesterday.
I believe that I'd have to use (Get-Date).AddDays(-1) and (Get-Date).AddDays(-2) but I'm not exactly sure how I'd script this in!
What I currently have:
echo "Adjusting Import Dates"
(Get-Content D:\Temp\Example.txt).replace('20180917', '20180918') | Set-Content D:\Temp\Example.txt
You could try this:
$yesterday = (Get-Date).AddDays(-1).tostring("yyyyMMdd")
$twodaysago = (Get-Date).AddDays(-2).tostring("yyyyMMdd")
(Get-Content D:\Temp\Example.txt).replace($twodaysago, $yesterday) | Set-Content D:\Temp\Example.txt
You just introduce variables for the two dates and format them to the required date format.
There is probably some other way of replacing in files, but the above should work.

Powershell/Batch file scripting - created time from file name

I want to use a file name to set the metadata created time for over 6,000 images in one folder.
The file name usually starts with the letters "IMG" then the date and then the letters "WA" with 4 numbers to represent the number of the picture on that date
i.e. IMG-YYYYMMDD-WA????
I've tried with advanced renamer and got it to work on my pc but the minute i move it back over to my device (galaxy s9) the created time changes to the time i move it.
Is there a way i can script this in powershell or batch?
I would recommend doing the following:
robocopy SourcePath DestinationPath /COPY:DT /R:1 /W:1 /DCOPY:DT /LOG:%TEMP%\PhotoCopy.log /NP
This will COPY all of the files from SourcePath recursively to DestinationPath, maintaining timestamps, retrying only once on failure, copying directory timestamps, and writing a log file to your system's Temporary folder called Photocopy.Log while displaying no per-file progress.
To change the CreationTime with PowerShell in place (adjust X:\Path)
without copying again:
Get-Chilitem X:\Path -File |
Where-Object BaseName -match '^IMG-(\d{8})-WA\d{4}$' |
ForEach-Object {
$_.CreationTime = [datetime]::ParseExact($Matches[1],'yyyyMMdd',$Null)
}
The script uses a RegEx to extract the date from the name and
converts it to a [datetime].
In lack of hour, minute and second these are set to 00:00:00.
The script doesn't care about the extension as long as the pattern
IMG-8digits-WA4digits matches.
PS: If the images aren't edited,
LastWriteTime could still reflect the date the images are taken. In that case you could copy that with
$_.CreationTime = $_.LastWriteTime

Amending timestamps using powershell or creating files in specified order from a csv

I have n number of files that have been created from a csv list by copying an original file using PowerShell:
Import-Csv C:\TEST\test.csv | % { Copy-Item -Path $_.oldfilepath –Destination "C:\TEST\$($_.newfilename)”}
Where .oldfile path is the original file and .newfilename is in the format xxxxx02012014.filetype
This takes the original file and creates as many copies as required using the names within the csv. However in doing so I lose the original/expected order of the files when viewing in explorer as they all appear to have been created at the same time (They are actually in ascending date order of the format of xxxxx02012014, xxxxx02082014 etc. where the number is a week in american date format).
Because of the required naming convention I cannot sort on name and due to how the files are produced (at the same timestamp) I cannot sort of time created within windows.
Is there either a way to ensure the files created and listed in the same order or something else I can run in PowerShell on the subsequently created files to modify the timestamps so they appear in windows explorer in the order required i.e in ascending american date format.
Thanks for any advice.
You can simply update the creation time of the copy with the creation timestamp from the original:
Import-Csv C:\TEST\test.csv | % {
$target = "C:\TEST\$($_.newfilename)"
Copy-Item -Path $_.oldfilepath –Destination $target
(Get-Item $target).CreationTime = (Get-Item $_.oldfilepath).CreationTime
}