Escape characters in batch scripts

后端 未结 3 669
花落未央
花落未央 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: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

提交回复
热议问题