Solved Batch file to extract string of characters from txt-file

May 5, 2015 at 13:59:39
Specs: Windows 7

I hope someone here can help me--it would be greatly appreciated. :-)

I have a folder with a bunch of text files. I'm looking for a batch-file that extracts a string of exactly 5000 characters from all of these and puts them in a seperate file (1 for each). The place within each txt-file that the 5000 characters are taken from should be random (ie. if I run it a couple of times on the same original file, the extracted bit would be different). Afterwards, the original file should no longer contain this string.

So I have:

And I want:
filename_1.txt and filename_1_5000.txt
filename_2.txt and filename_2_5000.txt
filename_3.txt and filename_3_5000.txt

Does that make sense? Can anyone help me with this?


message edited by Carola

See More: Batch file to extract string of characters from txt-file

Report •

May 5, 2015 at 21:18:58
I recommend: forget batch for this one, (or use bartch to feed the filenames to vbscript) and use vbscript instead. Batch is severely "character-challenged". As long as the files aren't TOO big, vbscript could read the entire file and clip the random 5k.

Also, what happens if the random is less than 5k from EOF, or if the textfile is less than 5K?

message edited by nbrane

Report •

May 6, 2015 at 13:16:45
Thanks... I'll try to see if I can make that work for me.
(All the texts are way longer than 5000 characters, so I shouldn't have a problem here. If the total number of characters fluctuates a bit +/- 100 characters, I wouldn't mind it either.)

Report •

May 6, 2015 at 19:56:53
✔ Best Answer
Here's a sample VBS, "5K.vbs) to extract 5K bytes and writes two files. A batch just needs to feed the filenames to the VBS, (as it stands now. Too lazy to write it all in VBS). Output files are in same directory, with ".5K" extension, and ".XK" extension.
First a simple batch:
@echo off & setlocal
for %%a in ("*.txt") do cscript 5K.vbs "%%a"

Now the nominally but barely tested VBS:

'---- extract random sample from text file
set fso=createobject("scripting.filesystemobject")
randomize timer
fso.createtextfile(fil+".5K",8).write x
fso.createtextfile(fil+".XK",8).write x2

message edited by nbrane

Report •

Related Solutions

May 7, 2015 at 08:59:54
Thanks--I'll give it a try tomorrow and see if I can get it to work :-)

Report •

July 9, 2015 at 04:46:48
I got it to work. Thanks a million :-)

Report •

Ask Question