Solved

Delete the last line in a Text File using a Bat

Posted on 2009-07-06
6
980 Views
Last Modified: 2012-05-07
I have a number of users on my network who each have a text file(XML) that needs to have the last line of the file removed.

I want to use a batch file to get this done as quickly as possible.

Every users file last line contains "</profiles>" without the quotes.
Attached is a sample of a users XML file I will be working with.

Thanks
<profiles>
  <profile id="0000000003" showindexes="True">
    <name><![CDATA[Bi-Weekly IT Update Agendas]]></name>
    <drawer id="0000000006"><![CDATA[My Files]]></drawer>
    <idxs>
      <idx id="000000000T" mapping="0" listid="19" type="list"><![CDATA[WESLEY]]></idx>
      <idx id="000000000U" mapping="0" listid="154" type="list"><![CDATA[BI-WEEKLY IT UPDATE AGENDA]]></idx>
      <idx id="000000000V" mapping="3" listid="0" type="string"><![CDATA[IT UPDATE AGENDA]]></idx>
      <idx id="000000000W" mapping="4" listid="0" type="date"><![CDATA[]]></idx>
    </idxs>
  </profile>
</profiles>

Open in new window

0
Comment
Question by:Raynovac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 125 total points
ID: 24788291
well the easiest way to remove a specific line when it can be uniquely identified like this is to use a find command:

del textfile.old 2>NUL
rename textfile.xml textfile.old
type textfile.old | find /v /c "/profiles" > textfile.xml
rem del textfile.old

Any other processing of xml type files line by line with dos is going to be "interesting" due to the use of the < and > chars.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24788315
Sorry remove the /c there... thats to count the number of lines.  It should have been /I (case insensitive).
Steve
0
 

Author Comment

by:Raynovac
ID: 24788531
I get an error stating the system cannot find the file specified.

Let me set back and explain more fully what I am trying to do.

I have 3 files.
UserProfiles.xml (Located in the users Documents and settings)
profileBuilder.bat (Located on the server and the bat that I am trying to create)
DefaultProfiles.txt (text contains more xml)

I want to removing the last line of the xml file I originally posted and appending the text in the attached code snippet which is more XML which includes the </profiles> closing tag.

I have the appending code correct and I have all the paths to make sure that the user running the script is the only one that will have their XML file changed.

Hope this clarifies what I am trying to do.



   <profile id="2147483646" showindexes="True">
    <name><![CDATA[Test to HOW TO]]></name>
    <drawer id="0000000003"><![CDATA[Library]]></drawer>
    <idxs>
      <idx id="000000000D" mapping="0" listid="10" type="list"><![CDATA[IT]]></idx>
      <idx id="000000000E" mapping="0" listid="68" type="list"><![CDATA[HARDWARE & SOFTWARE]]></idx>
      <idx id="000000000F" mapping="0" listid="88" type="list"><![CDATA[HOW TO]]></idx>
      <idx id="000000000H" mapping="2" listid="0" type="date"><![CDATA[]]></idx>
    </idxs>
  </profile>
</profiles>

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 84

Expert Comment

by:oBdA
ID: 24789268
That works actually just fine with for /f. Simply save the script below as "RemoveLastLine.cmd" or Whatever.cmd.
The script expects the file to be shortened as first argument (enclose in double quotes if the path contains a space).
If you want to hardcode the file, you can do so in line 3.
A backup file will be created.
@echo off
setlocal enabledelayedexpansion
set File=%~1
if not exist "%File%" (
  echo Syntax: %~nx0 ^<Filename^>
  echo Removes the last line in ^<Filename^>
  goto :eof
)
for %%a in ("%File%") do (
  set BackupFile=%%~dpna.bak
  if exist "%%~dpna.bak" del "%%~dpna.bak"
  ren "%%~fa" "%%~na.bak"
)
set Line=
for /f "delims=" %%a in ('type "%BackupFile%"') do (
  if not "!Line!"=="" >>"%File%" echo !Line!
  set Line=%%a
)

Open in new window

0
 

Author Closing Comment

by:Raynovac
ID: 31600296
It worked fine, it didn't occur to me to adjust the file names.  I had to adjust it a little to suit my needs but this is what I was pretty much looking for.

Thanks
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24789688
No problem, glad it helped... sorry wasn't around for a while there.

Steve
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert Systemjs to Webpack 3 121
How do I fix this situation in GIT? 2 71
activeMQ Queue Messages Enqueued and Messages Dequeued 1 96
junit initializtion error 2 17
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question