回显到.txt

时间:2018-03-06 22:26:30

标签: windows batch-file

这是我目前的.bat

set /p lastname=Please enter last name: 
set /p firstname=Please enter first name: 
set /p sex=Please enter sex: 
set /p age=Please enter age: 
set /p weight=Please enter weight (kg): 
set /p location=Please enter location: 
set /p admitdate=Please enter admit date: 
mkdir %lastname%%firstname:~0,1%
cd %lastname%%firstname:~0,1%
echo|set /p=%lastname%>lastname.txt
echo|set /p=%firstname%>firstname.txt
echo|set /p=%age%>age.txt
echo|set /p=%sex%>sex.txt
echo|set /p=%weight%>weight.txt
echo|set /p=%location%>location.txt
echo|set /p=%admitdate%>admitdate.txt

我正在尝试将每个变量写入自己的.txt。当我回到.txt时,我很难摆脱尾随的空格。例如,如果lastname是Smith,则lastname.txt显示为Smith_,其中_表示空格。

到目前为止我的尝试包括:

  • 删除变量和克拉之间的空格:%firstname% > firstname.txt%firstname%>firstname.txt
  • 切换echo和输出文件的顺序:>firstname.txt echo|set /p=%firstname%。这只会使每个.txtEcho is ON

这些方法都没有奏效。为了您的信息,我也试图摆脱追踪\n的{​​{1}}。到目前为止,我已经使用echo完成了这项工作。

1 个答案:

答案 0 :(得分:3)

这个结构对我有用:

@set firstname=John
@<nul >firstname.txt set /p=%firstname%