1
votes

I have amended this to a more simpler explanation below and removed my previous version

Good Afternoon

i was wondering if anyone could advise

Each day we have alogfile which bears the current date, eg 22012013.txt, 23012013.txt etc

I have a move item cmdlet in my script, i would like to record how many files have been moved to 3 specific folders each day and writing the counter to the dated text log mentioned above

Pretend this is my folder structure

folder1

folder2 folder3

As an example, here is how my move-item would work

my move item moves file1.txt to folder1

file2 to folder3

file3 to folder1

file4 to folder3

file5 to folder2

file6 to folder1

In the log file, i would like to see

Items moved to Folder1 = 3

Items moved to Folder2 = 1

Items moved to Folder3 = 2

And that is it as the next day, that days file moves will be recorded in the new log file for that day, i would like the increment to go up for each move item if this is possible

Hope this makes sense

Regards

Barrie

1

1 Answers

0
votes

Here is an example implementation of a function which moves a file and then updates a log.
No doubt it could be more concise, but it gets the job done and is reasonably readable.

The first argument is the file to be moved, the second is the name of the destination folder (it must not contain whitespace).

Basically, after moving the file to the specified folder, the last line of the log file is grabbed and checked to see if it contains today's date. If it does, the line is split on whitespace and the resulting array is iterated to find the folder name. If found, the next item of the array, which will be the number of moves made to that folder, is incremented by one. If not found, the folder name is appended to the line. The amended line then replaces the last line in the file. If the last line of the log file does not contain today's date a line is appended to the file with today's date and the folder name etc.

@() is used to ensure that the enclosed expression returns an array - it makes it easier to add content to the file as proper lines.

 function Log-Move() {    
   $logfile = 'O:\AutoScan\log.txt'
   $destination = 'O:\AutoScan\'
   $folder = $args[1]    
   Move-Item -ErrorAction Stop $args[0] ( $destination + $folder )        
   $content = @( get-content -path $logfile )
   $line = $content[-1]
   $date = Get-Date -format 'd'    
   if ( $line ) {    
     if ( $line.Contains( $date ) ) {
       $items = $line.split()
       $count = $items.count         
       for ( $i = 0; $i -lt $count; $i++ ) {
         if ( $items[$i] -eq $folder ) {
           $items[$i + 1] = 1 + $items[$i + 1]
           break
         }
       }
       if ( $i -eq $count ) {
         $items += @( $folder, '1' )
       }
       $line = $items -join ' '
       if ( $content.length -gt 1 ) {
         $content = @( $content[0..$($content.length-2)] ) + $line
       } else {
         $content = @( $line )
       } 
     } else {
       $content += $date + ' ' + $folder + ' 1'
     }       
   } else {
     $content = @( $date + ' ' + $folder + ' 1' )
   }
   $content | Set-Content -path $logfile
 }

Example usage

Log-Move $newLongFilename Multiples
# log.txt:
# 22/01/2013 Multiples 1

Log-Move $anotherfile Multiples
Log-Move $anotherfile Autosorting
# 22/01/2013 Multiples 2 Autosorting 1