Echo two set /p - windows

I made a bat that renames folders
and files inside them
it asks for the old name
and then new name
like
SET /p originalcodename="Please enter the ORIGINAL codename: "
SET /p newcodename="Please enter the NEW codename: "
and I made anohter bat for bulk processes
and its like this
echo example_oldfoldername| renamecodename.bat (
echo example_newfoldername| renamecodename.bat|rem
)
and it gave bunch of errors
I wonder if there is a way to make it echo two inputs
it's a bit complicated but

You need to provide both inputs to the same instance of your batch file:
(echo example_oldfoldername&echo example_newfoldername)| renamecodename.bat
or to keep it readable in a batch file:
(
echo example_oldfoldername
echo example_newfoldername
) | renamecodename.bat
Pay attention to any stray spaces, they are invisible but will get part of the variables, which may lead to unexpected behaviour.

Related

How to nslookup and write to txt file using a batch script.

Im in the middle of a small programming assignment right now, and am stumped. What needs to be done is->
Create a batch file to perform the following steps ( written the best I can in pseudocode )
IF USERDOMAIN==COMPUTERNAME
THEN
CreateFile cake.txt
cake.txt = %USERNAME% + " " + %COMPUTERNAME% + " " + nslookup(www.disney.com)
How far I've gotten on the other hand is so=
IF USERDOMAIN == COMPUTERNAME GOTO Text
:Text
echo.>"C:\cake.txt"
rem Saved in C:\cake.txt
echo USERNAME >> cake.txt
echo COMPUTERNAME >> cake.txt
I only know C#, and using stackoverflow I have found this similar question:
Batch script for loop & nslookup
But, the syntax in that question is just of nslookup in general, I dont understand the windows batch syntax at all, I got this far through a couple hours of searching.
It should be:
#echo off
rem case insensitive string comparison
if /i %USERDOMAIN% == %COMPUTERNAME% (
rem clear file
break>"C:\cake.txt"
rem output all commands in following block to file
(
echo %USERNAME%
echo %COMPUTERNAME%
nslookup www.disney.com
) >> "C:\cake.txt"
)
if you want to filter the output of nslookup you have to do it differently but as you don't tell about how the output should look like this mabe should be enough.
I dont understand the windows batch syntax at all
If you would be more specific about your problems it would be possible to explain you the things you didn't get.
A good reference about batch scripting is the site SS64. Every command is very well explained there and completed by examples.
Why did you use #echo off? in the start, and use echo later on? Isn't it just a way to output status info of how the code is progressing? The purpose of the /i is also unknown to me, and finally, why did you break before handling the file? I read that a single > is for removing and clearing text, so why would that need a break of some sort?
1.
#echo off and echo:
The #echo off command is used so that the commands itself are not displayed during the batch processing, consider the following example:
#echo off
echo Hello world with echo off!
#echo on
echo Hello world with echo on!
In your command prompt it would print something like:
Hello world with echo off!
C:\Users\...\Desktop>echo Hello world with echo on!
Hello world with echo on!
The echo command is just used to output something to the console.
2.
if /i:
The /i will compare the strings case insensitive so that "ABC" == "abc" or "abC" == "AbC" will be true.
3.
break and > or >>:
To clear a file you used echo.>"C:\cake.txt". This is not completely wrong but that will save a new line into the file and not clear it as echo. means new line. The break command will generate no output (see here on SO), if that will be redirected to a file with > the file will be completely empty.
You differ between > and >> when talking about redirection. A single > means overwriting the existing file if it already exists and a double >> means append to file or create a new file if it doesn't exists.

How to output multiple user input values into a single variable in BATCH using Windows 7?

SOLVED!
Update: It figures moments after posting for help which is something I never do I'd figure it out...I tend to over think things, and that was the case here, it was just so simple! >.<
Solution:
(This worked under Windows 7 Ultimate 64 Bit)
Set var=
Set var=SomeText %var1% %var2% %var3%
Echo %var% > output.txt
See an explanation in my answer below.
I've been searching and trying several posts here similar to my question for hours with no success. I'm not new to Programming in BATCH but I have memory problems and thus can't always remember things. It also doesn't help that I program in other languages on other platforms which usually means I'm trying to use *nix shell commands in my Windows Batch scripts >.<
I've gotten quite close with some examples but nothing that works as needed.
Ideally, I'd like this work to work on Windows 7, 8, 8.1, Vista and 10 as that is the intended target.
This is what I need to accomplish:
The user will answer a series of questions, each question is stored into a .txt file (or variable if you prefer. I just used text files because of a past project where I ran into issues with variables that couldn't be solved and text files worked). The lines in each text file will need to be output into a single text file, on a single line which will then be read back in as a variable and run. Again, you could just use and combine the variables in your example if that's easier for you or both of us ;P
This is a snippet example of how I was doing it
SET file1=
SET /P file1=file1:%=%
ECHO %file1% > file1.txt
Then
copy /b file1.txt + file2.txt + file3.txt + file4.txt output.txt
Here is how I'd like the result to look
toolkit /S "C:\ToolKit Bravo\Data\etc" "D:\ToolKit Bravo\Data\Ops"
The "" quotation marks are necessary. The output MUST be EXACTLY as shown above for the example I've given. The "/S" & paths are variable NOT fixed!
Here is the best I've been able to come up with using variables..
"toolkit /S "C:\ToolKit Bravo\Data\etc" "D:\ToolKit Bravo\Data\Ops""
Update 2 - An explanation as requested:
The paths in the above example directly above this are not fixed! This was an Example Only. "toolkit" is fixed, this doesn't change. "/S" is an option selected by the user to pass on to the "toolkit". Both the source and destination paths are again input by the user in "quotation" marks. They're not fixed paths.
As you can see the result is surrounded by quotations which is NOT acceptable. And Please remember, I NEED the quotations around the paths in the end result, so removing them all is NOT an option!
Any help is greatly appreciated! Thank you for your time.
Just take all of the characters between the quotes.
SET X="toolkit /S "C:\ToolKit Bravo\Data\etc" "D:\ToolKit Bravo\Data\Ops""
ECHO %X%
SET Y=%x:~1,-1%
ECHO %Y%
Solution:
This solved my problem under Windows 7 Ultimate 64 Bit
Set var=
Set var=SomeText %var1% %var2% %var3%
Echo %var% > textfile.txt
Using the SET command I first made sure the variable or var for short was empty. Using this command:
Set var=
I then proceeded to create my variable using all of the other variables I had created and wanted to combine using this line of code:
Set var=SomeText %var1% %var2% %var3%
Note that I have preceded the variables with "SomeText". This is where I'll place the name of the .exe I'm passing the arguments to, but it can be anything you want included. I also need spaces between each variable so I've left spaces between them in the example code. If you don't want the spaces simply remove them, and you'll have 1234, instead of 1 2 3 4.
Finally I send the combined variable out to a .txt file.
Echo %var% > textfile.txt
However, you could also simply call the new variable now like this:
%var%

") was unexpected at this time", Create list of files in a variable - Batch

So I am trying to create a batch file, that gets all the files in deletesrc and find's if it's name matches any folder in deletedest. To accomplish this, I have tried to generate a list of file names in deletesrc which is stored in deletefiles (in the format "first" "second" "third file"). Then I intend to loop through each folder in deletedest and check for it's existence in deletesrc (using this). So far I have managed:
setlocal enabledelayedexpansion
set "deletesrc=F:\Delete Source"
set "deletedest=C:\Users\Spaced Name\Delete\Dest"
set "deletefiles=" ::I think this is useless, but I would prefer it to be here
for /R "%deletesrc%\" %%i in (*) do (
set "deletefiles=!deletefiles!^"%%i^" "
::random comment that is source of error
)
set deletefiles=!deletefiles!:rTrim
echo !deletefiles!
The main problem is, I keep getting ) was unexpected at this time.
Placing an echo test as the first thing within the loop, does not change the output, implying the loop is not run and the error encountered first. Placing an echo just before the loop, does produce output and the error immediately after it.
If there are any syntax standards or things I should/should not be doing that do not answer the question, I would like to know.
Your problem is you're using :: as a comment inside your FOR loop. You can't do this because :: is actually the label designator and it is breaking the FOR loop.
Change this line:
::random comment that is source of error
To this:
REM random comment that is source of error
UPDATE
As foxidrive stated in the comments, you can use :: as a comment inside a FOR as long as it is not the last line before the closing ).
Try this:
set deletefiles=!deletefiles! "%%i"

Word Sorting in Batch

Right let me rewrite this try to make it more clear.
Picture added to make this even clearer:
I have two files
File 1, contains words.
file 2, contains commands.
I need to put words from FILE 1
into FILE 2
I cannot copy-paste them one by one, because there is a LOT of words in FILE 1
File 1 is listed in alphabetical order (by first letter)
File 2 the command does not change
The issue is getting words from file 1 into file 2
but they have to be moved into quotes " " in file 2
so a script that could for example..
Take apple from file 1 and move it between quotes admin.executemotecommand "apple"inside file 2 as it goes down the list keeping the words in order as they move them across.
This could perhaps be done the same way around in which, the script writes the command in front of the words in file 1 as it goes down file 1's list
Is this even possible? I've never seen this done anywhere else and completely clueless if batch is even the right language for it.
The question is a little confusing, but based on your responses in the comments my understanding is that you don't necessarily need the script to edit a preexisting file 2, because you're repeating the same command(s) for each word, so the script can just create a new file based on the words in file 1.
You can do it at the prompt like this:
FOR /F %a IN (words.txt) DO ECHO admin.executeremotecommand "%a" >> commands.txt
The original version of the question indicated that you want more than one command for each word. I take it you changed that in order to simplify that question, and figured you'd just run the script once for each command? However, it's quite simple to have it produce more than one command for each word:
FOR /F %a IN (words.txt) DO (ECHO first.command "%a" & ECHO second.command "%a") >> commands.txt
In a batch file, you'd do it this way:
#ECHO OFF
FOR /F %%a IN (words.txt) DO (
ECHO first.command "%%a"
ECHO second.command "%%a"
) >> commands.txt
BTW, in the code in some of your comments, you surrounded the variable with %'s (%A%). That's incorrect; it would evaluate to the value of %A followed by a literal %. Surrounding with %'s is used only for environment variables. (Note that the %'s around environment variables do not get doubled in a batch file. For example, to get the current date, use ECHO %date% both at the prompt and in a batch file.)

CMD appears not to recognize variables

CMD does not appear to be recognizing any variable I store using SET. If I run this batch file:
#ECHO off
SET /P name = What is your name?
ECHO %name%
PAUSE
ECHO on
I get the following output:
What is your name? steven
ECHO is off.
Press any key to continue . . .
When I run line 2 and then line 3 from the command prompt, it just prints:
%name%
Do I have something configured incorrectly? Am I correct in thinking that line 2 should create a session variable that should be recognized in line 3?
I searched, but I could only find answers related to variable expansion within IF blocks. This is happening to me outside any IF/FOR/etc blocks.
This is Windows 7, by the way. I'm not sure how much cmd changes from one version of Windows to another.
There must not be any spaces around the equal sign in the set instruction. Change this
SET /P name = What is your name?
into this
SET /P name=What is your name?
and your problem will disappear.
since I can not add any comments yet I have to post a new answer. The information:
"There must not be any spaces around the equal sign in the set instruction."
is only partly correct. To be exact it should say: the equal sign must follow directly after the variable name (in sricks3's case without a space). Whatever comes after the equal sign will be used as input-prompt for the variable including any spaces, so the following code will work as well:
SET /P name= What is your name?

Resources