Computing.Net > Forums > Programming > Batch that will remove all text und

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Batch that will remove all text und

Reply to Message Icon

Name: niche
Date: July 13, 2007 at 11:07:14 Pacific
OS: XP
CPU/Ram: P4 2.2GHZ 1GB
Product: Dell/Precision
Comment:

I need a batch file that will skip the first 5 lines of an .ini file then delete the remaining text. The amount of text after the first 5 lines vary per .ini file. Basically I just need the first 5 lines of the .ini all text after needs to be deleted or if the first 5 lines can be copied to a new .ini that would work too.

This will need to work for 2000,XP and Vista PCs.
I think the command starts with
For F/ "skip=5 but I can't figure this one out. Please Help.




Sponsored Link
Ads by Google

Response Number 1
Name: tonysathre
Date: July 13, 2007 at 12:04:52 Pacific
Reply:

@echo off
for /f "skip=5" %%i in (oldfile.ini) do echo %%i >> newfile.ini

"Computer security." — Oxymoron


0

Response Number 2
Name: Razor2.3
Date: July 13, 2007 at 20:07:22 Pacific
Reply:

That'll do the exact opposite of what he wants. Here's a VBScript that fulfils the request.

Const sInFile = "some.ini"
With CreateObject("Scripting.FileSystemObject")
sOutName = .GetTempName
Set fOut = .CreateTextFile(sOutName)
With .OpenTextFile(sInFile, 1, False)
For i = 1 To 5
If .AtEndOfStream Then _
Exit For
fOut.WriteLine .ReadLine
Next
End With
fOut.Close
.DeleteFile sInFile, True
.GetFile(sOutName).Name = sInFile
End With


In completely unrelated news, my code would look much nicer if the <pre> tag was allowed.


0

Response Number 3
Name: ghostdog
Date: July 13, 2007 at 20:20:41 Pacific
Reply:

@tony, OP wants to keep the first 5 lines.
@OP, if you don't mind vbscript:
[code]
Option Explicit
Dim objFSO,numOfLines,myFile,objFile
Set objFSO=CreateObject("scripting.FileSystemObject")
numOfLines=5
myFile="C:\temp\inputfile.txt"
Set objFile = objFSO.OpenTextFile(myFile)
Do Until objFile.AtEndOfLine
If objFile.Line > numOfLines Then
WScript.Quit
Else
WScript.Echo objFile.ReadLine
End If
Loop

[/code]


0

Response Number 4
Name: FishMonger
Date: July 13, 2007 at 20:30:17 Pacific
Reply:

Since solutions other than the requested batch file are being offered, here are 2 perl commands. The first one creates a new ini file with the desired 5 lines and the second one does an inline edit of the ini file as well as creating a backup of the original.

========================================
c:\>perl -ne "print if $. < 6" somefile.ini > newfile.ini

c:\>perl -ni.bak -e "print if $. < 6" somefile.ini



0

Response Number 5
Name: Razor2.3
Date: July 13, 2007 at 20:35:05 Pacific
Reply:

Well, to be fair, VBScript could be considered more native to WinXP than batch files. Perl is pure third party.


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: July 13, 2007 at 20:40:22 Pacific
Reply:

So, are you saying that since Microsoft doesn't (yet) include it with the OS, that it should not be used or offered. If that's the case, then the majority of programs that people use should not be used.

Perl is free and open source and takes only a few minutes to install.


0

Response Number 7
Name: Razor2.3
Date: July 14, 2007 at 03:57:49 Pacific
Reply:

There's a reason people ask for batch scripts; they know command scripts are native to Windows (well, CMD). That means they don't need to download and/or install any additional software. I use (and offer) VBScript because of its nativity. To say a programmer shouldn't download a compiler or a script environment because it doesn't come with his OS is to say all Photoshopping should be done with MS Paint.

Now I'm not saying people shouldn't use Perl, or that MS will never provide Perl (actually, it's included with Win2K3's Resource Kit), but using Perl will require additional downloads for MS Windows. It's just more of a hassle than most command scripters are looking for. It's ironic if you consider how much time and effort a batch file will consume, but I digress.

And for the record, I have ActivePerl.


0

Response Number 8
Name: Mechanix2Go
Date: July 14, 2007 at 05:02:40 Pacific
Reply:

