I need help to make a batch code (if it's possible) to get substring from filename.
My filename can be like (filename lenght is changing):
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
file number - from left to first _
id1 - from 1 to n string with _ separator; for example C_C1_C2_C3_C4
id2 - always 9 digits; for example 011122558
date - for example 2015-07-07
extension .jpg
How to loop substring (file number, id1, d2, date) for all filenames in folder and put it to my code
convert - "file number" -annotate "id1" -annotate2 "id2" -annotate "date"
for example:
convert - "01" -annotate "C_C1" -annotate2 "012345678" -annotate "2015-07-07"
Thanks for help.
pure batch. Simple string manipulation mixed with tokenization. No need for additional utilities.
(g.txt holds your example file names; could be replaced by 'dir /b /a-d')
#echo off
for /f %%i in (g.txt) do call :process %%i
goto :eof
:process
set x=%1
set ext=%x:*.=%
for /f "delims=_" %%i in ("%x%") do set fileno=%%i
for /f "tokens=1,*delims=-" %%i in ("%x%") do (
set x1=%%i
set x2=%%j
)
for /f "tokens=1,* delims=." %%i in ("%x2%") do (
set dat=%%i
set ext=%%j
)
set id2=%x1:~-9%
for /f "tokens=1,* delims=_" %%i in ("%x1:~0,-10%") do set id1=%%j
echo filename %x%
echo ------------------------
echo Nr. %fileno%
echo ID1 %id1%
echo ID2 %id2%
echo Date %dat%
echo Ext. %ext%
echo ------------------------
echo convert - "%fileno%" -annotate "%id1%" -annotate2 "%id2% -annotate "%dat%"
echo(
echo(
goto :eof
Since you said Windows 7, I know you have Powershell available. Here is a Powershell script:
$re = '^(\d+)_((?:(?:[a-zA-Z0-9]+)_?)+)_(\d{9})-(\d{4}-\d\d-\d\d)\.(\w+)$'
dir | ForEach-Object {$_ -replace $re, 'convert "$1" -annotate "$2" -annotate2 "$3" -annotate3 "-$4"'}
Given the filenames you gave in your question
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
It will produce this text output:
convert "100" -annotate "C_CCC1_C2_C3_C4" -annotate2 "055555555" -annotate4 "2015-07-07"
convert "10" -annotate "D_D1" -annotate2 "011122558" -annotate4 "2015-07-07"
convert "7" -annotate "D_D1" -annotate2 "012345678" -annotate4 "2015-07-07"
convert "8" -annotate "A" -annotate2 "087654321" -annotate4 "2015-07-07"
(The filenames were sorted first, so the one starting with 100 comes first and the one starting with 8 comes last).
By redirecting this text output into a .cmd file, you can execute the convert commands as desired.
Here is the breakdown of that regular expression:
Beginning of line or string
[1]: A numbered capture group. [\d+]
Any digit, one or more repetitions
_
[2]: A numbered capture group. [(?:(?:[a-zA-Z0-9]+)_?)+]
Match expression but don't capture it. [(?:[a-zA-Z0-9]+)_?], one or more repetitions
(?:[a-zA-Z0-9]+)_?
Match expression but don't capture it. [[a-zA-Z0-9]+]
Any character in this class: [a-zA-Z0-9], one or more repetitions
_, zero or one repetitions
_
[3]: A numbered capture group. [\d{9}]
Any digit, exactly 9 repetitions
-
[4]: A numbered capture group. [\d{4}-\d\d-\d\d]
\d{4}-\d\d-\d\d
Any digit, exactly 4 repetitions
#echo off
setlocal enableextensions disabledelayedexpansion
rem For each file
for /r "x:\starting\folder" %%z in (*.pdf) do (
rem Separate number part
for /f "tokens=1,* delims=_" %%a in ("%%~nz") do (
set "_number=%%~a"
set "_file=%%~fz"
rem Separate date and ids
for /f "tokens=1,* delims=-" %%c in ("%%~b") do (
set "_date=%%~d"
set "_ids=%%~c\."
)
)
rem Separate id1 from id2 handling the string as a path
rem This way id2 is the last element and the path to it
rem is id1
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("::!_ids:_=\!") do (
endlocal
set "_id2=%%~nxe"
set "_id1=%%~pe"
)
rem Correct id1 contents (it is a path) changing backslashes
rem to underscores. As there are initial and ending backslashes,
rem later we will remove the initial and ending underscores
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("!_id1:\=_!") do (
endlocal
set "_id1=%%~e"
)
rem Execute final command
setlocal enabledelayedexpansion
echo(
echo file[!_file!]
echo convert - "!_number!" -annotate "!_id1:~1,-1!" -annotate2 "!_id2!" -annotate "!_date!"
endlocal
)
Related
My arqtext.txt has the following dataset:
A,B,C,
(123 or 456) and (789 or 012),1,5,
(456 or 654) and (423 or 947),3,6,
(283 or 335) and (288 or 552),2,56,
I want to change the 1st column of the last 3 rows to a new string set in the script, with the result like:
A,B,C,
roi1,1,5,
roi2,3,6,
roi3,2,56,
But my code only output the header "A,B,C,":
#echo off
setlocal EnableDelayedExpansion EnableExtensions
set roi1="(123 or 456) and (789 or 012)"
set roi2="(456 or 654) and (423 or 947)"
set roi3="(283 or 335) and (288 or 552)"
set /p "header="<"arqtext.txt"
echo %header%>arqtextnovo.txt
for /f "skip=1 tokens=1,* delims=," %%a in ("arqtext.txt") do (
if %%a=="roi1" (
echo roi1,%%b>>arqtextnovo.txt
)
if %%a=="roi2" (
echo roi2,%%b>>arqtextnovo.txt
)
if %%a=="roi3" (
echo roi3,%%b>>arqtextnovo.txt
)
)
rem EXIT /B
pause>nul
This is the way I would do it:
#echo off
setlocal EnableDelayedExpansion
set "roi[(123 or 456) and (789 or 012)]=1"
set "roi[(456 or 654) and (423 or 947)]=2"
set "roi[(283 or 335) and (288 or 552)]=3"
set /P "header=" < "arqtext.txt"
> arqtextnovo.txt (
echo %header%
for /f "usebackq skip=1 tokens=1,* delims=," %%a in ("arqtext.txt") do (
echo roi!roi[%%a]!,%%b
)
)
rem EXIT /B
pause>nul
The for /F command requires "usebackq" option if the filename is enclosed in quotes. Otherwise, it process the literal string enclosed in quotes.
It is more efficient to (redirect the whole output) > to a file, instead of append >> every new line. This also avoid the problems of redirect lines that ends in numbers.
If you have several values and want to select a result value based on the first one, it is much simpler and more efficient to use an array instead of test each individual value.
Let's suppose this method:
set /P "selector=Enter selector: "
if "%selector%" equ "nine" set result=9
if "%selector%" equ "seven" set result=7
if "%selector%" equ "five" set result=5
Instead, you may define an array called "value":
set "value[nine]=9"
set "value[seven]=7"
set "value[five]=5"
... and then directly get the result value this way:
set "result=!value[%selector%]!"
The same method is used in this code. However, you have not specified what happen if the input value is not one of the array elements.
For a further description on array management in Batch files, see this answer
I have a text file that contains lots of values on multiple lines with a different amount of spaces between values. Some spacing is 4, 6, 7, 9, etc. I have this code written but it only works for removing odd numbers of spaces (and leaving one space) which means that if I have 4 spaces between values (an even amount) I have no required space left.
Edit: max spacing is 13 and values per line are not the same.
Example text file:
123.000 345.555 # 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 | 34.3737373
This is what I want after the fix:
123.000 345.555 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 34.3737373
This is what I am getting with my code below:
123.000345.555 777.45600001.55555 66.878444
333.444555.4848 999.758584 34.3737373
How do I set spacing to one space between every value regardless of the amount of spaces? I am also removing the # and | symbols as well.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "conv_output_clean.txt" (
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "LINE= !LINE: =!"
set "LINE=!LINE:#=!"
set "LINE=!LINE:|=!"
echo(!LINE!
endlocal
)
)
endlocal
exit /B
Based upon your provided text content, and ignoring your | and # characters being removed, the following example should replace all concurrent space characters with a single one:
#SetLocal EnableDelayedExpansion
#(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("conv_output.txt") Do #(Set "_="
For %%H In (%%G) Do #If Not Defined _ (Set "_=%%H ") Else Set "_=!_!%%H "
Echo !_!)) 1> "conv_output_clean.txt"
If you want to also remove the | and # characters, then the following modification should work with your provided content:
#SetLocal EnableDelayedExpansion
#(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("conv_output.txt") Do #(
Set "_="&Set "#=%%G"&Set "#=!#:|=!"
For %%H In (!#:#^=!) Do #If Not Defined _ (Set "_=%%H ") Else Set "_=!_!%%H "
Echo !_!)) 1> "conv_output_clean.txt"
Yet another way, output redirection left for the reader to add.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "line="
for %%M in (%%L) do (
if "%%M" neq "#" if "%%M" neq "|" (
if defined line set "line=!line! "
set "line=!line!%%M"
)
)
echo(!line!
)
[ EDIT ] Sample run output.
C:\etc>type conv_output.txt
123.000 345.555 # 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 | 34.3737373
C:\etc>conv_output.cmd
123.000 345.555 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 34.3737373
C:\etc>
You can wind down number of spaces step by step using a while-like loop (or until-like loop). Unfortunately, we cannot use :label inside a command block enclosed in () parentheses so call a subroutine as follows:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
>"conv_output_clean.txt" (
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "LINE=%%L"
call :removeAddChars
setlocal EnableDelayedExpansion
echo(!LINE!
endlocal
)
)
endlocal
exit /B
:removeAddChars
set "LINE=%LINE:#=%"
set "LINE=%LINE:|=%"
:remove2Spaces
set "LINE=%LINE: = %"
set "_LINE=%LINE: = %"
if "%_LINE%"=="%LINE%" goto :eof
goto :remove2Spaces
Output:
type conv_output.txt
123.000 345.555 # 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 | 34.3737373
.\SO\63063411.bat
type conv_output_clean.txt
123.000 345.555 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 34.3737373
This will work with up to 16 spaces:
#echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("conv_output.txt") do (
set "LINE=%%~a"
set "LINE=!LINE:#=!"
set "LINE=!LINE:|=!"
set "LINE=!LINE: = !"
set "LINE=!LINE: = !"
set "LINE=!LINE: = !"
set "LINE=!LINE: = !"
echo !line!
)
It replaces 9 spaces with a single space, then 5, 3, and 2. If you add 17 you can (I believe) replace up to 32 spaces. Note it is imperative to remove the # and | first.
Another idea is to use call and shift:
#echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("conv_output.txt") do (
set "lineinput=%%~a"
set "lineinput=!lineinput:#=!"
set "lineinput=!lineinput:|=!"
call :process !lineinput!
)
goto :eof
:process
set lineoutput=%~1
shift
:loop
if "%~1"=="" echo %lineoutput%&&goto :eof
set lineoutput=%lineoutput% %~1
shift
goto :loop
I'll leave it up to you to put this into your code.
Here is one more algorithm as designed by Jeb on DosTips.com
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "conv_output_clean.txt" (
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "str=%%L"
setlocal EnableDelayedExpansion
set "str=!str:#=!"
set "str=!str:|=!"
set ^"str=!str: =a !"
set ^"str=!str: a =!"
set ^"str=!str:a = !"
echo(!str!
endlocal
)
)
endlocal
exit /B
Here is Jeb's explanation from that post. In Jeb's example it was replacing commas. So wherever he mentions a comma, it equates to the space in this users desired output
I want to use the replacing of a search pattern with nothing to remove all but one comma.
Obviously this can't work with simply replacing ","->"".
So I create first a better pattern
"," -> "a,,"
abc,,,,cba,,,,end
will be expanded to
abca,,a,,a,,a,,cbaa,,a,,a,,a,,end
Then each ",a," will be replaced with nothing
abca,,a,,a,,a,,cbaa,,a,,a,,a,,end
->
abca,,cbaa,,end
Now, exactly one pattern remains and will be replaced with the rule "a,," -> ","
And just to show the efficiency of some of the code answers, I did a test with a 2,000 line file.
JosezF: 0 Days 0 Hours 0 Minutes And 5 Seconds
Jeb: 0 Days 0 Hours 0 Minutes And 0 Seconds
Compo: 0 Days 0 Hours 0 Minutes And 0 Seconds
The code I used from Jeb and Compo's code are quite efficient. Less than 1 second for each of them.
Here is an alternative approach that is foolproof against empty lines/ lines that contain !
#echo off
SETLOCAL DisableDelayedExpansion EnableExtensions
>"process.txt" (FOR /F "delims=" %%L in ('
""%__APPDIR__%FINDSTR.EXE" /N "^^" example.txt"
') do (
set "lnCont=%%L"
SETLOCAL EnableDelayedExpansion
set "lnCont=!lnCont:*:=!"
FOR %%C in (^| #) do if DEFINED lnCont set "lnCont=!lnCont:%%~C=!"
if DEFINED lnCont FOR /F delims^=^ eol^= %%T in (^"!lnCont:^ ^=^
%=DO NOT REMOVE ME=%
!^") do if DEFINED proc (set "proc=!proc! %%T") ELSE set "proc=%%T"
echo(!proc!
ENDLOCAL
))
The loop:
parses the output of FINDSTR /N, line by line
replace all SPACEs with linefeeds
FOR /F ignore empty lines, therefore consecutive spaces are treated as one
One more way...
Get-Content .\conv_output.txt |
ForEach-Object { ($_ -replace '\|','') -replace '\s+',' ' }
Running from a cmd.exe shell or .bat file script...
powershell -NoLogo -NoProfile -Command ^
"Get-Content .\conv_output.txt |" ^
"ForEach-Object { ($_ -replace '\|','') -replace '\s+',' ' }"
For instance:
file.txt contains:
4.3 - random1
5.6 - random2
2.2 - random3
3 - random4
1.8 - random5
I need a command that will output the highest number only, not the preceding text.
Ie.
Output = 5.6
You can give SORTN.bat a try.
Here is the code for it as well.
#ECHO OFF
if "%~1"=="/?" (
echo.Sorts text by handling first number in line as number not text
echo.
echo.%~n0 [n]
echo.
echo. n Specifies the character number, n, to
echo. begin each comparison. 3 indicates that
echo. each comparison should begin at the 3rd
echo. character in each line. Lines with fewer
echo. than n characters collate before other lines.
echo. By default comparisons start at the first
echo. character in each line.
echo.
echo.Description:
echo. 'abc10def3' is bigger than 'abc9def4' because
echo. first number in first string is 10
echo. first number in second string is 9
echo. whereas normal text compare returns
echo. 'abc10def3' smaller than 'abc9def4'
echo.
echo.Example:
echo. To sort a directory pipe the output of the dir
echo. command into %~n0 like this:
echo. dir /b^|%~n0
echo.
echo.Source: http://www.dostips.com
goto:EOF
)
if "%~1" NEQ "~" (
for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
set f=,%%B
(
set f0=!f:~0,%n%!
set f0=!f0:~1!
rem call call set f=,%%%%f:*%%f0%%=%%%%
set f=,!f:~%n%!
)
for /f "delims=1234567890" %%b in ("!f!") do (
set f1=%%b
set f1=!f1:~1!
call set f=0%%f:*%%b=%%
)
for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`##$*_-+=:;',.?/\ " %%b in ("!f!") do (
set f2=00000000000000000000%%b
set f2=!f2:~-20!
call set f=%%f:*%%b=%%
)
echo.!f1!!f2!!f!,%%B
rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)
I gave it a try using this input as an example
4.3 - random1
11.3 - random6
5.6 - random2
2.2 - random3
100.1 - random8
3 - random4
1.8 - random5
11.12 - random7
11.11 - random7
This is how I ran it but you should be able to capture the output as well using a FOR /F command just like Stephan showed you in his answer.
type sortme.txt |sortn.bat
Output
1.8 - random5
2.2 - random3
3 - random4
4.3 - random1
5.6 - random2
11.11 - random7
11.12 - random7
11.3 - random6
100.1 - random8
sort will sort in correct order (Attention: this is sorting strings, not numbers, but will work fine for your example. Note that with string comparison 5 or 6.3 are "bigger" than 15).
Put a for around to process the ouput (Standard tokens is 1 and Space is a standard delimiter, so the for /f gets only the first element - your desired number)
for /f %%a in ('sort t.txt') do set high=%%a
echo %high%
EDIT to also process numbers higher than 10. Note: there is no math involved - it's just clever string manipulation.
#echo off
setlocal enabledelayedexpansion
(
for /f "tokens=1,2 delims=. " %%a in (t.txt) do (
set a=0000%%a
if "%%b"=="-" (echo !a:~-4!) else (echo !a:~-4!.%%b)
)
)>temp.txt
type temp.txt
pause
for /f "tokens=1,2 delims=0" %%a in ('sort temp.txt') do set high=%%a
echo %high%
The following script does proper sorting of fractional numbers with eight digits before and after the decimal separator . at most:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (input file; `%~1` means first command line argument)
set /A "_ZPAD=8" & rem // (number of zeroes to be used for temporary padding)
set "_ZERO=" & for /L %%K in (0,1,%_ZPAD%) do set "_ZERO=!_ZERO!0"
for /F "usebackq delims=- " %%L in ("%_FILE%") do (
for /F "tokens=1,2 delims=." %%I in ("%%L.") do (
set "INT=%_ZERO%%%I" & set "FRA=%%J%_ZERO%"
set "$NUM_!INT:~-%_ZPAD%!_!FRA:~,%_ZPAD%!=%%L"
)
)
set "MIN=" & set "MAX="
for /F "tokens=2 delims==" %%L in ('2^> nul set $NUM') do (
if not defined MIN set "MIN=%%L"
set "MAX=%%L"
)
echo Minimum: %MIN%
echo Maximum: %MAX%
endlocal
exit /B
How do I extract everything between incno and the space ie12345678 in the example batch below and put it into a incno variable?
#echo off
Set mystring=test incno12345678 wo5555 locFred Street
Echo "my string=%mystring%"
Echo incno
Echo wo
Echo loc
The incno can be 8 to 11 digits long but will always be between incno and a space in the string
I am now having trouble assigning the %%d variable to the mystring variable now it is in a for loop. Can anyone help me with this? See Below.
#echo off
SETLOCAL enableDelayedExpansion
color 0B
For /d %%d in ("C:\Users\%Username%\LocalData\*") do (
Echo "Folder = %%d"
Set mystring=%%d
echo "MyString = %mystring%"
pause
REM delete until (including) "incno":
set mystring=%mystring:*incno=%
echo %mystring%
REM delete starting with (including) " ":
set mystring=%mystring: =&rem %
echo "Incident Number = %mystring%"
pause
)
you can do it with substring replacement in two steps:
Set mystring=test incno12345678 wo5555 locFred Street
echo %mystring%
REM delete until (including) "incno":
set mystring=%mystring:*incno=%
echo %mystring%
REM delete starting with (including) " ":
set mystring=%mystring: =&rem %
echo %mystring%
set "mystring=test incno12345678 wo5555 locFred Street"
for /f %%V in ("%mystring:* incno=%") do set "incno=%%V"
Delayed expansion can be used if there is a chance that mystring might contain poison characters plus quotes:
setlocal enableDelayedExpansion
set mystring=test incno12345678 wo5555 loc"4th & Main"
for /f %%V in ("!mystring:* incno=!") do set "incno=%%V"
If the resultant value might contain ! (not an issue in this case) then delayed expansion must be toggled
setlocal disableDelayedExpansion
set mystring=test varABC!XYZ "&)^|<>"
setlocal enableDelayedExpansion
for /f %%V in ("!mystring:* var=!") do (
endlocal
set "var=%%V"
)
I have a simple text file with numbers like:
12345
45678
34567
89101
I need a batch that will return the nth line from this file. n should be taken from a command line argument.
I am very new to batch scripting so Thanks in advance for any help on this.
To get the file from the nth line you could use more +n (For line1 is n=0).
To split the rest of the file you could use a FOR /F loop.
This works even, if there are empty lines before the nth line.
It could be necessary to set the EOL to an unused character or to linefeed (default is ;)
set "lineNr=%1"
set /a lineNr-=1
for /f "usebackq delims=" %%a in (`more +%lineNr% text.txt`) DO (
echo %%a
goto :leave
)
:leave
You can use batch file extension.
token.bat
#echo off
setlocal ENABLEDELAYEDEXPANSION
set l=%1
set c=0
for /f "delims=" %%1 in ('type foo.txt') do (
set /a c+=1 && if "!c!" equ "%l%" echo %%1%
)
If you have a file like following,
foo.txt
AAAA
BBBB
CCCC
DDDD
And specify line number like following
token 3
You'll get
CCCC
You could use FOR /F with the skip parameter:
#ECHO OFF
SET skip=%1
SET /A skip-=1
IF %skip% LSS 0 GOTO out
IF %skip% GTR 0 SET params="skip=%skip%"
FOR /F %params% %%L IN (filename) DO (SET "line=%%L"& GOTO out)
:out
ECHO %line%
The skip parameter means the FOR /F loop must skip the specified number of lines at the beginning. The parameter is only applied if you specify a line number greater than 1. If you specify a number less than one or a non-number, the script outputs an empty string.
+1 for Jeb's solution... I did not realize you could use the more command to skip lines like that!
Here is an alternate method that I use for getting a specific line from a file (or from the multi-line output of another program):
#echo off
for /f "tokens=1* delims=:" %%a in ('findstr /n .* "C:\SomeFile.txt"') do (
if "%%a" equ "%1" echo.%%b
)
I use findstr /n .* "Path\FileName.ext" to add line numbers, and to ensure no empty lines are skipped by the for loop.
I then set "tokens=1* delims=:" to separate the line numbers from the line content.
Finally, I compare the current line number (%%a) with the line specified by the %1 parameter, and echo the line contents (%%b) on a match.
To Find Nth to Mth Character In Line No. L --- Example For Finding Label
#echo off
REM Next line = Set command value to a file OR Just Choose Your File By Skipping The Line
vol E: > %temp%\justtmp.txt
REM Vol E: = Find Volume Lable Of Drive E
REM Next Line to choose line line no. +0 = line no. 1
for /f "usebackq delims=" %%a in (`more +0 %temp%\justtmp.txt`) DO (set findstringline=%%a& goto :nextstep)
:nextstep
REM Next line to read nth to mth Character here 22th Character to 40th Character
set result=%findstringline:~22,40%
echo %result%
pause
exit /b
Save as find label.cmd
The Result Will Be Your Drive E Label
Enjoy
Based off of Amit's Answer, I made a utility called snip.cmd.
USAGE: snip FILE LINE FIRSTCHARACTER LASTCHARACTER
example:
foo.txt
//
// File Checksum Integrity Verifier version 2.05.
//
77752e4c91ba96dcc6c2bb4bdcdbdec5 c:\users\lapinot\desktop\token.bat
snip foo.txt 3 0 33 will yield 77752e4c91ba96dcc6c2bb4bdcdbdec5
Here is my code for snipe.cmd (you can add snipe to your command line by copying snip.cmd to c:\Windows\System32):
#echo off
:: USAGE
:: snip <file> [Line - Starts at 0] [First Column - Start Count at 0] [Last Colum]
set file=%1
set line=%2
set char1=%3
set char2=%4
for /f "usebackq delims=" %%a in (`more +%line% %file%`) DO (set findstringline=%%a& goto :nextstep)
:nextstep
echo echo %%findstringline:^~%char1%,%char2%%% > %temp%\result.bat
%temp%\result.bat
del %temp%\result.bat
:EOF
exit /b
echo "enter the line number.."; read i; awk 'NR==$i' <file_name>;