【问题标题】:Trying to create text file using user-input with Batch尝试使用批处理的用户输入创建文本文件
【发布时间】:2014-05-15 20:42:53
【问题描述】:
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 off),但不是用户输入的内容。然后,如果我在同一个实例中再次运行它,它将创建上一个文件,但不会创建我刚刚告诉它创建的文件。

我试过 > 但没用,>> 也没用。

【问题讨论】:

    标签: batch-file command-line


    【解决方案1】:

    我认为您在这里遇到了一些问题。首先,您提到了@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
    

    【讨论】:

    • 是的,这是一个较大文件的一部分 :)
    • 不,它似乎不起作用。只是创建一个没有名称的文件,里面说“ECHO 已关闭”。编辑:奇怪的是它是独立工作的,而不是我的其余代码
    • @xDarockerx 你指的是这个解决方案吗?它适用于我的系统。
    • 就像我之前所说的,它是独立工作的,并且作为它自己的批处理文件,但是当我将它添加到我的其余代码中时,它就不起作用了。第一次它将创建一个没有标题的文件,其中只有文本“ECHO is off”,如果我只是按下空格,它会再次执行此操作,但使用我为文本文件名输入的变量和内容。
    • 这段代码是否在一个块内(在()之间?如果是,你需要“延迟扩展”(在这个网站搜索它,你会找到你需要的一切)跨度>
    【解决方案2】:

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

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2015-06-26
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多