Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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

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.

@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]

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.inic:\>perl -ni.bak -e "print if $. < 6" somefile.ini

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

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.

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.

::== keep5
@echo off > new.ini
setLocal EnableDelayedExpansionfor /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

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.

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!!!

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.

" 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

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...

When I post C/C++ code, I Search+Replace two spaces with "..", but that renders the code unrunable.
" " 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.

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.

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

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.

"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

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.

::== 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

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

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

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

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

![]() |
Run and compile Java file...
|
Syntax in VB6
|

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