Trying to create text file using user-input with Batch - batch-file

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. 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.
Also, I think there's some confusion with your variables. You capture the user's filename into textfilename, but then write to textfileWriteName. You capture the user's file contents in textfileContents, but then write textfileWrite to the file.
Finally, you specify a goto label that doesn't exist. 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

Related

Batch file put user input in a txt file in a different directory

This is my code. I want all the input it gets to go into one txt file in a certain different directory.
#echo off
echo Type in your song name and who it is by then press enter twice
:start
set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%
pause
echo %INPUT% temp.txt
goto :start
Save input into text file:
echo %input% > file.txt
or
echo %input% >> file.txt
Using > will overwrite the entire file, then add %input% to it.
Using >> will simply add %input% onto the next line of the file.
If the text file doesn't exist yet, it will automatically be created.
Move text file into desired directory:
MOVE file.txt <new-directory>
It's pretty self-explanatory. More information about the MOVE command can be found by opening the CMD, then entering MOVE /?.
Hope this helped.

Batch Remembering Configuration

I'm trying to create a batch code to remember user input. Like if you put in "echo What is your name?" then put in your name. It would then save it somewhere to remember it or recall it later on. Even if it were to create and write it to a file to remember things from.
This should do the trick:
#echo off
cls
set INPUT=
set /P INPUT=Input your name (it will be logged in names.txt): %=%
echo Your inputted name was: %INPUT%
echo %INPUT% >>names.txt
Here's a demonstration of using Windows batch commands to write user input to a file. Note the file will be written in the same directory you run the batch file from.
set /p name="What is your name: "
#echo off
echo %name% > name.txt
The /p flag turns this into a prompt, setting the value of name to the result of the user input string. #echo off prevents echoing the next line, which echoes the stored variable into a text file called name.txt. If name.txt doesn't exist, it's created; if it does exist, it's overwritten with this value.

How do I take the value of one batch file to another?

Key Generator
#ECHO OFF
COLOR A
ECHO Generating Key!
choice /d y /t 3 > nul
set /p "genkey"="%random%-%random%-%random%-%random%"
PAUSE
EXIT
Batch 2
COLOR A
#ECHO OFF
set /p base=
if %base% == %genkey% GOTO :ecs
:ecs
PAUSE
EXIT
The way I normally do this is by writing to a file and using SET to recall from the file.
For example:
BATCH FILE 1
echo off
set var1=%Random%-%Random%-%Random%
echo %var1%>temp.log
pause
exit
BATCH FILE 2
echo off
set Var1=nul
if EXIST Temp.log (set /p Var1=<Temp.log && del /Q Temp.log)
echo %Var1%
pause
exit
In this case, if you run the second batch file without running the first one, the output will be "nul". However, if you ran the first batch file before the seccond, the output of the first will be displayed.
You can change %Random%-%Random%-%Random% to whatever text or variable you want.
The program acts like the type function, however with this method it prints the contents of the file to a variable.
One last thing to note is that this method will ONLY read the first line of the file. This is useful where you are transfering numbers, then using that number in an operation. If you want to transfer the whole file, you can use a FOR state ment, but also note, the FOR statement will recall the entire into a singe line.

Batch script to Read the filenames in a directory and ask user to select one

Thanks for looking into my concern.
I have five config files in a given directory. In my batch script, I want to read those file names and prompt them to user. Once user has selected a config file, read the variables from it.
Could anyone help me with some logic here. So that, I can take it forward.
Thank you.
A batch or .cmd file like this demonstrates the menu technique (nothing fancy, the user has to enter the filename precisely). Key items:
FOR
SET /P
IF EXIST
Good luck!
#echo off
REM Show the user the list and ask them which one to use
echo.
echo Please select one of:
echo.
for %%F in ("D:\A Given Directory\*.config") do echo %%~nxF
echo.
set SEL_CFGFNM=
set /P SEL_CFGFNM=Which configuration file:
REM Make sure they answered, and that the file exists
if "%SEL_CFGFNM%" == "" goto ENDIT
if NOT EXIST "D:\A Given Directory\%SEL_CFGFNM%" goto NOCFG
REM User has selected file "D:\A Given Directory\%SEL_CFGFNM%" and it exists
REM Do whatever you want to do with that file now
REM Don't fall through the exit messages
goto ENDIT
REM Exit Messages
:NOCFG
echo.
echo ERROR: Configuration file "%SEL_CFGFNM%" is not on the list
echo.
goto ENDIT
REM Cleanup
:ENDIT
set SEL_CFGFNM=

Batch script to move to another section of the script if there is no input from user

I am trying to get this script to jump to another section of the script if there is no input from the user.
Down at the if %input%== area.
What I'm trying to do is skip to the section where the script checks for .mp4 files and moves them if they are there. Am I supposed to set a variable or loop for that section? Thanks for any replies
#echo off
echo Checking for youtube-dl updates.
pause
youtube-dl -U
rem Enter the url or urls that you want to download from
set /p input="Enter the url(s) you want to download:"
rem Uses the youtube-dl continue (-c) option to download multiple files if not in a playlist
youtube-dl -c "%input%"
rem pause
if %input%=="" GOTO:EOF
cls
echo Download complete, please wait while files are transfered to appropiate folder
pause
for %%o in (.mp4) do move "*%%o" "E:\Documents\scripts\videos\"
if not exist do echo .mp4 files are no longer in this directory
pause
How about following script? This script waits for 3 seconds while "file.mp4" doesn't exist. It keeps to wait until the file exists. About "filename", you can change for your script.
#echo off
set waittime=3
set filename=file.mp4
:loop
if not exist %filename% (
Timeout /t %waittime% /nobreak > nul
goto loop
)
echo Find %filename%
When doing string comparison in batch you have to make sure, that both parts are equal, which in your case will never happen! In most languages strings have double quotes around them. In batch they usually do not.
To solve your problem enclose %input% in double quotes as well.
Note that it can be useful to do something like "x%input%"=="x" to prevent certain characters like <>|to be at the beginning of the comparison string.
You can check this on your own with these few lines:
#echo off
set /p input="Input something or nothing here "
echo %input%
echo "%input%"
pause
If you are hitting Return without any input you will see that only the bottom one will output "" which is the string you are comparing to.

Resources