Harry
Harry

Reputation: 2951

Upload file to FTP using PowerShell but File name dynamic

I have multiple files that needs to be sent to an FTP site. These files are always .CSV but the file name keeps changing.

I have a PowerShell script that can send the files across successfully. But the code has the file name hard coded. I can't seem to find a way to pick the file name dynamically and write it to the FTP site with the dynamic file name. (hope that made sense)

Can someone please help?

Many thanks

Upvotes: 0

Views: 605

Answers (1)

Chris N
Chris N

Reputation: 7499

Is there another attribute that you can use? For example in my case I often have to choose the newest file in the directory. I've done something similar using the LastWriteTime attribute.

$lastWrittenCSV = Get-ChildItem -Path C:\csvFiles\ -Filter *.csv | Sort-Object -Descending LastWriteTime | Select-Object -First 1

Then you can run your script against $lastWrittenCSV.FullName (the fully path) and upload it as $lastWrittenCSV.Name (just the name of the file).

You could even us some sort of flag file to keep track of what files are new since your last upload, and upload them:

$flagFile = Get-Item C:\csvFiles\lastupload.flg

Get-ChildItem -path C:\csvFiles\ -Filter *.csv | ? {$_.LastWriteTime -gt $flagFile.lastWriteTime} | ForEach {Upload-File $_.FullName}

New-Item -ItemType File -Force -Path $flagFile.FullName

Good luck!

Upvotes: 2

Related Questions