locked
Parametreli yalnızca bir dosya alın RRS feed

  • Question

  • Merhaba 

    Sadece parametreyle belirttiğim.

    Kodlar seçeneği. Bana yardımcı olabilir misiniz?

        Bu kodlar tüm klasörün içinde pdf kullanılarak çalışır. Sadece parametreyle tanın ettirmek istiyorum

    param (
      [Parametre (Zorunlu = $ Doğru, kalkış = 0)] [string] $ apikey,
      [Parametre (Zorunlu = $ Doğru, uç = 1)] [string] $ indir,
      [Parameter (Zorunlu = $ True, Position = 2)] [string] $ outdir,
      [string] $ biçim = "xlsx-single"
    )

    $ ErrorActionPreference = 'Dur'

    Add-Type -AssemblyName 'System.Net.Http'

    $ url = 'https://pdftables.com / api? format = '+ $ biçim +' & key = '+ $ apikey

    $ files = Get-ChildItem -Path $ indir -Dosya -Filter * .pdf |
    Foreach-Object {
      $ outsubdir = $ outdir + $ _. Directory.FullName.Substring ($ awLength)
      $ outfile = $ outdir + $ _. FullName.SubString ($ awLength, $ _. FullName.Length - $ doLength - 3) + $ format.Split ('-') [0]
      Yeni Öğe -ItemType Dizini -Force -Path $ dış kaynak
      Yazma-Host (' Değerme '' + $ _. FullName + '"için" + $ outfile +' "')

      şu {
        $ client = Yeni Nesne System.Net .Http.HttpClient
        $ istek = Yeni Nesne System.Net.Http.HttpRequestMessage ([Sistem .Net.Http.HttpMethod] :: Gönderi, $ url)

        $ content = Yeni Nesne System.Net.Http.MultipartFormDataContent
        $ fileStream = [System.IO.File] :: OpenRead ($ _. FullName)
        $ fileContent = Yeni Nesne System.Net.Http.StreamContent ($ fileStream)
        $ content.Add ($ fileContent, 'Kullanmamış',

        $ request.Content = $ content 
        $ sonuç = $ client.SendAsync ($ istek, [System.Net.Http.HttpCompletionOption] :: ResponseHeadersRead) .GetAwaiter (). GetResult ()
        $ stream = $ sonuç.Content.ReadAsStreamAsync (). GetAtter () GetResult ()
        $ fileStream = [System.IO.File] :: Oluştur ($ outfile)
        $ akış.CopyTo ($ fileStream)
      }
      Yakalama {
        Yazma Hatası $ _
        exit 1
      }
      Sonunda {
        if ($ fileContent -ne $) null) {$ fileContent.Dispose ()}
        if ($ fileStream -ne $ null) {$ fileStream.Dispose ()}
        if ($ content -ne $ null) {$ content.Dispose ()}
        if ($ request -ne $ null) {$ request.Dispose ()}
        eğer ($ client -ne $ null) {$ client.Dispose ()}
      }
    }

    • Edited by Aydogann Monday, December 17, 2018 10:26 AM
    Friday, December 14, 2018 2:25 PM

Answers

  • I solved the problem.

    Thanks.

    param (
      [Parameter(Mandatory=$True,Position=0)][string]$apikey,
      [Parameter(Mandatory=$True,Position=1)][string]$infile,
      [string]$format = "xlsx-single"
    )

    $ErrorActionPreference = 'Stop'

    Add-Type -AssemblyName 'System.Net.Http'

    $url = 'https://pdftables.com/api?format=' + $format + '&key=' + $apikey
    $outfile = $infile.Split('.')[0] + '.' + $infile.Split('.')[1] + "." + $format.Split('-')[0]

    Write-Host ('Converting "' + $infile + '" to "' + $outfile + '"')

    Try {
      $client = New-Object System.Net.Http.HttpClient
      $request = New-Object System.Net.Http.HttpRequestMessage([System.Net.Http.HttpMethod]::Post, $url)

      $content = New-Object System.Net.Http.MultipartFormDataContent
      $fileStream = [System.IO.File]::OpenRead($infile)
      $fileContent = New-Object System.Net.Http.StreamContent($fileStream)
      $content.Add($fileContent, 'unused', 'unused')

      $request.Content = $content 
      $result = $client.SendAsync($request, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).GetAwaiter().GetResult()
      $stream = $result.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
      $fileStream = [System.IO.File]::Create($outfile)
      $stream.CopyTo($fileStream)
    }
    Catch {
      Write-Error $_
      exit 1
    }
    Finally {
      if ($fileContent -ne $null) { $fileContent.Dispose() }
      if ($fileStream -ne $null) { $fileStream.Dispose() }
      if ($content -ne $null) { $content.Dispose() }
      if ($request -ne $null) { $request.Dispose() }
      if ($client -ne $null) { $client.Dispose() }
    }

      
    • Marked as answer by Aydogann Monday, December 17, 2018 7:00 PM
    Monday, December 17, 2018 7:00 PM

