Need help with a Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ilneval
Posts: 7
Joined: 14 Feb 2011 11:33

Need help with a Batch File

#1 Post by Ilneval » 14 Feb 2011 12:11

I have documents of varying length, but every document has a phrase that is exactly 7 lines from the bottom of the document. This phrase varies from one document to the next. I need to write a batch file that will print out the value from those lines to a separate text file. Can anyone help me with this?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help with a Batch File

#2 Post by aGerman » 14 Feb 2011 13:05

You have to count the lines of the source file in a FOR loop. Then you could subtract 7 and skip the resulting number of lines.

Code: Select all

@echo off &setlocal
for /f "delims=:" %%a in ('findstr /n "^" "source.txt"') do set /a counter=%%a
set /a skip=counter-7
>"destination.txt" more +%skip% <"source.txt"

Regards
aGerman

Ilneval
Posts: 7
Joined: 14 Feb 2011 11:33

Re: Need help with a Batch File

#3 Post by Ilneval » 14 Feb 2011 15:04

I can't seem to get it to work. What I have is multiple XML files. Each individual files on the 7th line from the bottom has the parameter or text line that I need to copy to a new, separate text file.

Example

in my directory, I have the following files:
W001271-0181-0.xml
W001271-0182-0.xml
W001271-0183-0.xml
W001271-0184-0.xml
W001271-0185-0.xml

Each of those files are of varying length. Each has a result on the line that is 7th from the bottom on the file. I need the file to print the contents of that line from each of the individual XML's to a single text file called results.txt

I am not very good with this type of stuff, but my boss really needs it and I am the only one in the office this week. Please help

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help with a Batch File

#4 Post by aGerman » 14 Feb 2011 16:03

Good idea to tell me that the files are xml files. They need a special handling because
- there are special characters inside (especially < and >)
- probably they are UTF-8 encoded

Place this batch file in the same folder of your xml files, run it and have a look at what you will find in linecollection.txt.

Code: Select all

@echo off &setlocal
>"linecollection.txt" type nul
for /f "delims=" %%a in ('dir /a-d /b *.xml') do (
  set "source=%%~a"
  call :proc
)
del "tmp.txt"
goto :eof

:proc
for /f "delims=:" %%a in ('type "%source%"^|findstr /n "^"') do set /a counter=%%a
set /a skip=counter-7
>"tmp.txt" more +%skip% <"%source%"
set /p "line="<"tmp.txt"
setlocal enabledelayedexpansion
>>"linecollection.txt" echo(!line!
endlocal
goto :eof

Regards
aGerman

Ilneval
Posts: 7
Joined: 14 Feb 2011 11:33

Re: Need help with a Batch File

#5 Post by Ilneval » 14 Feb 2011 16:09

Ok, we are definitely getting closer. That linecollection.txt has the contents of the correct line, except it prints the result from the first XML only, and it duplicates it 5 times. I double checked, and each XML has a different line in the -7 position.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help with a Batch File

#6 Post by aGerman » 14 Feb 2011 16:28

That's strange, when I try the code it works.
Place a PAUSE command before the first "goto :eof" in the middle of the code and tell me whether it displays an error message.

Regards
aGerman

Ilneval
Posts: 7
Joined: 14 Feb 2011 11:33

Re: Need help with a Batch File

#7 Post by Ilneval » 14 Feb 2011 16:33

No error message when I put a pause command in.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help with a Batch File

#8 Post by aGerman » 14 Feb 2011 16:59

Hmm, I can't figure out why it doesn't work for you. Are these -7th lines too long for a batch variable (longer than 8191 characters)?
http://support.microsoft.com/kb/830473

Ilneval
Posts: 7
Joined: 14 Feb 2011 11:33

Re: Need help with a Batch File

#9 Post by Ilneval » 15 Feb 2011 10:01

I figured out what the problem was, a few of the XML files were incomplete and it was grabbing the 7th from the bottom, but since the file was incomplete it looked wrong. There is already a process to dlete incomplete files. I was testing the batch with older data, and some that hadn't been removed by the automated process. Thanks for all your help

Post Reply