Slicing Strings Using Variables In Batch - string

So I want a program like this:
Enter number:
5
Giving first 5 characters of string "apple pie"...
apple
I have tried something like this
set characters=5
set "string=apple pie"
set string=%string:~0, %characters%%
But it's not working, any idea why?

either rewrite
set string=%string:~0, %characters%%
as
call set "string=%%string:~0, %characters%%%"
or use delayed expansion
#echo off
set characters=5
set "string=apple pie"
setlocal enabledelayedexpansion
set "string=!string:~0, %characters%!"
echo %string%
see Variables in batch not behaving as expected

Related

How to get a substring with special characters in batch file?

I'm trying to remove a part of a string with special characters.
The string looks like: <abc>123</abc>
I want to extract only 123
I've tried
set substr=<abc>
%my_string:substr=%
and removing character by position
set result=%my_string:~5,-6%
but neither works. Someone can help me?
ps. I'm new in batch.
sorry for the english.
Delayed expansion and double quoting of strings are the methods needed here.
The use of the for loop is just as a way of defining all substrings to be removed in the one line.
#echo off
Setlocal enabledelayedexpansion
set "string=<abc>123</abc>"
For %%A in ("<abc>" "</abc>") do Set "string=!string:%%~A=!"
Echo(!string!
pause

Batch string variables with variables in the string name

I have a batch file where I have a variable named t%num% and num is an integer and t%num%=#.
I need to set another variable called q equal to the contents of t%num%.
I tried set q=t%num% so that q would contain a #, but it did not work.
#ECHO OFF
SETLOCAL
SET num=5
SET t%num%=36
CALL SET q=%%t%num%%%
ECHO %q% %t5%
GOTO :EOF
Uses the idea that % escapes % so that the parser substitutes-in the value of num and the result is set q=%t5%
You can (ab)use delayed environment variable expansion for this purpose.
Let's say you have an environment variable t5 defined. The following batch script will assign the content of t5 to the variable q:
setlocal EnableDelayedExpansion
echo t5: %t5%
set num=5
set q=!t%num%!
echo q: %q%
However, be aware that enabling delayed environment variable expansion will make ! a special character not only in the batch script, but also inside the content of environment variables. Try setting the following content for t5 before executing the little script above:
set t5=Hello World!
You will notice that the exclamation mark at the end of "Hello World" simply disappears.
Or try this cheeky bit:
set t5=Guess what: !num!
Setting t5 like this and then executing the script, well... i leave it to you to find out what will happen :)

Split string by special characters '*' in batch file

I need to delete the substring after *. with a batch file
Example :
The value of string is : TEST_SINISTRE*.csv
rem My code :
SET mystring="TEST_SINISTRE*.csv"
rem Do the split
SET ext_test=%mystring:*.="& rem %"
SET ext_test=%ext_test%
rem what i get
echo %ext_test% ===> "& rem csv"
rem What i want to see
===> TEST_SINISTRE
Can you help me :-)
If, and only if, the *. pattern can only occur once in the string, and the part after *. is not contained in the part before *., the following could be used:
rem this is the original string containing one `*.`:
set "STRING=TEST_SINISTRE*.csv"
rem now get everything after `*.`:
rem (if `*` is the first character in substring substitution, it means everything up to
rem and including the search string is to be replaced, by nothing here in this case)
set "SUBSTR=%STRING:**.=%"
rem get everything before `*.`, including the `*`:
setlocal EnableDelayedExpansion
set "SUBSTL=!STRING:.%SUBSTR%=!"
rem truncate the `*` from the string:
endlocal & set "SUBSTL=%SUBSTL:~,-1%"
Since the substitution syntax for variable expansion is used, this is done in a case-insensitive manner.
To make it more secure, you could append something that will most probably never occur to your original string temporarily and remove it afterwards. To accomplish this, replace the set command line between the setlocal/endlocal block with the following (using appendix ### here for instance):
set "STRING=!STRING!###"
set "SUBSTL=!STRING:.%SUBSTR%###=!"
The * character is a wildcard in batch variable substring substitution. When you do *.=something in an inline substitution, you're really saying "replace everything up to and including the dot". You should use a for /F loop so you can specify the asterisk as a delimiter.
set "str=TEST_SINISTRE*.csv"
for /f "tokens=1* delims=*" %%I in ("%str%") do set "ext_test=%%I%%J"
echo %ext_test%
I'm not sure what your ultimate goal is, but here's a hacksy possible alternative. You could actually create a file called TEST_SINISTRE.csv and then capture the filename into a variable as a wildcard match.
set "str=TEST_SINISTRE*.csv"
type NUL > TEST_SINSTRE.CSV
for %%I in (%str%) do set "ext_test=%%I"
echo %ext_test%
I'm sure that's not exactly you have in mind, but it does demonstrate that maybe you don't need to strip the asterisk if you are going to be performing filename matching.

How do I split a string in batch after the last "/"?

I am making a little program for a coding club and some friends and it is like an offline database. I need to put in a dir path (ex. C:\Users\My.Name\Desktop\txtfile.txt) and I need to know how to set these variables -
Set Input = C:\Users\My.Name\Desktop\txtfile.txt
REM (SPLIT THE LINE HERE)
REM Output -
Set Path = C:\Users\My.Name\Desktop
Set Identifier = txtfile.txt
How would I do this, I have tried seperating at every "/" but I don't know that there will always be a certain amount of slashes.
Like this:
#echo off
setlocal
Set "Input=C:\Users\My.Name\Desktop\txtfile.txt"
for /f %%a in ("%Input%") do (
echo %%~dpa
echo %%~nxa
)
For has nice builtin parameters for splitting paths. Read FOR /? at the very end for details. Don't use PATH as a variable. It's a builtin environment variable. I usually use sPath or something similar.

how to concatenate strings with the lines of a text file

I've been trying to concatenate strings with the lines of a text file, but something is wrong with my code and I belive is the agruments I am using in the the For cycle. If any one can help me I'll much appreciate it.
My code is:
#echo off
set "input=C:\Users\123\Desktop\List.txt"
for /f "usebackq tokens=*" %%F in ("%input%") do (
set "str1=C:\some directory\"
set "str2=%%~F"
set "str3=.pdf"
set "str4=%str1%%str2%%str3%"
echo.%str4%
)
and the text file is something like:
121122
122233
123344
124455
But I am only getting a wrong answer and I have to run it like 3 times to get a real result and it's wrong, the first two are blank spaces and the third one gives back the last line in the text file but repeated n times where, n is the number of lines in the text file.
Result:
C:\Users\123\Desktop>concatenate.bat
C:\Users\123\Desktop>concatenate.bat
C:\Users\123\Desktop>concatenate.bat
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\Users\123\Desktop>
So, if any one has a clue on what is wrong please let me know.
Regards
-Victor-
You'll need the Enable Delayed Expansion feature. It's required since within a FOR command block, you need to refer variables that was modified.
#echo off
setlocal enabledelayedexpansion
set "input=C:\Users\123\Desktop\List.txt"
for /f "usebackq tokens=*" %%F in ("%input%") do (
set "str1=C:\some directory\"
set "str2=%%~F"
set "str3=.pdf"
set "str4=!str1!!str2!!str3!!"
echo. !str4!
)

Resources