0

i want upload my files to FTP server using with Windows conmand prompt. It working good if the filename doesn't contain UTF-8 Characters. But If it contains UTF-8 Characters not uploading to FTP service. My test.bat :

@echo off

chcp 65001
echo >> %TMP%\ab.dat
echo user b12_17312633> %TMP%\ab.dat
echo 123456ab>> %TMP%\ab.dat
echo bin>> %TMP%\ab.dat
echo cd htdocs>> %TMP%\ab.dat

for /R %1 %%f in (*.txt) do (
    echo put "%%f">> %TMP%\ab.dat
    )

echo bye>> %TMP%\ab.dat
echo quit>> %TMP%\ab.dat
ftp -n -s:%TMP%\ab.dat ftp.byethost12.com
pause

Output:

220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 456 of 1900 allowed.
220-Local time is now 20:57. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 60 seconds of inactivity.
ftp> user b12_17312633
331 User b12_17312633 OK. Password required

230 OK. Current restricted directory is /
ftp> bin
200 TYPE is now 8-bit binary
ftp> cd htdocs
250 OK. Current directory is /htdocs
ftp> put "C:\Users\user\Desktop\FTP\çtü.txt"
C:\Users\user\Desktop\FTP\çtü.txt: File not foundftp> bye
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.

I tried it on "çtü.txt" for test. Note: I worked this on Windows Command Prompt.

Raymond Chen
  • 42,606
  • 11
  • 86
  • 125

1 Answers1

0
  • -s:<FileName> Specifies a text file that contains ftp commands. These commands run automatically after ftp starts. This parameter allows no spaces. Use this parameter instead of redirection (<).
  • Note. In Windows 8 and Windows Server 2012 or later operating systems, the text file must be written in UTF-8.

FTP documentation conceals that this text file must be written in UTF-8 with BOM (byte order mark) although according to the Unicode standard, the BOM for UTF-8 files is not recommended.

I'd use next improvement of your test.bat script:

@echo off
chcp 65001
powershell -command [System.IO.File]::WriteAllBytes("'%TMP%\ab.dat'", (0xEF,0xBB,0xBF))
echo user b12_17312633>> %TMP%\ab.dat
rem … the script continues here

where powershell call creates the ab.dat file with UTF-8 BOM, i.e. with initial / leading byte sequence 0xEF,0xBB,0xBF.

Community
  • 1
  • 1
JosefZ
  • 22,747
  • 4
  • 38
  • 62
  • 1
    The problem doesn't at file content. It is at file name. Also my file already using UTF-8 without BOM. – Recep Selim Ağırman Feb 16 '16 at 14:57
  • @RecepSelimAğırman did you try my solution? [Take a look](ftp://ftp.byethost12.com/htdocs/), you should see a file `tçtü.txt` uploaded with your script improved as I have suggested… – JosefZ Feb 16 '16 at 16:50
  • Yes, i tried now it. Yo're right. I saw tçtü.txt. Well, now how do I get the results I want? I mean, it doesn't work on my computer. – Recep Selim Ağırman Feb 16 '16 at 16:56
  • Double check the `utf8bom.txt` file and check `%TMP%\ab.dat` using a hexadecimal viewer/editor whether it contains UTF-8 BOM. Note `>>` redirector in `echo user b12_17312633>> %TMP%\ab.dat`. Or try command line `chcp 437` and then `type %TMP%\ab.dat` it should return `∩╗┐user b12_17312633` at the first line. – JosefZ Feb 16 '16 at 17:05
  • It shows: `C:\Users\user>chcp 437 Etkin kod sayfasi: 437 C:\Users\user>type %TMP%\ab.dat user b12_17312633 123456ab binary cd htdocs put "C:\Users\user\Desktop\Recep Selim\FTP\testttt.txt" put "C:\Users\user\Desktop\Recep Selim\FTP\utf8.txt" put "C:\Users\user\Desktop\Recep Selim\FTP\├ºt├╝.txt" bye quit C:\Users\user>` – Recep Selim Ağırman Feb 16 '16 at 17:18
  • I guess so, problem at this `C:\Users\user\Desktop\Recep Selim\FTP\├ºt├╝.txt` – Recep Selim Ağırman Feb 16 '16 at 17:25
  • Also my .txt files at main directory. So it's at program directory. Not `%TMP%` – Recep Selim Ağırman Feb 16 '16 at 17:35
  • Again: the FTP script file, i.e. `%TMP%\ab.dat` need to contain UTF-8 byte order mark, i.e. must start with three bytes 0xEF,0xBB,0xBF. Those bytes _appear_ as `∩╗┐` when you `type` such file under `chcp 437`. – JosefZ Feb 16 '16 at 17:36
  • Result is same. It doesn't appear `∩╗┐`. – Recep Selim Ağırman Feb 16 '16 at 17:43
  • Please do not show what does not appear. Just **force** the BOM as suggested. Double check (see and follow my comment 45 min. ago above)… – JosefZ Feb 16 '16 at 17:48
  • Okey, i forced it to UTF-8 - BOM (I used Notepad++). CMD appears `∩╗┐` for now. But Script doesn't still upload `çtü.txt`. – Recep Selim Ağırman Feb 16 '16 at 17:56
  • Please [edit your question](http://stackoverflow.com/posts/35415997/edit) and add the newest version of your _batch_ script `test.bat`. Also now under `chcp 65001` and with proper BOM in FTP script `type %TMP%\ab.dat` should show `put "C:\Users\user\Desktop\Recep Selim\FTP\çtü.txt"` just before `bye` instead of mojibake `├ºt├╝.txt` – JosefZ Feb 16 '16 at 18:29