PS script that monitors folder and copies to new folder and renames

I'm looking to fix a PowerShell script that I have and add some more features to it. Currently, it's working as intended: when a file is saved to a directory, it moves the item to a new one.

What I'm looking to add to it is instead of just overwriting the existing file, it renames it to yyyyMMdd. The files are all the same and are always the same name/extension, so adding this would be a great way for some semi version control.

$folder = 'C:\scripts\test'
$filter = '*.*' # <-- set this according to your requirements
$destination = 'H:\Office Documents\text_move'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ IncludeSubdirectories = $true # <-- set this according to your requirements NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { $path = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file '$name' was $changeType at $timeStamp" $dirname = [io.path]::GetDirectoryName($folder) $filename=[io.path]::GetFileNameWithoutExtension($file) $ext = [io.path]::GetExtension($file) $newpath = "$destination\$filename\$(get-date -f yyyyMMdd)$ext" Move-Item $path -Destination $destination -verbose
}

I get that I need to add to string the get-date, but I can't seem for the life of me figure out how or where to add that in to make it work the way I think it should.

2

2 Answers

You can try to use this logic to set the name of the file

$dirName = [io.path]::GetDirectoryName($folder)
$filename = [io.path]::GetFileNameWithoutExtension($file)
$ext = [io.path]::GetExtension($file)
$newPath = "$destination\$filename $(get-date -f yyyy-MM-dd)$ext"

Inspired by this answer

1
$folder = 'C:\scripts\test'
$filter = '*.*'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
# define the destination inside this script block
$destination = 'H:\Office Documents\text_move'
$createdFile = $Event.SourceEventArgs.FullPath
$createdFileName = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$createdFileTimeStamp = $Event.TimeGenerated
Write-Output "Trigger: Get-Date -Format 'u'" >> c:\scripts\logWatcher.txt
$existingFileName = Join-Path -Path $destination -ChildPath $createdFileName
if(Test-Path($existingFileName)) {
Write-Output "File: '$createdFileName' exists at: $destination - renaming existing file first" >> c:\scripts\logWatcher.txt
$newFileName = "$(get-date -Format 'yyyyMMdd')$createdFileName"
Rename-Item -Path $existingFileName -NewName $newFileName
}
Move-Item $createdFile -Destination $existingFileName -verbose
Write-Output "File: '$createdFileName' State: $changeType At: $createdFileTimeStamp" >> c:\scripts\logWatcher.txt
Start-Process
}
$onCreated

This is what I ended up using to make it work. I had lots of variable errors as well as lots of issues with where I needed certain things. I had help writing it as well as suggestions. The watcher log helped a lot because it gave me a point of reference to what I was doing and what was happening.

@echo off
powershell.exe -noexit -file "c:\scripts\move-filefinal.ps1"

In a batch file allows me to have it running non-stop in the background, if anyone has a better suggestion to that, I'm all ears.

Thanks for the help and pushing me in the right direction though, I think this was way harder than it needed to be.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like