All replies

  • I don't think you've named the file! You're going to get every PDF file in the "$indir" with this code:

    $files = Get-ChildItem -Path $indir -File -Filter *.pdf |

    If you change that to 

    $files = Get-ChildItem -Path $indir

    And give the full path to the file as the value to the $indir parameter you'd only retrieve one file.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, December 14, 2018 4:01 PM
  • Hello there

    this way I get an error in the parameters I entered this way

    apikey: ttggsavbcs
    indir: C:\Users\aydogan.ilhan\Desktop\Bomlar\merge.pdf
    outdir: C:\Users\aydogan.ilhan\Desktop\Bomlar\merge.xlsx
    Foreach-Object : You cannot call a method on a null-valued expression.
    At C:\Users\aydogan.ilhan\Desktop\pdf\pdftables-convert-dir.ps1:15 char:1
    + Foreach-Object {
    + ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [ForEach-Object], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull,Microsoft.PowerShell.Commands.ForEachObjectCommand

    Saturday, December 15, 2018 4:40 AM
  • get-childitem -path C:\Users\aydogan.ilhan\Desktop\Bomlar\merge.pdf

    If the ForEach is failing then the Get-ChildItem didn't find the file. If you just run the above line does it find the file?

    Also, you use the $indir variable in a few other places in your script. Make sure that you're using it in a way that makes sense now that it holds a complete path and not just a directory name.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, December 15, 2018 3:55 PM
  • A little note.  A pipeline cannot fail on a null return as it just does nothing.

    Example:

     Get-ChildItem c:\ -Filter test.what|Foreach{'peocessing'}

    This is by design and is one reason we use the pipeline.

    A null path variable does not cause an error.

    Remove-item x -force -ErrorAction 0
    Get-ChildItem -Path $x

    The error posted does not match the code as posted.

    Line 15 is the following:

    $url = 'https://pdftables.com/api?format=' + $format + '&key=' + $apikey

    This is not the line causing the error.


    \_(ツ)_/

    Saturday, December 15, 2018 7:45 PM
  • Please post your code correctly. Edit your original post and use the code posting tool provided.  Also be sure you are posting the correct code and the complete error message.

    Please remove al of the repeated junk in your post.


    \_(ツ)_/

    Saturday, December 15, 2018 7:46 PM
  • Also note that you must remove the following line to get a correct error message.

    $ErrorActionPreference = 'Stop'

    API calls will always be automatically trapped in a Try/Catch block.  For CmdLets to be trapped they must be inside of the block.  You CmdLets that may throw an exception are not in a block so the errors will go unseen.


    \_(ツ)_/

    Saturday, December 15, 2018 7:51 PM
  • Merhaba

    , dosyayı bu komutla bulabilirim

    

    Monday, December 17, 2018 8:15 AM
  • param (
      [Parameter(Mandatory=$True,Position=0)][string]$apikey,
      [Parameter(Mandatory=$True,Position=1)][string]$indir,
      [Parameter(Mandatory=$True,Position=2)][string]$outdir,
      [string]$format = "xlsx-single"
    )



    Add-Type -AssemblyName 'System.Net.Http'

    $url = 'https://pdftables.com/api?format=' + $format + '&key=' + $apikey

    $files = Get-ChildItem $indir  -File -Filter *.pdf |
    Foreach-Object {
      $outsubdir = $outdir + $_.Directory.FullName.Substring($indir.Length)
      $outfile = $outdir + $_.FullName.Substring($indir.Length, $_.FullName.Length - $indir.Length - 3) + $format.Split('-')[0]
      New-Item -ItemType Directory -Force -Path $outsubdir
      Write-Host ('Dönüştürülüyor "' + $_.FullName + '" to "' + $outfile + '"')

      Try {
        $client = New-Object System.Net.Http.HttpClient
        $request = New-Object System.Net.Http.HttpRequestMessage([System.Net.Http.HttpMethod]::Post, $url)
        
        $content = New-Object System.Net.Http.MultipartFormDataContent
        $fileStream = [System.IO.File]::OpenRead($_.FullName)
        $fileContent = New-Object System.Net.Http.StreamContent($fileStream)
        $content.Add($fileContent, 'unused', 'unused')

        $request.Content = $content 
        $result = $client.SendAsync($request, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).GetAwaiter().GetResult()
        $stream = $result.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
        $fileStream = [System.IO.File]::Create($outfile)
        $stream.CopyTo($fileStream)
      }
      Catch {
        Write-Error $_
        exit 1
      }
      Finally {
        if ($fileContent -ne $null) { $fileContent.Dispose() }
        if ($fileStream -ne $null) { $fileStream.Dispose() }
        if ($content -ne $null) { $content.Dispose() }
        if ($request -ne $null) { $request.Dispose() }
        if ($client -ne $null) { $client.Dispose() }
      }
    }
    Monday, December 17, 2018 9:58 AM
  • Is there a question? 

    Please post code correctly with the code posting tool on the edit bar.


    \_(ツ)_/

    Monday, December 17, 2018 11:02 AM
  • Monday, December 17, 2018 12:24 PM
  • Monday, December 17, 2018 12:30 PM
  • I repeat.

    Is there a question? 

    Please post code correctly with the code posting tool on the edit bar.


    \_(ツ)_/

    Monday, December 17, 2018 6:45 PM
  • I solved the problem.

    Thanks.

    param (
      [Parameter(Mandatory=$True,Position=0)][string]$apikey,
      [Parameter(Mandatory=$True,Position=1)][string]$infile,
      [string]$format = "xlsx-single"
    )

    $ErrorActionPreference = 'Stop'

    Add-Type -AssemblyName 'System.Net.Http'

    $url = 'https://pdftables.com/api?format=' + $format + '&key=' + $apikey
    $outfile = $infile.Split('.')[0] + '.' + $infile.Split('.')[1] + "." + $format.Split('-')[0]

    Write-Host ('Converting "' + $infile + '" to "' + $outfile + '"')

    Try {
      $client = New-Object System.Net.Http.HttpClient
      $request = New-Object System.Net.Http.HttpRequestMessage([System.Net.Http.HttpMethod]::Post, $url)

      $content = New-Object System.Net.Http.MultipartFormDataContent
      $fileStream = [System.IO.File]::OpenRead($infile)
      $fileContent = New-Object System.Net.Http.StreamContent($fileStream)
      $content.Add($fileContent, 'unused', 'unused')

      $request.Content = $content 
      $result = $client.SendAsync($request, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).GetAwaiter().GetResult()
      $stream = $result.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
      $fileStream = [System.IO.File]::Create($outfile)
      $stream.CopyTo($fileStream)
    }
    Catch {
      Write-Error $_
      exit 1
    }
    Finally {
      if ($fileContent -ne $null) { $fileContent.Dispose() }
      if ($fileStream -ne $null) { $fileStream.Dispose() }
      if ($content -ne $null) { $content.Dispose() }
      if ($request -ne $null) { $request.Dispose() }
      if ($client -ne $null) { $client.Dispose() }
    }

      
    • Marked as answer by Aydogann Monday, December 17, 2018 7:00 PM
    Monday, December 17, 2018 7:00 PM