# 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. • Howdy! Ss far all you've done is tell us what you want and give us the script you have that doesn't do it. We are not a script writing service, but can/will help with specific scripting problem you run into. Please edit your question to include answer to: What have you attempted already? What were the results? Where exactly are you getting stuck implementing this new feature? Otherwise this may/will be closed as too broad. – Ƭᴇcʜιᴇ007 Nov 25 '15 at 15:04 • My mistake, I did update one of the things I tried. I've tried lots of other ways but I either A: break everything or B: it just seems to go over it and it still overwrites it. – Matt King Nov 25 '15 at 15:22 ## 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"


• Playing around with what you wrote, its now adding a new file instead of overwriting the old one, so in an instance, I would just get New Microsoft Word Document, I'm now getting New Microsoft Word Document (2).docx Certainly a step in the right direction as before it would just overwrite. – Matt King Nov 25 '15 at 18:32
$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 https://website.blah/app.jnlp }$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.