Escape characters in batch scripts

后端 未结 3 631
花落未央
花落未央 2021-01-14 11:07

I have the following script to automate uploading a file to a remote server. The problem is that the password I have been given is full of special characters which are killi

相关标签:
3条回答
  • 2021-01-14 11:37

    FTP passwords are sent in plain text so installing something like Wireshark will allow you to see what is being sent.

    I would expect you would need to prefix the special characters with a backslash.

    0 讨论(0)
  • 2021-01-14 11:43

    FYI, character prefixing is done with ^. So if you were doing an echo with > in the string but writing a file, it will fail without escaping. The syntax would be:

    echo ^<html^> >> foo.txt
    
    0 讨论(0)
  • 2021-01-14 11:44

    In your case you could simply type your file to reveal the password, just before you delete the file.

    But it's better to use the delayed expansion, because then the special characters lose their "special" behaviour, even carets and percent signs.

    @echo off
    setlocal EnableDelayedExpansion
    set /p passwd=Enter pwd
    echo user myloginname> ftpcmd.dat
    echo mypassword>> ftpcmd.dat
    echo bin>> ftpcmd.dat
    echo put !passwd! >> ftpcmd.dat
    echo quit>> ftpcmd.dat
    type ftpcmd.dat
    ftp -n -s:ftpcmd.dat myserver
    

    The second problem is the ftp command, where the characters also need escaping. Perhaps (like Vicky wrote) the backslash could work. You can place it before each single character, by using a for-loop

    0 讨论(0)
提交回复
热议问题