在将ECHO输出重定向到文件时,如何避免在文本文件中写入尾随空格? [英] How to avoid writing trailing spaces into text file on redirecting ECHO outputs to a file?

查看:403
本文介绍了在将ECHO输出重定向到文件时,如何避免在文本文件中写入尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

@echo off
color 0a
title FTP
CLS
echo username > FTP.txt
echo password >> FTP.txt
ftp -s:FTP.txt ftp.website.com

但是我出现问题,因为在文本文件的用户名密码上附加了空格,所以在执行身份验证时显示错误。换句话说, FTP.txt 的第一行是 用户名,而不是 用户名。另外第二行是 密码 c,而不是 密码

But I have the problem that it shows an error in authentication on execution because a space is appended on username and password in text file. It other words first line of FTP.txt is "username " and not "username". Also second line is "password " and not "password".

如何删除每行末尾的空格或避免将其写入文件中?

How to delete those spaces at end of each line or avoid writing them into the file?

推荐答案

如Stephan所述上方,用户名密码之后的空格被视为文字空间,而不是令牌分隔符。删除> 重定向器之前的空格。

As Stephan comments above, the space after username and password is being treated as a literal space, not a token separator. Remove the spaces before the > redirectors.

echo username>FTP.txt
echo password>>FTP.txt

如果您觉得很尴尬,也可以将重定向放在行的开头并达到相同的效果。

If that seems awkward to you, you can also put the redirection at the beginning of the line and achieve the same effect.

>FTP.txt echo username
>>FTP.txt echo password

区别纯粹是装饰性的。或者,您只能打开一次FTP.txt进行写入,但是在关闭之前使用多个语句进行写入。

The difference is purely cosmetic. Or you can open FTP.txt only once for writing, but use multiple statements to write before closing. This is actually a bit more efficient.

>FTP.txt (
    echo username
    echo password
)

(
    echo username
    echo password
) >FTP.txt

这篇关于在将ECHO输出重定向到文件时,如何避免在文本文件中写入尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