I would like to create a powershell which must rename a file by adding the current date then move it to another folder (ie.e to archive it). the script should be automatically executed every day at a specific start time (10:OO PM)
I wonder if someone could help me to do that.
Thanks in advance !
this is not how stackoverflow works but i had this part ready
$destpath = 'C:\TEMP\XMLTRANS\Backup\'+(get-date -Format
yyyyMMddHHmm)+'.xml' $sourcepath = 'C:\TEMP\XMLTRANS\FILE.xml'
Move-Item -Path $sourcepath -Destination $destpath
you can run this script behind a scheduler or Cron
hope it help !
This script will rename a file with the addition of a date/time stamp and move to an another folder.
A mention to #alex above as I used his script and made a slight change. I wanted to keep the original file name but add the date/time stamp. Thank you Alex, very simple script for a newbie like me :)
$fileName = 'My Test File_'
$destpath = 'G:\ArchiveFolder\'+ $filename + (get-date -Format yyyyMMddHHmm)+'.sql'
$sourcepath = 'G:\OriginalFolder\My Test File.sql'
Move-Item -Path $sourcepath -Destination $destpath
Hope it helps?
Related
I need a script that only copy files after 5 minutes based on the modification date. Does anyone have a solution for this ?
I couldn't find any script online.
The answer from jdweng is a good solution to identify the files in scope.
You could make your script something like this to easily re-use it with other paths or file age.
# Customizable variables
$Source = 'C:\Temp\Input'
$Destination = 'C:\Temp\Output'
[int32]$FileAgeInMinutes = 5
# Script Execution
Get-ChildItem -Path $Source | Where-Object { $_.LastWriteTime -lt (Get-Date).AddMinutes(-$FileAgeInMinutes) } | Copy-Item -Destination $Destination
You could then run a scheduled task using this script and schedule it to run in periodically, depending on your need.
I am working out a scenario using PowerShell.
As of now, I am trying to workout a scenario where I have a batch job which upon successful execution first creates a date folder for that particular day and then creates a .CSV file under. The folder structure than look like below.
\\Server\SourceA\Processed\20200120\TestA.CSV
when the job runs next day, it will create another folder and file like below.
\\Server\SourceA\Processed\20200121\TestB.CSV
This is how there have been so many folders already created in past.
My PS script is good to run daily after the batch job is completed. I read a date, append to path and it copies file from source to destination folder. But I want to enable my PS script to read all previous date folders created under
\\Server\SourceA\Processed\
Another tricky part is - under the date folder there is few other sub folders. I.e.
\\Server\SourceA\Processed\20191010\Log
\\Server\SourceA\Processed\20191010\Charlie
\\Server\SourceA\Processed\20191010\Alpha
\\Server\SourceA\Processed\20191010\Delta
among them, I only need to read files from Log folder only.
Hence, my actual source path becomes like
\\Server\SourceA\Processed\20191010\Log\TestA.CSV
Here is my script (which is static right now and unable to read past existing date folders).
$fullSourceFileName = "\\Server\SourceA\Processed\"
$date = Get-Date -format "yyyyMMdd"
$fullSourceFileName = "$($fullSourceFileName)$($date)\Log
$destination = "\\Server\DestA\Processed\"
$destination = "$($destination)$($date)\"
get-childitem -path $fullSourceFileName -recurse | copy-item -destination "$($destinationFolder)$($date)\"
Your help is highly appreciated.
I did not know I can use foreach loop in Powershell.
So, here is the answer to read all dynamic date folder under my given path.
I hope this helps the community.
$fullSourceFileName = "\\Server\SourceA\Processed\"
$DirToRead = "\Log\"
$dates = Get-ChildItem -Path $fullSourceFileName -Directory
$destination = "\\Server\DestA\Processed\"
foreach ($date in $dates){
$ActualPath = "$($fullSourceFileName)$($date)$($DirToRead)"
if(!(Test-Path $ActualPath))
{
Write-Output $($ActualPath)$(" source path does not exist")
}
else
{
get-childitem -path $ActualPath -recurse | copy-item -destination "$($destinationFolder)$($date)\"
}
$ActualPath = ""
}
I have a few thousand pictures & videos in a directory and i want to change all the lastAccess, lastWrite,... dates.
My files are all named like 'IMG_20170101_120000.png'.
I want to change for every File the lastAccessDate as the date is in the filename.
Surely there are also other ways, but i found some statements to run it in powershell
$(ls IMG_20170101_120000.png).LastAccessTime = (Get-Date "2017-01-01T12:00:00")
But now i want to do this for all these files in the directory automatically.
Somebody who can help me?
Thank you for your help!
This code should do what you want.
$Files = Get-ChildItem -Path $YourPath -Filter "*.png"
Foreach($File in $Files){
try{
$FileName = $File.BaseName
$FileName =$FileName.Replace("IMG_","")
$FileName =$FileName.Replace("_","")
$File.LastAccessTime = [datetime]::ParseExact("$($filename.Substring(0,4))-$($filename.Substring(4,2))-$($filename.Substring(6,2)) $($filename.Substring(8,2)):$($filename.Substring(10,2))", "yyyy-MM-dd HH:mm", $null)
}catch{"Wrong filename"}
}
Please take a look in the powershell documentation, because your code is hard to read, and will not work.
Firstly, Hello and apologies for the stupidly long title...
Secondly, I hope someone out there can help with what should be a simple task that has annoyed me for the past 4days, I will elaborate;
I have 4 x BAK file that are created between 22:30 and 23:00hrs each night.
Each BAK file is named differently and we append the date in format "yyyy_MM_dd".
I need to 7z each BAK file into separate archives and move them to a new directory either named for the date they where created or the appended date of the file but keep the format "yyyy_MM_dd". (Both will be the same obviously but the code will be different, so whichever is easiest)
I believe I have the separate lines for some of the script I need...
For Creating the 7z
dir *.bak | ForEach-Object { & "C:\Program Files\7-Zip\7z.exe"
a -t7z -mx3 ($.Name+".7z") $.Name }
For Creating the folder
$Folder = New-Item -ItemType Directory -Path
"DRIVE2:\Folder1\Folder2\$((Get-Date).ToString('yyyy-MM-dd'))"
For Moving the files
Get-ChildItem 'DRIVE1:\Folder1\Folder2\*.7z' | Copy-Item
-Destination $Folder
Can someone point out where I am being a complete nugget with this?
Thanks in advance
RobD
If the files to copy are in DRIVE1:\Folder1\Folder2, then you are missing a slash between the folder and the wildcard.
Get-ChildItem 'DRIVE1:\Folder1\Folder2\*.7z' | Copy-Item -Destination $Folder
I'm moving some folders and all their content with PowerShell Move-Item, but when I moved the folder its date modified is the current date not the original. How can I move the folder without changing its last modified date.
Here's a powershell function that'll do what you're asking... It does absolutely no sanity checking, so caveat emptor...
function Move-FileWithTimestamp {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)][string]$Path,
[Parameter(Mandatory=$true,Position=1)][string]$Destination
)
$origLastAccessTime = ( Get-Item $Path ).LastAccessTime
$fileName = ( Get-Item $Path ).Name
Move-Item -Path $Path -Destination $Destination
$(Get-Item ($Destination+'\'+$fileName)).LastAccessTime = $origLastAccessTime
}
Once you've run loaded that, you can do something like:
Move-FileWithTimestamp c:\foo.txt c:\newFolder
There's no simple way to do this in windows but you can use robocopy. There is an option to copy the modified date attribute.
Run help on robocopy (robocopy /?) and look for /COPY and /COPYALL options.
This only works in Vista and newer Windows versions.