none
copy-item $path $archivepath$filename"_ALL.pdf" RRS feed

  • Question

  • I have $Path, $archivepath and $filename. Also, $filter = *.pdf.  $filename = [io.path]::GetFileNameWithoutExtension($path). I want to copy all pdf files from $path and paste into $archivepath also i want to add _ALL in filename for all pdf files. The statement I am using is copy-item $path $archivepath$filename"_ALL.pdf". but it hans't work so far. Any suggestion, why it is not working ?

    Thank you,

    Kashyap

    Tuesday, May 30, 2017 7:56 PM

Answers

  • Wow ... I'm not really sure if I got what you're trying to do but with your original post in mind you could start with something like this:
    $path = 'D:\1\2\3\4\5\Master'
    $destination = 'D:\1\2\3\4\5\MasterStaging'
    $filter = '*.pdf'
    
    Get-ChildItem -Path $path -Filter $filter | 
        ForEach-Object{
            Copy-Item -Path $_.FullName -Destination $destination -PassThru | 
            Rename-Item -NewName ($_.BaseName + '_all' + $_.Extension)
        }
    


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by gurudevam Thursday, June 1, 2017 8:13 PM
    Tuesday, May 30, 2017 8:42 PM
  • Here is how to start testing the FSW to see if your strings will cause errors - they will:

    $path = 'D:\1\2\3\4\5\Master'
    $destination = 'D:\1\2\3\4\5\MasterStaging'
    $filter = '*.pdf'
    $action = {
    	$path = $Event.SourceEventArgs.FullPath
    	Write-Host "PATH: $path" -ForegroundColor green
    	$name = $Event.SourceEventArgs.Name
    	Write-Host "NAME: $name" -ForegroundColor green
    	
    	$today = Get-Date -format MM-dd-yyyy
    	Write-Host "$name $today Moved to Staging" -ForegroundColor green 
    	#| out-file 'D:\1\2\3\4\5\SplittingLog\splitting_$today.txt' -append
    	
    	$archivepath = "D:\1\2\3\4\5\MasterArchive\$today\"
    	#new-item $archivepath -ItemType directory #"
    	
    	$filename = [io.path]::GetFileNameWithoutExtension($path)
    	Write-Host "FILENAME: $filename" -fore green
    	
    	#copy-item $path $archivepath$filename"_ALL.pdf" -force
    	#Move-Item $path -Destination $destination -Force # Force will overwrite files with same name
    	write-host "OUTFILE: $path$archivepath$filename_ALL.pdf" -ForegroundColor green
    }
    
    unregister-event NewReports
    $fsw = New-Object IO.FileSystemWatcher($path, $filter)
    $fsw.NotifyFilter = 'FileName, LastWrite'
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier NewReports -Action $action


    \_(ツ)_/




    • Edited by jrvModerator Tuesday, May 30, 2017 8:54 PM
    • Marked as answer by gurudevam Thursday, June 1, 2017 8:13 PM
    Tuesday, May 30, 2017 8:52 PM
    Moderator

All replies

  • If you show your code we might be able to help ... 

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Tuesday, May 30, 2017 8:17 PM
  • Below is my code block  -

    unregister-event NewReports

    $path = "D:\1\2\3\4\5\Master"
    $destination = "D:\1\2\3\4\5\MasterStaging"
    $filter = "*.pdf"
    #Create a new filesystem watcher to monitor the master folder
    $fsw = New-Object IO.FileSystemWatcher $path, $filter -Property @{
     NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }

    #H
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier NewReports -Action {
     $path = $Event.SourceEventArgs.FullPath
     $name = $Event.SourceEventArgs.Name
     $changeType = $Event.SourceEventArgs.ChangeType
     $timeStamp = $Event.TimeGenerated
     $today = Get-Date -format "MM-dd-yyyy"
     $name + $today + " Moved to Staging" | out-file "D:\1\2\3\4\5\SplittingLog\splitting_$today.txt" -append -Encoding Ascii

     $archivepath = "D:\1\2\3\4\5\MasterArchive\" + $today +"\"
      new-item $archivepath -ItemType directory
      $filename = [io.path]::GetFileNameWithoutExtension($path)
     copy-item $path $archivepath$filename"_ALL.pdf" -force
     Move-Item $path -Destination $destination -Force # Force will overwrite files with same name
     write-host $path + $archivepath$filename"_ALL.pdf"
    }

    Tuesday, May 30, 2017 8:27 PM
  • You have to detect errors and report them.  Errors will cause issues and no output will be produced.

    Start by using just Write-Host to validate all strings and work back from there.


    \_(ツ)_/

    Tuesday, May 30, 2017 8:39 PM
    Moderator
  • Wow ... I'm not really sure if I got what you're trying to do but with your original post in mind you could start with something like this:
    $path = 'D:\1\2\3\4\5\Master'
    $destination = 'D:\1\2\3\4\5\MasterStaging'
    $filter = '*.pdf'
    
    Get-ChildItem -Path $path -Filter $filter | 
        ForEach-Object{
            Copy-Item -Path $_.FullName -Destination $destination -PassThru | 
            Rename-Item -NewName ($_.BaseName + '_all' + $_.Extension)
        }
    


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by gurudevam Thursday, June 1, 2017 8:13 PM
    Tuesday, May 30, 2017 8:42 PM
  • Here is how to start testing the FSW to see if your strings will cause errors - they will:

    $path = 'D:\1\2\3\4\5\Master'
    $destination = 'D:\1\2\3\4\5\MasterStaging'
    $filter = '*.pdf'
    $action = {
    	$path = $Event.SourceEventArgs.FullPath
    	Write-Host "PATH: $path" -ForegroundColor green
    	$name = $Event.SourceEventArgs.Name
    	Write-Host "NAME: $name" -ForegroundColor green
    	
    	$today = Get-Date -format MM-dd-yyyy
    	Write-Host "$name $today Moved to Staging" -ForegroundColor green 
    	#| out-file 'D:\1\2\3\4\5\SplittingLog\splitting_$today.txt' -append
    	
    	$archivepath = "D:\1\2\3\4\5\MasterArchive\$today\"
    	#new-item $archivepath -ItemType directory #"
    	
    	$filename = [io.path]::GetFileNameWithoutExtension($path)
    	Write-Host "FILENAME: $filename" -fore green
    	
    	#copy-item $path $archivepath$filename"_ALL.pdf" -force
    	#Move-Item $path -Destination $destination -Force # Force will overwrite files with same name
    	write-host "OUTFILE: $path$archivepath$filename_ALL.pdf" -ForegroundColor green
    }
    
    unregister-event NewReports
    $fsw = New-Object IO.FileSystemWatcher($path, $filter)
    $fsw.NotifyFilter = 'FileName, LastWrite'
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier NewReports -Action $action


    \_(ツ)_/




    • Edited by jrvModerator Tuesday, May 30, 2017 8:54 PM
    • Marked as answer by gurudevam Thursday, June 1, 2017 8:13 PM
    Tuesday, May 30, 2017 8:52 PM
    Moderator
  • @Grüße  - I am trying multiple things in my original code - copy and rename at once, than move from original to another folder. Then $today to create subfolder naming today's date. And all this action - recording into splitting_$today.txt file. What you suggest does work. Still don't understand is same code works which i posted, does works for someone else !

    @JRV - i am trying your codes with actual path.

    Thank  you both,

    Kashyap

    Tuesday, May 30, 2017 9:15 PM