::== keep5
@echo off > new.ini
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (my.ini) do (
set /a N+=1
if !N! gtr 5 goto :eof
echo %%a >> new.ini
)



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 9
Name: FishMonger
Date: July 14, 2007 at 09:13:55 Pacific
Reply:

Razor,

Everything you said is true, however it is also true that some people are open to a non native solution even though they didn't specifically ask for one. Here's an example thread where the person said they needed a batch file, but ended up using the Perl solution, and was very happy about it.

http://computing.net/programming/ww...


0

Response Number 10
Name: tonysathre
Date: July 14, 2007 at 13:57:20 Pacific
Reply:

Sorry, I read the post wrong.


"Computer security." — Oxymoron


0

Response Number 11
Name: niche
Date: July 16, 2007 at 07:40:04 Pacific
Reply:

You guys are great!!! Lots of input and suggestions. I am going to work with the VBscript and try and learn that some more. I needed this quick and Mechanix2Go suggestion did exactly what I wanted it to do.

Thanks again!!!


0

Response Number 12
Name: Razor2.3
Date: July 16, 2007 at 18:23:17 Pacific
Reply:

niche: I am going to work with the VBscript and try and learn that some more.

Word of advice: We're here to help and we'll answer your questions, but don't try to learn from my sample code. I do a LOT of things wrong in the name of brevity, and the lack of allowed indentation kills any readability.


0

Response Number 13
Name: Mechanix2Go
Date: July 16, 2007 at 18:27:24 Pacific
Reply:

" the lack of allowed indentation kills any readability."

LOL

I don't indent these days.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 14
Name: ghostdog
Date: July 16, 2007 at 18:54:35 Pacific
Reply:

I prefer indentation, it makes code easier to read. any ideas on how to use indentation in this forum? whenever i post code, it all goes left aligned...


0

Response Number 15
Name: Razor2.3
Date: July 16, 2007 at 19:32:02 Pacific
Reply:

When I post C/C++ code, I Search+Replace two spaces with "..", but that renders the code unrunable.

"&nbsp;" doesn't work.

"<pre>" doesn't work.

The spaces are preserved, but the posting script replaces all line returns with "<br>", so it MIGHT look okay if you created a custom style sheet with td { white-space: pre }, but that'd only affect you, and not the random people asking questions.



0

Response Number 16
Name: FishMonger
Date: July 16, 2007 at 19:47:08 Pacific
Reply:

We need to talk to Justin and have him fix the indentation problem. The way he is parsing our posts is very unfriendly to us programmers.

When I want to indent, I add the html coding for the required number of leading spaces. This needs to be done at the stage where you review and confirm the posting.


0

Response Number 17
Name: ghostdog
Date: July 16, 2007 at 20:02:55 Pacific
Reply:

oh wow...seems its takes a bit of exercise.:). oh well, until that's done, i will have to live with it. thanks guys


0

Response Number 18
Name: niche
Date: July 18, 2007 at 09:16:25 Pacific
Reply:

Ok I have ran into an issue with this code.

::== keep5
@echo off > new.ini
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (my.ini) do (
set /a N+=1
if !N! gtr 5 goto :eof
echo %%a >> new.ini
)

It does what I want and works great until I insert it in with the rest of my batch file. My batch ends after this code is ran the code after this doesn't get processed.

I think it has something to do with setLocal EnableDelayedExpansion
I don't know what this does.


0

Response Number 19
Name: Mechanix2Go
Date: July 18, 2007 at 09:25:10 Pacific
Reply:

"My batch ends after this code is ran the code after this doesn't get processed."

not clear


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 20
Name: niche
Date: July 20, 2007 at 07:58:56 Pacific
Reply:

Mechanix2Go I want to insert the batch file you created (to only keep the first five lines) into a batch file I created. I have other tasks in my batch file that must complete after your code runs. After your code runs, the rest of my code isn't ran and my batch file terminates.

It's a batch file to add users to a program and after it copies their ini file it creates a shortcut then asks if they would like to add another user. After the code runs to "copy only the first 5 lines from the ini" the "shortcut" code and the "ask if they want to add another user" code isn't processed.

