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.
Related
So my friend gave me a batch file he wanted me to check if everything is correct, but I've got no clue what =< does in this.
:artthou_54
mode 50,20
cls
set videopath=0
title Art thou sure?
set /p videopath=<\DataTron\Ainstro\YePathToVido.dat
if %videopath%=="" echo Video Path = %videopath%
if %videopath%=="C:\Vodie" echo It seems you have the same path as the creator
set /p videopath="Would you like to open %VideoPath%? (Y/N): "
mode 50,20
if %videopath%==Y goto videohole-b
if %videopath%==N goto videohole
if %videopath%==y goto videohole-b
if %videopath%==n goto videohole
if %videopath%==back goto videohole-c
if %videopath%==exit exit
goto Art-thou-unsure
That line sets the first line of YePathToVido.dat to the variable videopath.
Normally, set /p gets input from the user and reads in until it receives a CRLF.
However, < redirects input from the specified file and sends it to the set /p command. Since set /p reads until it receives a CRLF, this effectively tells the command to read from the start of the file until the end of the first line.
On a side note, those first two if statements are incorrect because batch compares everything on both sides of the ==, including the quotes. Those lines need to change to if "%videopath%"=="" and if "%videopath%"=="C:\Vodie", respectively.
This is a very simple batch file I did to start practicing with the command line, but for some reason it wont work properly. Here the code:
::Change names
#echo off
set /p DirLoc = "Enter file location:"
cd %DirLoc%
echo %DirLoc%
dir
set /p SetFrom = "What file type is it?"
set /P SetTo = "What file type do you want?"
echo Change from %SetFrom%
echo to %SetTo%
rename *.%SetFrom% *.%SetTo%
echo process has been completed
pause
echo on
For some reason, when I insert the folder location, which in my case is "C:\Users\Marco DS\Desktop\Test", the program will only go till "C:\Users\Marco DS\Desktop", which is no good. I have tried a few alternatives of my entries, but I never manage to get the desired directory.
Thanks for any suggestions.
Part of your problem is that, in Batch, you should not use spaces or quotation marks when setting variables, or else they will be part of the variable. Unlike many languages, Batch reads all whitespace characters as part of the code. In this case, the name of the variable is set as %DirLoc % instead of just %DirLoc%. In order to make your code work in the way you want, you need to remove all the unwanted spaces and quotation marks in your code.
For example:
set /p DirLoc = "Enter file location:" becomes set /p DirLoc=Enter file location:
To see proof of this, try writing echo %DirLoc % right after echo %DirLoc% and running the code.
I'm having trouble with a batch file. I need it to remember somthing for a long
period of time. Lets say I have a variable called %Rememberme%: I need to remember this for lets say a year for some reason. How can I make my batch file remember that variable?
Well... I could echo the variable to a file using the command
echo >>%Rememberme% C:\File.txt
Well the thing is I can't have that. I need it to be remembered some other way.
Or somehow I need to give the batch file administrator rights so that it can read or write to a file. Is there a way to do this?
You can use environment variables to do this, but be careful not to overwrite existing variables.
EX:
SETX REMEMBERME "C:\windows\system32"
And then in another file,
>echo %REMEMBERME%
>C:\windows\system32
The documentation for SETX is here: TechNet - SETX:
Important remark from the link:
Setx provides the only command-line or programmatic way to directly and permanently set system environment values. System environment variables are manually configurable through Control Panel or through a registry editor. The set command, which is internal to the command interpreter (Cmd.exe), sets user environment variables for the current console window only.
Here's an example of saving a variable in a file name. This obviously assumes the variable is limited to filename-friendly characters (like a number).
:: Reading variable
for %%f in (*.myvariable) do (
set myvariable=%%f
)
:: Remove extension
set myvariable=%myvariable:~0,-11%
:: Setting variable
del *.myvariable
echo. > "%myvariable%.myvariable"
echo %myvariable%
You can also store the variable inside the text file, but this requires opening the file in order to read the variable.
:: Reading variable
for /F "tokens=*" %%A in (myvariable.txt) do (
set "myvariable=%%A"
)
:: Setting variable
echo %myvariable%> myvariable.txt
echo %myvariable%
Your question is not clear. It have not any example nor a clear description of the request, so I can only guess...
The obvious point first: how to have a variable in a Batch file that remember its value for a long period of time? (i.e.: always) Easy: just define the variable in the Batch file:
set Rememberme=The value to remember
Of course, previous method does not allows to change the value of the variable, but you had mentioned nothing about this point in your request: "the value of the variable may be changed"...
Although SETX command should work, it requires admin rights and SETX is not designed for cases like this one. For example, what happens if the Batch file is changed or is not used anymore in a given computer? Well, the last value of the variable will remain in such computer for ever!
The second obvious solution is to write the variable into a separate file and read it from there when the Batch file start. If the separate file is a data file, the method described by Matt Johnson should work and it does not require administrator rights. You must note that the separate file may also be a Batch file, so the value of the variable may be read via a simple call statement:
rem Save the value
echo set Rememberme=%Rememberme%> setTheValue.bat
rem Read the value
call setTheValue.bat
However "the thing is you can't have that, so you need to be remembered some other way", although you don't explain the reasons for this restriction...
Another solution (that I think is what you are looking for) is to set the definition of the variable in the same Batch file:
rem Read the variable at beginning of the Batch file:
call :setTheValue
echo Value = %Rememberme%
rem The rest of the Batch file goes here
. . .
rem If the value needs to be changed:
if %changeValue% equ "yes" (
rem Do it:
echo set Rememberme=%Rememberme%>> "%~F0"
)
rem End the Batch file
goto :EOF
rem Define the subroutine that set the value:
:setTheValue
set Rememberme=Initial value of the variable
In previous code "%~F0" represent the name of the running Batch file itself. This way, each change in the variable value will append a new line at the bottom of the Batch file.
first:
echo >>%Rememberme% C:\File.txt
is wrong. It tries to write the string "C:\File.txt" into a filename referenced by the variable %rememberme%. What you want is:
echo %Rememberme%>>c:\File.txt
(or only one > to overwrite the file)
Second: if you have no permission to write to the root-directory, use another directory, where you have, for example:
echo %Rememberme%>%userprofile%\file.txt
or
echo %Rememberme%>%public%\file.txt
Note: be sure, there is no space between %Rememberme% and >; it would become part of the string, if you try to read it back.
to read it back, use:
set /p "remembered=" <%public%\file.txt
Here. This should help.
#echo off
echo What would you like to do?
echo 1. Enter your name
echo 2. Load your Name
choice /c 12
if %errorlevel% equ 1 goto newname
if %errorlevel% equ 2 goto loadname
:newname
cls
echo Input your name
set /p name=Name:
echo Your Name is saved!
::Here is the command where your name saves to a file.
(
echo %name
)>yourname.name
pause
exit /b
:loadname
cls
::Here is the command where it loads your name
(
echo %name%
)<yourname.name
echo Your Name: %name%
pause
exit /b
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
I've made a Program that Launches other programs, but here is the problem.
You need to specify the path of the file in the code which means that the end-user needs to get into the code to specify the file which is not really the ideal situation.
I have solution in mind,when you launch the program a dialog box comes up and asks you to give it the file path so it can run the specified program. How would I go about doing something like this?
You can also do the following
#echo off
set foo=%1
echo %foo%
%1 refers to the 1st parameter that you have passed it to the program. This first parameter will then be set to the variable %foo%. Here is the example:
C:\>test.bat "C:\passwd"
C:\passwd
Hope this helps :)
Update
You can make your program to execute another program by doing the following:
#echo off
REM `%~f1` will helps to expand `%1` to a fully qualitified path name
set "executable=%~f1"
REM checks if the first parameter exist. If it did not exist, a usage text will be displayed and the program will exit
if "%executable%"=="" (
echo Usage: %0 path\to\executable
goto :EOF
) else (
call :program
goto :EOF
)
:program
echo %executable% is starting...
start "" "%executable%"
goto :EOF
You can reads user input. e.g. edit the following snippet according to you need
ECHO User will have to enter the input file path.
set /p variable=Enter input files path please:
The user can type as many letters as they want, and it will go into the delBuild variable.