Search code examples
powershellwebclientfilenames

powershell webclient uploadfile # in filename


Hi I just tested around with powershell Webclient. Everthing seemed to work fine, but then i had to upload a file with a # in the name (test.#00). The File on the Server just strips the .#00. The file on the server is named "test" instead of "test.#00". The test.zip works without any problems.

Here is my Script used on Powershell Windows 10:

$webclient = New-Object System.Net.WebClient 
$localPath = "c:\temp\";
$ftp = "ftp://192.168.0.202/"

$webclient.Credentials = New-Object System.Net.NetworkCredential("admin","admin")  
$webclient.UploadFile($ftp + "test.zip",$localPath +  "test.zip") 
$webclient.UploadFile($ftp + "test.#00",$localPath +  "test.#00") 

Solution

  • As @kirill mentioned, you have to hex encode the file name. You can also do this programmatically using [uri]::EscapeDataString("test.#00")

    C:\> get-item .\test.#00
    
        Directory: C:\
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----        7/20/2017   7:23 AM              0 test.#00
    
    C:\> $file = get-item .\test.#00
    C:\> $encFileName = [uri]::EscapeDataString($file.Name)
    C:\> $encFileName
    test.%2300
    C:\>