Copy specific line and column from a file

December 23, 2010 at 11:10:43
Specs: Windows XP
I need to do the following using either a VBS or batch file.

Here is an example of what the script will need to do:
Copy Line 15, column 18 thru 23 from a text file and put it into another text file.

The point of this is i have around 30 files that the value in that place will change perodically, I need to be able to run the vbs or batch file and get the new values from all of these files and put them in one place. Ideally I would like to pull all of these values from the text document and put them into excel. I can do this myself if i have the files with all of the values in it. I can even have it just add the comma between the values, but i dont know how to pull this exact place from the file using a script. If anyone has one I would appreciate it.

Thanks!


See More: Copy specific line and column from a file

Report •


#1
December 23, 2010 at 17:34:08
I think this will do what you want, not fully tested & you will have to change the Pushd to suit your system. This is probably superbly inefficient, Sed might be a better choice in extracting a specific line.

Good luck

@echo off
setlocal enabledelayedexpansion
cls

pushd %temp%\input\

for /f "tokens=*" %%1 in ('dir /b *.txt') do (
    set filename=%%1

    more /e +14 "!filename!">"%temp%\tempfile.txt"
    set  /p line15=<"%temp%\tempfile.txt"

    for /f "tokens=18-23" %%1 in ("!line15!") do (
        set linefifteen=%%1 %%2 %%3 %%4 %%5 %%6
        echo !linefifteen!>>%temp%\linefifteen.txt

    )
)
popd

type %temp%\linefifteen.txt

Did you Google for an answer before asking the question?


Report •

#2
December 24, 2010 at 06:56:25
I tried modifying the script. I probably wasnt very clear on the files ill be grabbing. The file name does not have a .txt extension, it is just a text base file. The files dont have any extension at all. They are names 3210. and they are all in very different locations. I tried to modify the script to do this but i obviously failed miserably. If you have any modifications to add to this to do this I would appreciate it.
What my end goal is that I can just have a large script where each section pertains to each file. I have to do it this way because the line that i copy from each file will be different. however it will always be the same.

Thanks!


Report •

#3
December 28, 2010 at 00:47:34
Here's another attempt:

@echo off>%temp%\output.txt
cls
setlocal enabledelayedexpansion

set input=%temp%\1801,15,18-23,%%A %%B %%C %%D %%E %%F
call :fields

type %temp%\output.txt
exit /b

:fields
for /f "tokens=1-3* delims=," %%1 in ("!input!") do (
    set file=%%1&set line#=%%2&set toks=%%3&set flds=%%4
)

set /a line#-=1

more +%line#% !file!>%temp%\tempfile.txt
set /p inputline=<%temp%\tempfile.txt

for /f "tokens=%toks%" %%A in ("!inputline!") do (
    set output=%flds%
    echo !output!>>%temp%\output.txt
)

You must set an Input variable for each of your files. The variable consists of :
1) path\filename, (shown in the script as %temp%\1801)
2) line number you want to extract fields from, (shown as 15)
3) the tokens to be extracted from the line, (shown as 18-23)
4) For loop variables.

No allowance has been made for spaces in the path\filename.

Did you Google for an answer before asking the question?


Report •

Related Solutions

#4
December 29, 2010 at 06:36:16
Here is what the script was edited to for my specific instance.

setlocal enabledelayedexpansion

set input="P:\STOCK\I08\SA\IES\3210",15,18-23,%%A %%B %%C %%D %%E %%F
call :fields

type %temp%\output.txt
exit /b

:fields
for /f "tokens=1-3* delims=," %%1 in ("!input!") do (
set file=%%1&set line#=%%2&set toks=%%3&set flds=%%4
)

set /a line#-=1

more +%line#% !file!>%temp%\tempfile.txt
set /p inputline=<%temp%\tempfile.txt

for /f "tokens=%toks%" %%A in ("!inputline!") do (
set output=%flds%
echo !output!>>%temp%\output.txt
)

I removed the @echo because when I run the .vbs file it gives me an invalid character on line 1 character 1.

I then added the path to the file without quotes and it did not like the colon in the string.

So then I added quotes to the file path which is gave an error on character 37 of that line which is the comma after the quotes.

Above is the exact path of one of the files.

I have to run it from a different drive as the source file is on a mapped drive and running the script from the server itself is not possible.

I appreciate it, any further help would be great, not sure what needs to be done.

Thanks!


Report •

#5
December 29, 2010 at 13:48:14
You stated in your original post "I need to do the following using either a VBS or batch file."

My responses are in batch script format, there is no .VBS file in my responses Of course the script will throw up compilation errors if you try to run it in WScript or CScript. Try saving the script with a .BAT extension and running it by simply entering the filename.

Did you Google for an answer before asking the question?


Report •

#6
January 3, 2011 at 10:31:17
Sorry for the confusion on my part, I tested the script. the script writes the temp file and contains the entire file beginning at line 15.. for example if the files is like the one below


Line 1 Stuff
line 2 stuff
line 3 stuff

in the temp file it has

line2 stuff
line 3 stuff

However, it writes nothing to the output.txt file. It does create the file but nothing is written to it. Any ideas?


Report •

#7
January 3, 2011 at 23:58:21
So far so good.

I'm beginning to suspect that the fields in the input lines are not deliminated by spaces but by another character, say a comma which would mean they are CSV files, not simple text files. Please post a sample line from your input file.

Did you Google for an answer before asking the question?


Report •

#8
August 11, 2011 at 07:54:47
I would like to use this batch file to copy line 3 characters 15-18 to the output file.
I want the output file to contain the store number 9999

The following is the first 5 lines of the input text file.


[DaySheet]
StoreName = My Store
StoreNumber = 9999
MaxInvoiceAmount = 10000.00
ArchiveDays = 60


Report •

#9
August 12, 2011 at 21:16:57
Test this:

@echo off
cls

for /f "tokens=1* delims== " %%1 in (inputfilename.ext) do (
    if /i "%%1" equ "storenumber" echo %%2>outputfilename.ext&&goto :eof
)


Please come back & tell us if your problem is resolved.


Report •

Ask Question