简体   繁体   English

尝试使用用户输入和批处理创建文本文件

[英]Trying to create text file using user-input with Batch

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

Whenever I try to use this code, it comes up with "Echo is off" (from @echo off previously in the file) but not what the user inputted. 每当我尝试使用此代码时,它就会带出“ Echo is off”(来自文件中先前的@echo off),而不是用户输入的内容。 Then, if I run it again in the same instance, it will create the previous file but not the one I just told it to create. 然后,如果我在同一实例中再次运行它,它将创建前一个文件,而不是我刚刚告诉它创建的文件。

I've tried having > and it didn't work, >> didn't work either. 我试过>>却不起作用,>>也不起作用。

I think you have a few problems going on here. 我认为您在这里遇到了一些问题。 First, you mentioned @echo , but looking at your code, you're just using echo . 首先,您提到了@echo ,但查看您的代码,您只是在使用echo

Also, I think there's some confusion with your variables. 另外,我认为您的变量有些混乱。 You capture the user's filename into textfilename , but then write to textfileWriteName . 您将用户的文件名捕获到textfilename ,然后写入textfileWriteName You capture the user's file contents in textfileContents , but then write textfileWrite to the file. 您可以在textfileContents捕获用户的文件内容,然后将textfileWrite写入文件。

Finally, you specify a goto label that doesn't exist. 最后,您指定一个不存在的goto标签。 Maybe this is part of a larger batch file that you just partially copied? 也许这是您刚刚部分复制的较大批处理文件的一部分?

Anyhoo, I think this is along the lines of what you intended: 顺便说一句,我认为这符合您的意图:

@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

you have serious space issues in your variables " text filename and text contents" avoid spaces in your variables or if you must use more than one word in your variable use symbols to separate the strings .. the code below should work fine 您的变量“文本文件名和文本内容”中存在严重的空间问题,请避免在变量中使用空格,或者如果必须在变量中使用多个单词,请使用符号来分隔字符串..以下代码应该可以正常工作

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM