Single line AND statement in a batch file - windows

What is a simple way to do an AND statement in a SINGLE line?
example:
if %turn2%== 1
and
if %ttt1%== 1
set ttt1=X

There is no AND, AFAIK. To make sure both conditions are met, simply place them one after the other:
REM The next line echos
if "1"=="1" if "2"=="2" echo Equal
REM This one does not
if "1"=="1" if "2"=="3" echo Equal again

Related

slice and replace string before assigns to a variable

I was looking, to replace and slice a string/text before assigns to a variable,
but I did in two, I want to know if this can be done.
Example:
SET MY_TIME=%%time:~0,8%::=_%
ECHO %MY_TIME%
#output: %12:50:47::=_%
My current result, because i didn't find the way to replace and slice in the same line..
SET GET_TIME=%time:~0,8%
SET MY_TIME=%GET_TIME::=_%
ECHO %MY_TIME%
#output: 12_42_48
cmd does not allow for multiple inline replacements, as you have already learned. The closest you will probably get is:
from cmd:
set "mytime=%time:~0,8%" & call echo %mytime::=_%
From batch-file:
set "mytime=%time:~0,8%" & call echo %%mytime::=_%%
As a side note, if you want to retain the format, you need to consider single digit hours to be amended as well:
set "mytime=%time =%
The above will replace the whitespace before a single digit hour (1am to 9am) with a zero, which retains your format hhmmss
When getting to this however, there is no longer a purpose to run it as a one liner and you might as well add it line by line:
set "mytime=%time:~0,8%"
set "mytime=%mytime: =0%"
echo %mytime::=_%

Why %% changes to single % in generated BATCH automatically?

I don't understand why while I generate new batch file from primary file one parameter (%%A) automatically changes to single (%A)
Prime batch file:
echo FOR %%A in (1k\*.txt) do ... (whatever) >> another.bat
As result I become a new another.bat with single line:
FOR %A in (1k\*.txt)
And this another.bat doesn't work. How can I solve this problem?
Thanks!
To echo a single % you need to double it to %%.
To echo two % you need to do it twice, as in %%%%.
The for command replaceable parameter needs one percent sign, but inside a batch file percent signs need to be escaped, doubling the percent sign. That is the reason for the output you see. You write two characters but they represent only one. And you get one in the output.
If you need two percents in output, you need to escape each of them, so you need to write four percent signs to get two in the output.

Echo string to .txt file with multiple lines - with Windows Batch file

I am attempting to create a Windows Batch File that creates a .txt with mulitple lines. I've tried several solutions to insert a line break in the string but no avail. There are other similar questions/answers but none of them address putting the entire string into a text file.
My batch file currently reads:
echo Here is my first line
Here is my second line > myNewTextFile.txt
pause
my goal is to have the text file read:
Here is my first line
Here is my second line
Obviously, this does not work currently, but wondering if anyone knows how to make this happen in a simple fashion?
(
echo Here is my first line
echo Here is my second line
echo Here is my third line
)>"myNewTextFile.txt"
pause
Just repeat the echo and >> for lines after the first. >> means that it should append to a file instead of creating a new file (or overwriting an existing file):
echo Here is my first line > myNewTextFile.txt
echo Here is my second line >> myNewTextFile.txt
echo Here is my third line >> myNewTextFile.txt
pause
Searching for something else, I stumbled on this meanwhile old question, and I have an additional little trick that is worth mentioning, I think.
All solutions have a problem with empty lines and when a line starts with an option for the echo command itself. Compare the output files in these examples:
call :data1 >file1.txt
call :data2 >file2.txt
exit /b
:data1
echo Next line is empty
echo
echo /? this line starts with /?
echo Last line
exit /b
:data2
echo:Next line is empty
echo:
echo:/? this line starts with /?
echo:Last line
exit /b
Now, file1.txt contains:
Next line is empty
ECHO is off.
Displays messages, or turns command-echoing on or off.
ECHO [ON | OFF]
ECHO [message]
Type ECHO without parameters to display the current echo setting.
Last line
While file2.txt contains:
Next line is empty
/? this line starts with /?
Last line
The use of echo: miraculously solves the issues with the output in file1.txt.
Besides the colon, there are other characters that you could 'paste' to echo, among them a dot, a slash, ... Try for yourself.
STEP 1: Enter Line 1 followed by the ^ character.
echo Here is my first line^
STEP 2: Hit RETURN key to get a prompt for more text
echo Here is my first line^
More?
STEP 3: Hit RETURN key once more to get a second prompt for more text
echo Here is my first line^
More?
More?
STEP 4: Continue line 2 from the second prompt
echo Here is my first line^
More?
More? Here is my second line
STEP 5: Hit the RETURN key to get 2 statements displayed on two separate lines
Results:
echo Here is my first line^
More?
More? Here is my second line
Here is my first line
Here is my second line
NOTE
However, if you wish to save this to file, you could add a final STEP.
STEP 6: with the help of the > character, you can append the filename so you save your output to file instead.
echo Here is my first line^
More?
More? Here is my second line >"myNewTextFile.txt"
Example from CMD

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.)

Can't seem to get a consistent substring in a batch file

This is a strange problem. I have a batch file where I have two arguments. I wish to check the first three characters of each. The first will substring fine, but the second will not. Here is an example:
SET FIRST_ARG=%1
SET SECOND_ARG=%2
ECHO first argument is %FIRST_ARG%
ECHO first substring is %FIRST_ARG :~1,3%
ECHO second argument is %SECOND_ARG%
ECHO second substring is %SECOND_ARG :~1,3%
The first two ECHO statements work fine and display my strings as they should. The ECHO statement "ECHO second argument is" shows the second argument as it should, but the last line that says "ECHO second substring is" returns nothing.
Have I missed something?
Thanks for any help.
Rob
Your issue is the space preceding the colons :. Using %FIRST_ARG:~1,3% and %SECOND_ARG:~1,3% should fix your issue.

Resources