Link to home
Start Free TrialLog in
Avatar of johnb6767
johnb6767Flag for United States of America

asked on

Remove the last character from a line in batch.....

I have a simple text file that I have created in a batch script that is a result of the reg export of [HKEY_CURRENT_USER\printers\Connections]

Next line trims it down just fine, to the \\server\share format, but leaves the "]" at the end, that I cannot seem to remove..... Been fighting it long enough, now Ill let yall take a stab.. Sure its simple, but this one eludes me for some reason.....

So......

Goal is to get a text file with the following in it......

\\server\5034C]
\\server\5034C_PS]
\\server\PB-2050]
\\server\Xerox Phaser 6300N PS]
\\Computername\RICOH Aficio SP C222DN PCL 6]

To look like..... Just simply remove the last bracket.....

\\server\5034C
\\server\5034C_PS
\\server\PB-2050
\\server\Xerox Phaser 6300N PS
\\Computername\RICOH Aficio SP C222DN PCL 6

Avatar of Wayne Michael
Wayne Michael
Flag of United States of America image

Do you have vi?

if so edit with vi

then in vi type

:1,$s/]//g

Do you have to use a batch file? VBScript or Powershell script would be more flexable.
Avatar of knightEknight
another way ... download this tool:

   http://www.paulslore.com/utils/chgstr.zip

then you can change your text file like this:

  chgstr  "]"  ""  myfile.txt


... add that last line to your batch file and viola!
$File_To_Change = "C:\Full\Path\To\File.txt"
$File = Get-Item -path $File_To_Change
(Get-Content $File.FullName) | % {$_ -replace "]", ""} | Set-Content -path $File.FullName

Save above as xxx.ps1 (powershell script) and call it from your batch file will also work.
batch file command
Powershell &C:\xxx.ps1

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of johnb6767

ASKER

This is already a pre existing series of scripts designed to capture and rebuild profiles on reimaged machines, and im just looking for something to add to the existing script..... I know it can be done, just my syntax is whacked......

Like using the :~0, -1 switches......
Steve, I dont know why I didnt think of that..... Incredibly simple.... Works great!!!

I love it when you spend too much time overthinking......

Thanks to all for the quick help.....
Thanks!!
You guys and your scripting powers are amazing.  If any of you think you can solve this riddle, I would be greatly appreciative.  

https://www.experts-exchange.com/questions/26568755/Copy-data-via-Windows-scheduler.html