尝试使用Batch的用户输入创建文本文件

时间:2014-05-15 20:42:54

标签: batch-file command-line

echo.
set /p textfileName=What would you like the file to be named? 
echo.
echo On the line below, write what text you would like the file to contain:
echo.
set /p textfileContents= 
echo %textfileWrite% >> %textfileWriteName%.txt
echo.
echo Your file has been created and is in the same directory as this batch file.
echo It is named %textfilename%.txt
echo.
goto inputCommand

每当我尝试使用此代码时,它会出现“Echo is off”(来自之前在文件中的@echo)但不是用户输入的内容。然后,如果我在同一个实例中再次运行它,它将创建前一个文件,但不会创建我刚刚告诉它创建的文件。

我尝试过>并且它不起作用,>>也没用。

2 个答案:

答案 0 :(得分:0)

我认为你在这里遇到了一些问题。首先,您提到@echo,但查看您的代码,您只是使用echo

另外,我认为你的变量存在一些混淆。您将用户的文件名捕获到textfilename,然后写入textfileWriteName。您在textfileContents中捕获用户的文件内容,但随后将textfileWrite写入该文件。

最后,您指定一个不存在的goto标签。也许这是您刚刚部分复制的较大批处理文件的一部分?

Anyhoo,我认为这与你想要的一致:

@echo off
set /p textfileName=What would you like the file to be named? 
@echo On the line below, write what text you would like the file to contain:
set /p textfileContents= 
@echo %textfileContents% > %textfileName%.txt
@echo Your file has been created and is in the same directory as this batch file.
@echo It is named %textfilename%.txt

答案 1 :(得分:0)

你的变量中存在严重的空间问题"文本文件名和文本内容" 避免变量中的空格,或者如果必须在变量中使用多个单词,请使用符号来分隔字符串..下面的代码应该可以正常工作

@echo off
: main_menu
echo.
echo.
set /p notf=name of text file :
set / p cof=contents of file :
echo %cof%>>"%notf%.txt"
echo %notf% text file was  successfuly created...
echo.
echo.
echo press any key to return to main menu
pause>null
cls
goto main_menu