Extract substring


G

Guest

Folks:


I have a folder containing files with very long names that look like:
"105723456322 Three Singles To Adventure (123456) - English titles"
I can run the "Dir" command & pipe the results to a text file. I would like
to be able to extract various strings from these file names.

* How to extract the strings "123456" and 105723456322" then pipe the
results to some text file ?


Thanks,
JoJo.
 
Ad

Advertisements

B

billious

Folks:


I have a folder containing files with very long names that look like:
"105723456322 Three Singles To Adventure (123456) - English titles"
I can run the "Dir" command & pipe the results to a text file. I would
like
to be able to extract various strings from these file names.

* How to extract the strings "123456" and 105723456322" then pipe the
results to some text file ?


Thanks,
JoJo.
[Cross-post to ,microsoft.public.win3x_wfw_dos removed as apparently
irrelevant]

Suggest you look at alt.msdos.batch.nt & the FAQ in that group for many,
many examples.

"piping" means transferring the output of one command to the next. If you
are outputting to a text file, then you probably want to REDIRECT using the
">" (redirection) operator which sends the output of a command to a file
rather than the default output device (the console.)

Extracting strings is relatively simple - but your specification is far too
vague for a useful answer.

For instance, if you are using the "/b" switch to get your DIR listing (DIR
/B) then the output will be filename-only and not include the date/time,
size, etc.

We can't determine from your description whether you want precise substrings
or whether there's a better way. For instance, if the "123456" is always
contained in the first parenthesis-pair in your DIR line, then the command

for /f "tokens=2delims=()" %%i in ( ' dir /b ' ) do echo %%i >>filename.txt

would list that character string.

If the "105723456322" is the first space-separated token on the line, then

for /f %%i in ( ' dir /b ' ) do echo %%i >>filename.txt

will list those items.

If you want both, then they'd have to be combined - not a particularly
arduous task, but a lot easier if there's a better specification.

To substring in general, the command is

SET substring=%varname:~m,n%

where m is the start position, counting from 0 as the first character and n
is the length (and optional.)

But this is unlikely to be particularly useful where the data is as you've
implied - a variable-length title containing multiple space-separated words.
 
M

Marco A Achury P

JoJo escribió:
Folks:


I have a folder containing files with very long names that look like:
"105723456322 Three Singles To Adventure (123456) - English titles"
I can run the "Dir" command & pipe the results to a text file. I would like
to be able to extract various strings from these file names.

* How to extract the strings "123456" and 105723456322" then pipe the
results to some text file ?


Thanks,
JoJo.

Some languages as Perl are specially designed for this kind of works.

I like Euphoria, You can download euphoria (about 2 Mb) at
www.rapideuphoria.com.

The code is very simplified.
I suposse only one "(" per file name, and all filenames contains "(" and
")". No checking for overwrite file and other possible problems.

Using Euphoria language The program would be something like:



include file.e
sequence DirectoryDump
sequence Output
integer begin
integer end

DirectoryDump=dir("c:\mydir")
1Output = {}

for i = 1 to DirectoryDump[$] do
begin=find('(', DirectoryDump[NAME]) + 1
end = find(')', DirectoryDump[NAME]) - 1
Output = append(Ouput, DirectoryDump[begin..end]
end for


-- Now you have all the list of numbers at Output sequence
-- You can dump to a file.

-- Open for output, may overwrite existing file.
OuputFile = open("salida.txt", "w")

for i=1 to Output[$] do
puts (OutputFile, Output & "\n")
end for
 
M

Marco A Achury P

Would be great if windows a free subset of visual basic interpreter.
DOS allways had a basic interpreter so any user can make programs for
this kind of small talks.

Exist a windows script, but I have never hear someone who use it.

With qbasic you may make a similar program, the skeleton is:

shell "dir /b > tempdir.txt"
open "tempdir.txt" for input as #1
open "output.txt" for output as #2

while
Entry=lineinput #1
begin=instr("(",Entry) + 1
end=instr(")",Entry) -1
print #1, mid$(Entry, begin, end-begin+1)
wend


--
+-+-+-+-+-+-+-+
Marco A. Achury P.
Caracas, Venezuela
www.geocities.com/marcoachury


Marco A Achury P escribió:
JoJo escribió:
Folks:


I have a folder containing files with very long names that look like:
"105723456322 Three Singles To Adventure (123456) - English titles"
I can run the "Dir" command & pipe the results to a text file. I would
like
to be able to extract various strings from these file names.

* How to extract the strings "123456" and 105723456322" then pipe the
results to some text file ?


Thanks,
JoJo.

Some languages as Perl are specially designed for this kind of works.

I like Euphoria, You can download euphoria (about 2 Mb) at
www.rapideuphoria.com.

The code is very simplified.
I suposse only one "(" per file name, and all filenames contains "(" and
")". No checking for overwrite file and other possible problems.

Using Euphoria language The program would be something like:



include file.e
sequence DirectoryDump
sequence Output
integer begin
integer end

DirectoryDump=dir("c:\mydir")
1Output = {}

for i = 1 to DirectoryDump[$] do
begin=find('(', DirectoryDump[NAME]) + 1
end = find(')', DirectoryDump[NAME]) - 1
Output = append(Ouput, DirectoryDump[begin..end]
end for


-- Now you have all the list of numbers at Output sequence
-- You can dump to a file.

-- Open for output, may overwrite existing file.
OuputFile = open("salida.txt", "w")

for i=1 to Output[$] do
puts (OutputFile, Output & "\n")
end for
 
Ad

Advertisements

F

foxidrive

Would be great if windows a free subset of visual basic interpreter.
DOS allways had a basic interpreter so any user can make programs for
this kind of small talks.
Windows Scripting Host is available on Windows these days and does VBS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top