Extract numeric part from a string using batch scripting - windows

I have a file name abcd_1234.java. How can i able to extract only the number and store into a variable using batch scripting. The output should come as 1234.
There is no fixed length of the string, if the string is testfile_345.java the output should be 345.
In Unix i find it easy, but in batch scritping i am not getting the proper commands.
Please help.
Thanks.

To get the Last underscore delimited section of a file name,
use string substitution to replace the underscores with a space and iterate with a simple for.
#Echo off & setlocal EnableDelayedExpansion
for %%A in (*_*.java) do (
set "fname=%%~nA"
set "fname=!Fname:_= !"
for %%B in (!fname!) Do set Number=%%B
Echo number from %%A is !Number!
)
Sample output:
number from abc__def_123.java is 123
number from abc_123.java is 123

Related

How to split up comma separated strings from a .txt file into separate files each containing a data column?

I am beginner in scripting language and wanted to write a batch script by which I can get separate new .txt files each with a data column by reading a .txt file which contains comma separated strings as shown below:
James,10098,Jan
Arthur,19086,Feb
Katerina,10000,Mar
After getting the output, I want to have an array for the strings so that I can use them in another batch script.
Below script is what I have tried, but which is not worked the way I want it.
#echo on
setlocal ENABLEDELAYEDEXPANSION
set outputPath="C:\Users\Ben Martin\Test Scripts\"
set index=0
for /f "delims=," %%x in (Demo_MData.txt) do (
set A[!index!] = %%x
set /a index +=1
)
cd %outputPath%
Set A > %~n0_Output.txt
The current output in a single new separated file is:
A[0]=James
A[1]=Arthur
A[2]=Katerina
The expected output should be in three new separated files.
Example1.txt
James
Arthur
Katerina
Example2.txt
10098
19086
10000
Example3.txt
Jan
Feb
Mar
for /f "tokens=1,2,3delims=," %%g in (Demo_MData.txt) do (
set "G[!index!]=%%g"
set "H[!index!]=%%h"
set "I[!index!]=%%i"
set /a index +=1
)
then
set G[>gfile.txt
.
I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables)
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs
(See `for/f` from the prompt for documentation)
Batch is sensitive to spaces in an ordinary string SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN". Remove Space from both sides of the =.
Using set G[ to output t a file causes all variables starting G[ to be listed.

Using Variable as Substring Parameter in Batch

I'm trying to extract the string before the last slash that I retrieve from my function; :FIND_SLASH. But when I echoed the result, it is displaying the variable name ndxOfSlash, instead of the extracted string.
The list.txt contains the sample input below.
Sample Input:
AAA/BBBB/CCC/test.txt
Expected output:
AAA/BBBB/CCC/
Actual output:
ndxOfSlash
Code Snippet
#echo off
setlocal EnableDelayedExpansion
FOR /F "tokens=*" %%x in (list.txt) DO (
set tempname=%%x
call :FIND_SLASH indexSlash
set ndxOfSlash=!indexSlash!
call set fileDir=!!tempname:~0,!ndxOfSlash!!!
echo !fileDir!
)
I also tried replacing the variable parameter; ndxOfSlash, with an actual number, and it was able to display the expected output.
I referred to this site when doing the substring.
call set fileDir=%%tempname:~0,!ndxOfSlash!%%
should fix the problem.

CMD Batch - Search for last occurence of character while looping through file

I have a .txt file which I loop through every line and spool to another file. Ok no problem so far. But I want NOT to spool lines, which have following criteria:
they contain more slashes. Find the last slash. After this one search the rest of the string for .*** (* = wildcard). If not found don´t spool, else spool.
Input file content for example:
c:/abc/abc/
c:/abc/abc/test.txt
c:/eee/
c:/eee/test.cfg
c:/test/abc/test/xxx/bbb/ccc/aaa/test.txt
c:/test/abc/test/xxx/bbb/ccc/aaa/
Output should look like:
c:/abc/abc/test.txt
c:/eee/test.cfg
c:/test/abc/test/xxx/bbb/ccc/aaa/test.txt
It is not static, where this lines appear, which should be removed. So I thought about finding the last slash and take all after that and look if there the last thing is ".***" If so keep else don´t echo
I don´t want to use other tools for this. It must be done via native command-line functionality.
Maybe somebody can help me out.
Code:
>OUTPUT.txt (
FOR /F "usebackq delims=" %%I IN ("FILE.txt") DO (
set "line=%%I"
setlocal enabledelayedexpansion
rem DO SOMEHTING HERE I DON`T KNOW HOW TO DO
echo(!line!)
)
)
just do it in one line using findstr and its regular expression mode (\....$ means all strings ending with . followed by 3 characters):
findstr /R \....$ FILE.txt
result:
c:/abc/abc/test.txt
c:/eee/test.cfg
c:/test/abc/test/xxx/bbb/ccc/aaa/test.txt

How to find particular contents in list of files

Please can someone tell how to find particular pattern in list of files.
I use the command:
dir *.txt /b /s >> C:\Users\Amrendra\Downloads\NIT_Testing\fileList.txt
In fileList.txt I have a list of files.
Now I want to find a certain pattern say abc in all the files listed.
So I want each line containing that pattern to be written into a new file.
Please suggest as I am completely new to batch command.
Thanks
I'm not sure if I got you completely right. You've echoed the names of all .txt files from some folder into one text file. Now you want to check it line by line for filenames containing some substring in the file name. Is that correct? If it is, here is the solution:
#ECHO OFF
SET targetFile=C:\Some\Path\SomeTextFile.txt
SET sourceFile=C:\Some\Path\Source.txt
SETLOCAL EnableDelayedExpansion
TYPE NUL>%targetFile%
FOR /F "tokens=*" %%L IN (sourceFile) DO (
SET templine=%%L
SET templine=!templine:abc=!
IF NOT !templine!==%%L (
ECHO %%L>>%targetFile%
)
)
Let's take a look on how it works: first we put the path to the file containing the file names into the variable %sourceFile% and the path to the target file into the variable %targetFile%. We need EnableDelayedExpansion to be able to work with changing variables inside the FOR-loop. TYPE NUL>%targetFile% simply clears the target file in case there are some entries from former runs.
The main work is done inside the FOR-loop. We read the source file line by line and want to check if the line contains the substring we are looking for. To do this we first store the line (%%L) in a temporary variable. Then we replace any occurrences of the search string with an empty string, means we simply remove abc from the temporary string. Finally we compare our modified temporary string with the original one. If they are equal we can be sure that the serch string abc was not in the original string. The other way around, if the strings differ, the substring was in the original string. So in the second case we do ECHO %%L>>%targetFile% which means we write the whole file name containing the search substring into our target file. Et voilà, we're done!
EDIT: As you want to search all listed files for the substring, here the new code:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET sourceFile=C:\Users\Amrendra\Downloads\NIT_Testing\fileList.txt
SET targetFile=C:\Users\Amrendra\Downloads\NIT_Testing\newList.txt
SET searchString=abc
TYPE NUL>%targetFile%
FOR /F "tokens=*" %%L IN (%sourceFile%) DO (
FINDSTR %searchString% "%%L">NUL
IF !ERRORLEVEL!==0 ECHO %%L>>%targetFile%
)
Here we are using findstr to check if a text file contains the string we are searching. As findstr usually would output the line which contains the searched string (which we don't want) we simply mute the command using >NUL. What we actually need is the ERRORLEVEL! If the string was found in a file findstr will set ERRORLEVEL to 0 or to 1 otherwise. So the only thing we have to do is to check if !ERRORLEVEL!==0.

Need batch file help to pull data from a text file, separated by a bunch of |||

I'm running windows vista and I need some help to write a batch program to pull out some data from a text file, so that I can generate another text file with the data in a specific format
The problem is that the text file data is separated by a bunch of |||| and I don't know how to remove or read the data between them.
Here's a sample of the data file:
MMM|^~\&|SSS||||20130813084347||RUR|14864-1W2220300-9|P^|2.3.0|||NE|ER
PID|||2013-1W2220300|-|LASTNAME^FIRSTNAME^||19971101|F|||||(416)222-3888||||||X2861673469 HY
and here's the output file that I need to generate.
currentdate currenttime REC F M 1W2220300 2861673469 HY LASTNAME FIRSTNAME 14864 13-AUG-2013 08:43:47
The "REC" "F" and "M" variables don't change, but the others do. Any help would be greatly appreciated, as my knowledge of batch files is pretty limited.
solution for sed for Windows
sed "y/|/ /" file
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%a IN (
q18241872.txt
) DO (
SET "var=%%a"
SETLOCAL enabledelayedexpansion
SET var=!var:^|=^| !
ECHO !var!
ENDLOCAL
)
)>newfile.txt
GOTO :EOF
This should produce newfile.txt from your original file which I've arbitrarily named q18241872.txt.
The new file should have each | separated by a space (nothing magical about space, could be any character within reason - # perhaps...) and you would then be able to process further using FOR/F "tokens=...delims=|" ... but since you give no clue as to the actual structure of your source nor the processing required to produce your desired output, that's where I'll have to leave it...

Resources