I have made other variables in my batch and if I remove "setLocal EnableDelayedExpansion" the rest of my batch will finish but then the ini is copied with all the contents not just the first five lines.


0

Response Number 21
Name: tonysathre
Date: July 20, 2007 at 08:22:33 Pacific
Reply:

::== keep5
@echo off > new.ini
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (my.ini) do (
set /a N+=1
if !N! gtr 5 goto :your_code
echo %%a >> new.ini
)

:your_code
...

"Computer security." — Oxymoron


0

Response Number 22
Name: Mechanix2Go
Date: July 20, 2007 at 11:24:31 Pacific
Reply:

niche,

If you're setting vars before the keep5 routine, they are probably getting lost. Try putting endlocal at the end of keep5:

==================================
::== keep5
@echo off > new.ini
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (my.ini) do (
set /a N+=1
if !N! gtr 5 goto :your_code
echo %%a >> new.ini
)
endlocal

:your_code


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 23
Name: niche
Date: July 20, 2007 at 11:49:27 Pacific
Reply:

the endlocal had no effect, here is all of my batch. I'm new at making Batch files so don't laugh.


:users
ECHO Please type the UserName.
SET /P variable=[UserName]
MD "C:\Program Files\lotus\notes\%variable%"
REM XCOPY /Y /D /I "C:\Program Files\lotus\notes\notes.exe" "C:\Program Files\lotus\notes\%variable%"
XCOPY /Y /D /I "C:\Program Files\lotus\notes\data\*" "C:\Program Files\lotus\notes\%variable%"
XCOPY /Y /D /I "C:\Program Files\lotus\notes\notes.ini" "C:\Program Files\lotus\notes\data"
REM DEL "C:\Program Files\lotus\notes\notes.ini"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\notes.ini"
GOTO inie

:inie
::== keep5
@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (C:\progra~1\lotus\notes\data\notes.ini) do (
set /a N+=1
if !N! gtr 5 goto :eof
echo %%a >> "C:\Program Files\lotus\notes\%variable%\notes.ini"
)
endlocal
echo Notesprogram=C:\Program Files\lotus\notes >> "C:\Program Files\lotus\notes\%variable%\notes.ini"
echo Winnticonpath=C:\Program Files\lotus\notes\%variable%\w32 >> "C:\Program Files\lotus\notes\%variable%\notes.ini"

rundll32.exe appwiz.cpl,NewLinkHere C:\Documents and Settings\%username%
XCOPY "C:\Documents and Settings\*.lnk" "C:\Documents and Settings\%username%\Desktop"
Del "C:\Documents and Settings\*.lnk"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\Help\*.nsf"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\*.id"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\*.nsf"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\*.dsk"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\desktop6.ndk"
DEL /F /Q "C:\Program Files\lotus\notes\%variable%\cache.ndk"

Echo Do you want to add another User Folder?
SET /P v2=[Y/N]
if %v2%==y GOTO users
if %v2%==Y GOTO users
if NOT %v2%==y GOTO exit


:exit
EXIT


0

Response Number 24
Name: Mechanix2Go
Date: July 20, 2007 at 12:55:43 Pacific
Reply:

I think you need to change this:
=========================
if !N! gtr 5 goto :eof
echo %%a >> "C:\Program Files\lotus\notes\%variable%\notes.ini"
)
endlocal
=============================
to this:
==============================
if !N! gtr 5 goto :donehere
echo %%a >> "C:\Program Files\lotus\notes\%variable%\notes.ini"
)
:donehere
endlocal
==============================


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 25
Name: tonysathre
Date: July 20, 2007 at 18:29:54 Pacific
Reply:

Wow, I need to look more closely at the code from now on. My suggestion was the worst ever.

"Computer security." — Oxymoron


0

Response Number 26
Name: niche
Date: July 26, 2007 at 11:40:29 Pacific
Reply:

ooohhh that looked so simple haha it works! yes! Thanks Mech :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Run and compile Java file... Syntax in VB6



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch that will remove all text und

batch file removing specific lines www.computing.net/answers/programming/batch-file-removing-specific-lines/18549.html

Batch file to remove directories www.computing.net/answers/programming/batch-file-to-remove-directories/13039.html

batch script www.computing.net/answers/programming/batch-script/14086.html