Hi guys im trying to add multiple variables to one.
ET CA1="C:/Users/"
SET CA2=Bla
SET CA3="/New Folder/"
SET CA=%CA1%%CA2:~%%CA3%"
echo %CA%
This is the Output
"C:/Users/"Bla"/New Folder/"
When i do this i always get double quotes " in the middle no matter what i do.
I have tried to use :~1,-1%" to remove the last char but the output is just "l" in the middle.
The "end product" i want to archive is to ask the username by prompt and use the string for something else. This was the best ( and surely not best) i could think of. to add 3 different Variable to get the right path.
Is there maybe another way to just have something like this:
set /p Username="Insert Username" -> pete for example
set CA="C:/Users/%CA%/New Folder/"
echo %CA%
Output:
"C:/Users/pete/New Folder/"
As per your first example try this:
#echo off
SET "CA1=C:/Users/"
SET "CA2=Bla"
SET "CA3=/New Folder/"
SET "CA=%CA1%%CA2:~%%CA3%"
echo "%CA%"
pause
which simply echo's:
"C:/Users/Bla/New Folder/"
Note that you move the quotes to before the variable name, and not after the =
On the second part, where you require user input. there are 2 methods:
User input method
set /p "myuser=Insert Username: "
set "CA=C:/Users/%myuser%/New Folder/"
echo "%CA%"
pause
or Get the name from environment.
#echo off
set "CA=C:/Users/%username%/New Folder/"
echo "%CA%"
pause
Note, the second method gets the %username% variable of the user currently logged in from the environment and will set it automatically.
Finally, some hints. always use help from cmd.exe to find relevant commands
Use each command with the /?switch to get more information on it's abilities. i.e set /?
Also, NEVER modify existing environment variables. For instance in your example set /p Username="Insert Username.. %username% is already an existing environment variable, rather create something unique likemysername`
To test this, simple do from cmd.exe echo %username% and to understand where it got it from, simply run set to display Environment variables.
Related
I'm required to write a program that displays the computer name, username, and path to user files. All of these should be taken from local system variables. It's been hinted that I'm supposed to use the set command, however, I'm not sure how to use the set command in this situation... I had assumed that I could just use echo %COMPUTERNAME% etc. How can I implement the set command?
I'm guessing that the hint about the Set command was so that you could use the output of it to help you identify the local system variables to use in your batch file, not that you need to use Set to output them.
When you enter Set at the Command prompt you'll get output showing you each of the defined system variables.
Additionally, if you enter Set ComputerName at the prompt, you should get output showing you all variables which begin with the string ComputerName.
So based on the output of the Set command you could Echo, your three variables from a batch file like this:
#Echo Off
Echo %ComputerName%
Echo %UserName%
Echo %UserProfile%
You could also include the variables with their values:
#Echo Off
Echo %%ComputerName%%=%ComputerName%
Echo %%UserName%%=%UserName%
Echo %%UserProfile%%=%UserProfile%
You could also consider running a simple For loop in your batch file to show the same content using the Set command directly:
#Echo Off
For %%A In (ComputerName,UserName,UserProfile) Do Set %%A
Pause
Or you could return just their values using Set and Echo from nested For loops:
#Echo Off
For %%A In (ComputerName,UserName,UserProfile) Do (
For /F "Tokens=1* Delims==" %%B In ('Set %%A') Do Echo %%C)
Pause
to take the question very literal ("display the computer name ... using set command"):
set computername
set username
set userprofile
output like:
COMPUTERNAME=Elon-PC
USERNAME=Muscrat
USERPROFILE=C:\Users\Muskrat
(Compo already has this method in his answer, but I guess using the for command is over the boundaries of the current state of your course)
(Note: for practical use, in most cases Compo's answer (using variables) is better, because in practice, you will probably do something with the values, not "just" show them, but this literally answers your question)
I would like to find a way to use batch to change the keys a user types into something else. I'm not sure if there is a way to do this in batch, so if there is a better way to do this please let me know. Basically, I would like to change what is typed into something else, meaning when the program is run, (for example) any time the user types the letter "a" it could replace it with the letter "b". I'm not sure if I can do this, and to clarify I do not want the answer given to me, just some guidance on how to do it. Thanks
Perhaps you mean something like that in batch : - Variable Edit/Replace
#echo off
Title Variable Edit/Replace
:Main
cls
echo Type a word ...
set /p "Input="
set "StrToFind=a"
set "NewStr=b"
setlocal enabledelayedexpansion
set AfterReplaceInput=!Input:%StrToFind%=%NewStr%!
echo Before replace : !Input!
echo After replace : !AfterReplaceInput!
echo(
echo Hit any key to try again with another word...
pause>nul
Goto Main
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
I'm trying to make a code where I can go to websites without going to my browser I have atop search function which I will change manually I'm fairly new to coding so any advice is helpful here is the code.
#echo off
echo Top searches
echo 1. Faceit
set /p name =
if %name% EQU "1" goto F
if %name% NEQ "1" then goto custom
:F
start "" https://www.faceit.com/en
:custom
echo What website would you like to go to?
set /p x =
start "" https://www.%x%
There's quite a bit going on in your code that is keeping it from working in any meaningful sort of way. Just at an initial glance, I see ten separate things that are either completely wrong or simply violate what could be considered "good programming practices" in batch.
1/2. Whitespace in variable names is significant
For some reason, Microsoft decided to allow whitespace in variable names, so %this is a valid variable name%. Seriously. As a result of this, both of your set /p statements are creating variables that you never use.
Instead of set /p name = and set /p x =, use set /p name= and set /p x=
3/4. Put quotes around set statements
This one is just good programming practice and is arguably not "wrong," but it's a good habit to form early.
Use quotes to avoid the user entering things like & or > and having those break the flow of the script. You can put the quotes to the around the prompt (like set /p variable="Enter text: "), but if you do that with a regular set statement, the quotes will become part of the value. To avoid this, put the first quote to the left of the variable name, like this: set /p "variable=Enter text: "
This also prevents any hidden spaces from getting tacked on at the end of the value by accident.
5. Then is not a keyword in batch
The then in your second if statement if going to give you a syntax error because it's not a valid keyword in batch. Just get rid of it.
if "%name%" NEQ "1" goto custom
6/7. Quotes in comparisons are significant
When you put quotes around one side of a comparison, you need to put quotes on the other side as well. This has the added effect of keeping characters like & and > from breaking the flow of the script.
if "%name%" EQU "1" goto F
if "%name%" NEQ "1" goto custom
8. A missing goto (or exit) will cause :custom to run immediately after :F
Batch scripts run from top to bottom unless acted upon by a goto, call, if, or some other flow control command. In this case, after start "" https://www.faceit.com/en is called, the very next non-whitespace line is :custom.
To avoid :custom from running, kill the script after the first start with exit /b or goto :eof - both of these will stop the script but keep the command prompt open if you ran the script from the command line instead of double-clicking it. Note that if you use goto :eof, you do not need to make a :eof label, since it's built into the command prompt.
9/10. Put colons in front of labels in goto commands
Again, not necessary, just good programming practice. You have to include the colons when you use call to run subroutines anyway, so you might as well be consistent everywhere.
Other notes
When the script is first run, all you see is
Top searches
1. Faceit
and that's it. Nothing to tell the user what to do or to indicate that they can enter another site by typing something other than 1. Unless you plan on being the only person to use the script, I'd recommend putting something somewhat more descriptive in that section.
If you're going to automatically tack on https://www. to the start of a custom URL, put that on the screen so that the user doesn't accidentally end up going to https://www.https://www.google.com or something.
You may want to look into the choice command for future versions of the script to replace the initial set /p command, depending on how many options you want to give the user.
Putting comments in your code wouldn't hurt.
Ultimately, it will look something like this
#echo off
echo Top searches
echo 1. Faceit
echo Enter anything else to go to a different site
set /p "name=Your selection: "
if "%name%" EQU "1" goto :F
if "%name%" NEQ "1" goto :custom
:F
start "" https://www.faceit.com/en
exit /b
:custom
echo What website would you like to go to?
set /p "x=https://www."
start "" https://www.%x%
I am kind of new at this but what I am trying to do is when you run the batch file it will ask you what user account. Than when you enter the user account it will bring you into that account location. I feel like I am close but not to sure what I am missing.
#ECHO OFF
Title Enter Users Profile
color 4f
cd C:\Users\ dir
Set /p %User%= Account:
%User%== cd C:\Users\%user%\
I'm not sure what the hack you want to achieve with this but here is the code:
#ECHO OFF
:enterUser
SET /p usr=enter user account:
IF EXIST C:\Users\%usr% (
CD C:\Users\%usr%
) ELSE (
ECHO User not found!
ECHO Try again!
GOTO enterUser
)
PAUSE
Set /p %User%= Account:
This is incorrect syntax, because %User is expanded, but has no value, so you end up trying to prompt;
Set /p = Account:
Which won't set anything.
Just use;
set /p User= Account:
Also the last line in your script is
%User%== cd C:\Users\%user%\
Which makes me think you forgot to put if and the corresponding commands after it.
It's also good practice to put quotes around your variables, in case a few odd symbols or, more often spaces mess things up, which can easily happen in most commands that use user/file/folder names.
And as I always mention, if you plan to have others using this, check if they just press enter after the set /p with
if not defined User (
echo Error: Empty Input
pause
goto :top
)
MichealS's code should be sufficient, I just wanted to explain a few odditied of your code.
I am trying to call a function present in saur.exe using bat.
It looks something like this:
saur.exe readName
When i execute it, it returns a string "Saurabh".
Now that I want to store "saurabh" in a variable called name.
So I am doing :
set name = saur.exe readName
echo .%name%
In this case, it doesnot execute.
It gives blank in front of echo command.
For some strange reason, doing what you want requires some awkward workarounds. The long way would be to store the output of the command in a file, then read the file into the variable, and finally delete the file. The shorter (and barely readable) way is:
for /f "delims=" %%a in ('saur.exe readName') do set name=%%a
echo %name%
:-(
Some SET command documentation will show that you can only assign strings to environment variables (http://www.computerhope.com/sethlp.htm).
In your example above, you have actually set an environment variable called:
"name "
Yet you are echoing the variable:
"name"
The best solution I can find is to do something similar to
saur.exe readName>tempFile
SET /p variableName=<tempFile
ECHO %variableName%
Hopefully this helps :)