PowerShell script to Move/Rename (w/Date and Time stamp) a file with random name

Home Forums Scripting PowerShell PowerShell script to Move/Rename (w/Date and Time stamp) a file with random name

This topic contains 3 replies, has 2 voices, and was last updated by CaTBuS CaTBuS 3 years, 9 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • CaTBuS
    CaTBuS
    Participant
    #165606

    I’m trying to rework a PowerShell script that does the following: It monitors a specific directory; when a new file is created in said directory (*.txt), a log file is created/updated with the time stamp and name of the newly created file. The code is:

    Code:
    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = “C:TEMPtest”
    $watcher.Filter = “*.*”
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true

    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $logline = “$(Get-Date), $changeType, $path”
    Add-content “C:TEMPtestloglog.txt” -value $logline
    }
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
    $created = Register-ObjectEvent $watcher “Created” -Action $action
    while ($true) {sleep 5}

    I’d like to get rid of the current “logging” action and build a new one that does the following. When a new *.txt file is created, it is moved into an archive directory (C:testarchive) and renamed something like capture_%date/timestamp%.txt

    I’ve found loads of scripts that will move/rename but only using $OldName $NewName variables. In my case, I won’t know the name of the text file being generated as it is random so can’t use any of these methods. I can’t seem to find a way to use the wild card method used above for the logging action.

    Looking forward your ideas! Thanks in advance.

    CaTBuS
    CaTBuS
    Participant
    #309109

    I have worked this out myself. Here is what I came up with:

    Code:
    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = “C:TEMPtest”
    $watcher.Filter = “*.*”
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true

    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { Get-ChildItem C:TEMPtest*.txt | Move-Item -Destination C:TEMPtestArchive
    Get-ChildItem C:TEMPtestArchive*.txt | Foreach {Rename-Item -Path $_.Fullname -newname “$($_.DirectoryName)$($_.BaseName) $(get-date -format [email protected])$($_.extension)”}
    }
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
    $created = Register-ObjectEvent $watcher “Created” -Action $action
    while ($true) {sleep 10}

    While this code does work, it is buggy. The renaming process is greatly effected by “when” the file is created. During my testing, I got some pretty weird file names. Some had the date/time stamp 2 or 3 times in one file name. Other files had the “basename” disregarded and were renamed “New Text File.” Although this was a fun endeavor, I think I need to completely rethink this.

    JeremyW
    JeremyW
    Moderator
    #271330

    Your logic is slightly off. In the script you’re working on all the files in the archive folder each time the event is triggered. I haven’t tested your code but I added a working directory so that should take care of the logic issue:

    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = “C:TEMPtest”
    $watcher.Filter = “*.*”
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true

    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { Get-ChildItem C:TEMPtest*.txt | Move-Item -Destination C:TEMPtestWorking
    Get-ChildItem C:TEMPtestWorking*.txt | Foreach {Rename-Item -Path $_.Fullname -newname “$($_.DirectoryName)$($_.BaseName) $(get-date -format [email protected])$($_.extension)”}
    Get-ChildItem C:TEMPtestWorking*.txt | Move-Item -Destination C:TEMPtestArchive
    }
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
    $created = Register-ObjectEvent $watcher “Created” -Action $action
    while ($true) {sleep 10}[/CODE][CODE]### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = “C:TEMPtest”
    $watcher.Filter = “*.*”
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true

    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { Get-ChildItem C:TEMPtest*.txt | Move-Item -Destination C:TEMPtestWorking
    Get-ChildItem C:TEMPtestWorking*.txt | Foreach {Rename-Item -Path $_.Fullname -newname “$($_.DirectoryName)$($_.BaseName) $(get-date -format [email protected])$($_.extension)”}
    Get-ChildItem C:TEMPtestWorking*.txt | Move-Item -Destination C:TEMPtestArchive
    }
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
    $created = Register-ObjectEvent $watcher “Created” -Action $action
    while ($true) {sleep 10}[/CODE]

    CaTBuS
    CaTBuS
    Participant
    #309110

    JeremyW, I just say your reply. I will try this out, hopefully this will remedy some buggy behavior I’ve been trying to fix. Thanks!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Register for this Petri Webinar!

Software-Defined Unlimited Backup Storage

Tuesday, August 27, 2019 @ 1:00 pm EDT

A Scale-Out Backup storage infrastructure is a must-have technology for your backups. In this webinar, join expert Rick Vanover for a look on what real-world problems are solved by the Scale-Out Backup Repository.

Register Now

Sponsored By