PowerShell File Rename after download

Bron Tamulis 41 Reputation points
2021-12-22T20:44:25.347+00:00

Okay - New to Powershell - inherited someone else's work.....

Basic Process -

  1. Find the latest billings file on network (get item works great)
  2. Download it to a folder maintaining the file name (I can't seem to get this)
  3. Rename the source file by adding a .processed to the file name (no clue how to do this)

Here's what I got so far:

Source URL

$url = (Get-ChildItem -Path "\10.50.11.41\APFMFN\" | Sort-Object LastWriteTime -Descending)[0].FullName
--------url works great

Destination file

$dest = "\usw2gpapp01\Bron_GPguy\SmartConnect\billings-$(get-date -f yyyyMMddhhmmss).csv"
--------dest works great

------Need to add a step where we rename the file adding a '.processed' to end of the URL file

Download the file

Invoke-WebRequest -Uri $url -OutFile $dest

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2021-12-23T00:38:46.953+00:00

    Okay - New to Powershell

    Here is good reference to start with.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    --------url works great

    Yeah, but it's not a url. It a UNC path to a file. That's not something that you can use Invoke-WebRequest against. A url should be something like "HTTP://SiteName/Folder/SomeName". Not "\10.50.11.41\APFMFN\".

    --------dest works great

    It also is a UNC path. That's what the leading "\" indicates.

    Download it to a folder maintaining the file name (I can't seem to get this)

    Invoke-WebRequest requires a url. That needs to start with HTTP:// or HTTPS://. If you have a UNC path you need to use Copy-Item because you are using SMB (network shares).

    copy-item -Path $url -Destination $dest   
    

    Rename the source file by adding a .processed to the file name (no clue how to do this)

    Just use Rename-Item.

    $NewName = $url + ".processed"                           #   $url contains a UNC path, not a real url. 
    Rename-Item -Path $url -NewName $NewName
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-12-22T22:09:54.69+00:00

    See if this works:

    $f = [system.io.path]::GetFileName("$url")
    Rename-Item -Path "\\10.50.11.41\APFMFN\$f" -NewName ($f + ".processing")
    
    0 comments No comments

  2. Bron Tamulis 41 Reputation points
    2021-12-23T18:07:36.483+00:00

    I tried MotoX80 suggestion and it kind of worked.

    It re-named the newest file in the folder (not the file I had downloaded).

    In the folder are multiple files - some start with 'billings' (my getchild item does properly grab the latest billings file). There are other files in same folder - invoices, gppayments, etc.

    $NewName = $url + ".processed" # $url contains a UNC path, not a real url.
    Rename-Item -Path $url -NewName $NewName

    when I added this it renamed a gppayment file that was 'newer' than the billings file I grabbed


  3. Bron Tamulis 41 Reputation points
    2021-12-23T18:50:50.727+00:00

    Here's my current script - it works except it doesn't add the .processed to my billings file that i downloaded

    Source URL

    $url = (Get-ChildItem -Path "\10.50.11.41\APFMFN\" | Sort-Object LastWriteTime -Descending)[0].FullName

    Destination file

    $dest = "\usw2gpapp01\GPShare\SC_Import\Source\billings-$(get-date -f yyyyMMddhhmmss).csv"

    Download the file

    Invoke-WebRequest -Uri $url -OutFile $dest

    I'm new so I am open to a complete re-write using the copy file method rather than Invoke web request. I realize invoke web request was designed to connect to a web site versus internal network shred folders.


  4. Bron Tamulis 41 Reputation points
    2021-12-27T23:36:09.697+00:00

    Actually - Thank you to everybody.

    1. I used the copy item instead of invoke webrequest.
    2. i added the filter suggested to assure latest billings only file.
    3. I added the rename file method as well.
    4. I tested it on my laptop and it works beautifully.

    Here's the finished code:

    # Source URL
    $url = (Get-ChildItem -Path "C:\Users\btamu\Desktop\APFM Docs\Data requests" -File -filter 'billings*'  | Where-Object {$_.name -notmatch 'processed'} | Sort-Object LastWriteTime -Descending)[0].FullName 
    
    # Destination file
    $dest = "C:\TEMP\billings-$(get-date -f yyyyMMddhhmmss).csv"
    
    # Download the file
     copy-item -Path $url -Destination $dest  
    
      $NewName = $url + ".processed"                            
     Rename-Item -Path $url -NewName $NewName
    

    Thank you to all that assisted.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.