none
Renaming file after download. RRS feed

  • Question

  • We have an SFTP connection that brings in files weekly. This file comes in with the format 1234Filenameyyyymmdd.csv 

    I need to append the existing STFP batch file to simply add a period between "1234" and "Filename" and unfortunately this is not possible from the source location, it has to be done on our side. 

    Any help would be greatly appreciated. 

    Monday, August 6, 2018 9:28 PM

Answers

  • Since the request is fairly short and simple, here's a PowerShell example:


    Get-ChildItem "*.csv" | Where-Object { $_.Name -match '^\d+[^\.\d]' } | ForEach-Object {
      $_ | Rename-Item -NewName ($_.Name -replace '^(\d+)', '$1.')
    }
    


    -- Bill Stewart [Bill_Stewart]

    Monday, August 6, 2018 10:48 PM
    Moderator

All replies

  • 1) Of necessity, this forum is not a code-on-demand service. Read this first:

    This forum is for scripting questions rather than script requests

    2) What problem does renaming these files actually solve? (Why do you need the dot in the filename?)


    -- Bill Stewart [Bill_Stewart]

    Monday, August 6, 2018 9:52 PM
    Moderator
  • Fair enough. Apologies for breaking the rules. Its a standardized naming convention we get from multiple external sources. As to what function it serves thats really out of my department, i think it has something to do with how other departments applications read the files. 
    Monday, August 6, 2018 10:09 PM
  • Since the request is fairly short and simple, here's a PowerShell example:


    Get-ChildItem "*.csv" | Where-Object { $_.Name -match '^\d+[^\.\d]' } | ForEach-Object {
      $_ | Rename-Item -NewName ($_.Name -replace '^(\d+)', '$1.')
    }
    


    -- Bill Stewart [Bill_Stewart]

    Monday, August 6, 2018 10:48 PM
    Moderator
  • Thanks Bill, I really appreciate it. Ill look into that, I'm still new to scripting so it'll take me some time to understand exactly what does what in that. 
    Monday, August 6, 2018 11:28 PM