Batch cmd to edit file content

PashPash Member Posts: 1,600 ■■■■■□□□□□
Hi Peeps,

Hope all be well for you in the world of IT!

I dunno if there are any batch cmd masters out there, but I need a batch script that can do this example:-

1. file named notes.ini has a line in it

directory path of c:\temp\folder.

2. This line needs to be changed to

directory path c:\program file\folder

The line needs to remain where it is in the .ini file

For anyone who may have guessed (because they might have done this in the past), I am doing a Lotus notes migration from version 6.5.4 to version 8 and the data folder paths will be be changing in the migration.

I was thinking of going the long way around and copying the file to a temp location, rename the file to something else, then use find or findstr to remove this line, and then use echo to put back in the new line, but im not sure how to gurantee the line will go where i want...

I hope someone can helpz me! Major thanks,

Cheers,

Pash
DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.

Comments

  • tierstentiersten Member Posts: 4,505
    PowerShell should be able to do what you want or use something like SED.
  • dynamikdynamik Banned Posts: 12,312 ■■■■■■■■■□
    I am willing to put money on Hero providing a PoSh solution. I'm also willing to put money on him intentionally not providing a solution in order to have me lose money. It looks like I'm going to break even on this one...
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Don't have time to write it all out, but PoSh could do that.

    Basic logic of script would be something like:

    #list of computers you want to do this to...
    $computers = get-content computers.txt
    foreach ($computer in $computers){
    get the contents of the current file
    logic to search for string and replace it
    variable for the new file to equal that altered version
    write the content over the old file on the file share
    }

    Something like that. Might be able to write it tonight.
    Good luck to all!
  • wd40wd40 Member Posts: 1,017 ■■■■□□□□□□
    I remember we did it manually when we upgraded from 5 to 6.5

    Scripting Guy is your friend

    http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    id love to do it in powershell guys, but i cant, because none of my clients can run powershell (not allowed on the update lists yet). meaning i couldnt run this scripts "locally from the client" tbh this would be a perfect opportunity for it as well icon_sad.gif

    Because im forced into using altiris deployment server, i have to create "run script" jobs on the server to perform most of my tasks in a numbered ping pongy type job setup. And for all intent and purpose, it works damn fine just with batch scripts and msiexec.exe flagged commands to get all my uninstalls and installs done. BUT if even if i do a run script job from the deployment server and choose to run the script on the server i have no way of feeding the PC name from altiris to the ps script for example to run remotely....

    Or if anyone thinks it is possible to do this, and has done it like this, i will be forever in your e-debt.

    Cheers,

    Pash
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    HeroPsycho wrote: »
    Don't have time to write it all out, but PoSh could do that.

    Basic logic of script would be something like:

    #list of computers you want to do this to...
    $computers = get-content computers.txt
    foreach ($computer in $computers){
    get the contents of the current file
    logic to search for string and replace it
    variable for the new file to equal that altered version
    write the content over the old file on the file share
    }

    Something like that. Might be able to write it tonight.

    Thanks for your pseudo code Hero! :)

    Yeh this is how i would do it, pretty much. Please dont write anything if its taking up your time fella, i could probably knock something fairly quickly. But i know you love your scripting ad id love to see dynamik outta money :D

    wd40 wrote: »
    I remember we did it manually when we upgraded from 5 to 6.5

    Scripting Guy is your friend

    http://www.microsoft.com/technet/scr...5/hey0208.mspx

    Cheers dude, this very well might be an option actually, i could indeed upload a vbs file to the client pc's and execute them. Silly me for overlooking vbs and the scripting guy :p

    I will let you all know how i get on and what option works....

    Cheers,
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Pash wrote: »
    id love to do it in powershell guys, but i cant, because none of my clients can run powershell (not allowed on the update lists yet). meaning i couldnt run this scripts "locally from the client" tbh this would be a perfect opportunity for it as well icon_sad.gif

    You could run it via PowerShell remotely from one computer.

    Give this a whirl:

    $computers = get-content computers.txt
    foreach ($computer in $computers){
    $oldfile = get-content "\\$computer\c$\path\notes.ini"
    $newfile = $oldfile | foreach {$_ -replace 'c:\temp\folder', 'c:\program file\folder'}
    $newfile | out-file "\\$computer\c$\path\notes.ini"
    }

    Change it as you need to. You need to run PowerShell with an account that has rights to access administrative shares on the computers you're targeting and write over the file. Make absolutely sure you test this on one or two computers before going hog wild.
    Good luck to all!
  • KaminskyKaminsky Member Posts: 1,235
    This is a great example of where unix simply powers over the MS OS. Simple push this through an awk filter.. practically a one liner.

    I'm no *nix lovin, sandle wearin, fluffy chin but you can get win command line versions of awk, sed and grep which you put on the command path and write batch files to do this kind of stuff with rediculous ease.

    Once had a contract to audit over 1500 PCs throughout SE UK doctor's practice and was given AIDA to do it with. Output from this is redulously obese... what was the weather like when the user last pressed the space bar.. exageration but that kind of rediculously useless level of detail. Each PC popped out about a 1k-2k text file of which I was only interested in about 15 relatively usefull details.... and that was going over the top.

    Having a unix background from uni days, I knocked up a quick win awk script which read every PCs file from each GP surgery and pushed out just the info I wanted into a sweet little csv. *nix is made for line processing. Push the file in and change the bits that match your filters and you get the same file out with the changes you want. As a future OS for the mases, I don't think it has a hope in hell in my lifetime but the extra juicy bits that come along with it should be utuilised by all IT people. Win, Net, dbas, devs, programmers, etc. , etc.

    No harm in multi skilling.... take the tools where you can get them.

    Something like the OP is looking for would be a doddle with text file line processing skills. Not programming skills but something every tech should have in their arsenal.

    Worth looking into
    Kam.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Kaminsky wrote: »
    This is a great example of where unix simply powers over the MS OS. Simple push this through an awk filter.. practically a one liner.

    I'm failing to see how a MS OS is getting overpowered here even with the first version of PowerShell. Once PowerShell 2.0 is released with remoting capability, it's practically a one liner there, too. To be honest, I could compress what I wrote to almost a one liner as is, just didn't have time to think it all the way through.

    I don't see anything in *nix comparable to PowerShell 2.0, which is due out with Win7/Server 2008 R2.
    Good luck to all!
  • tierstentiersten Member Posts: 4,505
    HeroPsycho wrote: »
    I don't see anything in *nix comparable to PowerShell 2.0, which is due out with Win7/Server 2008 R2.
    It'd be a combination of tools but its had very good scripting for years now. SSH etc... + Perl/whatever.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    tiersten wrote: »
    It'd be a combination of tools but its had very good scripting for years now. SSH etc... + Perl/whatever.

    Don't get me wrong. Before PowerShell, Windows was abysmal compared to *nix in this department.

    PowerShell changed everything.
    Good luck to all!
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    Kaminsky wrote: »

    No harm in multi skilling.... take the tools where you can get them.

    Yeh I totally agree, but sometimes learning new skills that arent workable for the majority of your daily tasks, is probably a waste of time (short term).

    I went with wd40's vbs link he provided, it works best within the scenario I actually explained. Thanks mate.

    Cheers,
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • wd40wd40 Member Posts: 1,017 ■■■■□□□□□□
    Can you give us the final script, just in case if we need it in the future :D
  • KaminskyKaminsky Member Posts: 1,235
    HeroPsycho wrote: »
    Don't get me wrong. Before PowerShell, Windows was abysmal compared to *nix in this department.

    PowerShell changed everything.

    Oh I wasn't dis'ing PowerShell. Never used it but just highlighting what has always been a massive failing with windows OSs.
    Kam.
  • SenHuSenHu Member Posts: 1 ■□□□□□□□□□
    The requirement is
    1. file named notes.ini has a line in it

    directory path of c:\temp\folder.

    2. This line needs to be changed to

    directory path c:\program file\folder

    The line needs to remain where it is in the .ini file

    Here is a small script in biterscripting ( http://www.biterscripting.com/install.html ).
    # Script autoedit.txt
    var str content ; cat "notes.ini" > $content
    sal -c "^directory path of c:\temp\folder.^" "directory path c:\program file\folder" $content > null
    echo $content > autoedit.txt
    

    The sal command means "String ALterer", -c means case insensitive.

    Save the script in file C:/Scripts/autoedit.txt. And execute it as follows from biterscripting.
     
    script autoedit.txt
    

    You can also call store it on the server, or even on a web site and execute it from there, such as
    script "http://www.yoursite.com/scripts/autoedit.txt"
    

    Sen
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    wd40 wrote: »
    Can you give us the final script, just in case if we need it in the future :D

    Sure.

    Const ForReading = 1
    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Temp\Notes Migration\notes.ini", ForReading)

    strText = objFile.ReadAll
    objFile.Close
    strNewText = Replace(strText, "Directory=C:\Program Files\notes\data", "Directory=C:\Program Files\IBM\Lotus\notes\data")

    Set objFile = objFSO.OpenTextFile("C:\Temp\Notes Migration\notes.ini", ForWriting)
    objFile.WriteLine strNewText
    objFile.Close
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
Sign In or Register to comment.