Page 1 of 1

Need help with a Batch File

Posted: 14 Feb 2011 12:11
by Ilneval
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?

Re: Need help with a Batch File

Posted: 14 Feb 2011 13:05
by aGerman
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

Re: Need help with a Batch File

Posted: 14 Feb 2011 15:04
by Ilneval
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

Re: Need help with a Batch File

Posted: 14 Feb 2011 16:03
by aGerman
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

Re: Need help with a Batch File

Posted: 14 Feb 2011 16:09
by Ilneval
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.

Re: Need help with a Batch File

Posted: 14 Feb 2011 16:28
by aGerman
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

Re: Need help with a Batch File

Posted: 14 Feb 2011 16:33
by Ilneval
No error message when I put a pause command in.

Re: Need help with a Batch File

Posted: 14 Feb 2011 16:59
by aGerman
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

Re: Need help with a Batch File

Posted: 15 Feb 2011 10:01
by Ilneval
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