Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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:\TEMP\test"
        $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:\TEMP\test\log\log.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:\test\archive\) 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.

  • #2
    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:\TEMP\test"
        $watcher.Filter = "*.*"
        $watcher.IncludeSubdirectories = $false
        $watcher.EnableRaisingEvents = $true  
    
    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
        $action = { Get-ChildItem C:\TEMP\test\*.txt | Move-Item -Destination C:\TEMP\test\Archive
                    Get-ChildItem C:\TEMP\test\Archive\*.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.

    Comment


    • #3
      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:

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

      Network Consultant/Engineer
      Baltimore - Washington area and beyond
      www.gma-cpa.com

      Comment


      • #4
        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!

        Comment

        Working...
        X