Jump to content

Help with a .bat file


coles
 Share

Recommended Posts

Hi guys

i am looking for any help with a .bat file i am working on.

I am trying to extract few info from a file and dump into a text file (which i am able to do)

i am stuck at the part of deleting few text from the output file.

 

FINDSTR "\<vehicle>" c:\temp\1.xml >> result.txt

which outputs the result as <vehicle>100</vehicle>.

how can i delete <vehicle></vehicle> so that just 100 remains in the new file or new dump file

i was trying FINDSTR /v <vehicle> c:\temp\result.txt >> fleet.txt which is not working 

any help will be really great full

 

Thank you

 

 

Link to comment
Share on other sites

You want to remove it using a bat file or perform with autoit?
If you take on AutoIt:
1. Extract the contents of <vehicle> and </vehicle> using StringBetween().

#include <String.au3>
#include <FileConstants.au3>
Local $NewContent
Local $aContent = FileReadToArray("result.txt")
For $i = 0 To UBound($aContent) - 1
    $NewContent &= _StringBetween($aContent[$i], "<vehicle>", "</vehicle>") & @CRLF)
Next
Local $hOpen = FileOpen("result.txt", $FO_OVERWRITE + $FO_CREATEPATH + $FO_ANSI)
FileWrite($hOpen, $NewContent)
FileClose($hOpen)


2. Remove <vehicle> and </vehicle> you can use StringReplace() or StringLeft/StringRight/StringTrimLeft/StringTrimRight.

#include <FileConstants.au3>
Local $NewContent = FileRead("result.txt")
      $NewContent = StringReplace($NewContent, "<vehicle>", "")
      $NewContent = StringReplace($NewContent, "</vehicle>", "")
Local $hOpen = FileOpen("result.txt", $FO_OVERWRITE + $FO_CREATEPATH + $FO_ANSI)
FileWrite($hOpen, $NewContent)
FileClose($hOpen)


3. If you want to create a program to use with BAT command:
SearchAndRepace.exe "FileContent.txt" "Strign to Search" "String to Replace"

#Region 
#AutoIt3Wrapper_Change2CUI=y
#EndRegion 
Local $NewContent, $OldContent, $cTMP = ""
If $CMDLine[0] >= 2 Then
    $OldContent = FileRead($CMDLine[1])
    If $CMDLine[0] > 2 Then $cTMP = $CMDLine[3]
    $NewContent = StringReplace($OldContent, $CMDLine[2], $cTMP)
    FileDelete($CMDLine[1])
    FileWrite($CMDLine[1], $NewContent)
    ConsoleWrite("Search:" & $CMDLine[2] & "Replace:" & $cTMP & @CRLF & "Repace On File:" & & @CRLF & $CMDLine[1] & @CRLF)
Else
    ConsoleWrite('Usage: "' & @ScriptName & '"FileToSearchAndRepaceContent' & '" ' & '"StringToSearch' '" ' & '"StringToReplace' & '"' & @CRLF)
EndIf

 

Edited by Trong
<vehicle> ==> </vehicle>

Regards,
 

Link to comment
Share on other sites

17 hours ago, Trong said:

You want to remove it using a bat file or perform with autoit?
If you take on AutoIt:
1. Extract the contents of <vehicle> and </vehicle> using StringBetween().

#include <String.au3>
#include <FileConstants.au3>
Local $NewContent
Local $aContent = FileReadToArray("result.txt")
For $i = 0 To UBound($aContent) - 1
    $NewContent &= _StringBetween($aContent[$i], "<vehicle>", "</vehicle>") & @CRLF)
Next
Local $hOpen = FileOpen("result.txt", $FO_OVERWRITE + $FO_CREATEPATH + $FO_ANSI)
FileWrite($hOpen, $NewContent)
FileClose($hOpen)


2. Remove <vehicle> and </vehicle> you can use StringReplace() or StringLeft/StringRight/StringTrimLeft/StringTrimRight.

#include <FileConstants.au3>
Local $NewContent = FileRead("result.txt")
      $NewContent = StringReplace($NewContent, "<vehicle>", "")
      $NewContent = StringReplace($NewContent, "<vehicle>", "")
Local $hOpen = FileOpen("result.txt", $FO_OVERWRITE + $FO_CREATEPATH + $FO_ANSI)
FileWrite($hOpen, $NewContent)
FileClose($hOpen)


3. If you want to create a program to use with BAT command:
SearchAndRepace.exe "FileContent.txt" "Strign to Search" "String to Replace"

#Region 
#AutoIt3Wrapper_Change2CUI=y
#EndRegion 
Local $NewContent, $OldContent, $cTMP = ""
If $CMDLine[0] >= 2 Then
    $OldContent = FileRead($CMDLine[1])
    If $CMDLine[0] > 2 Then $cTMP = $CMDLine[3]
    $NewContent = StringReplace($OldContent, $CMDLine[2], $cTMP)
    FileDelete($CMDLine[1])
    FileWrite($CMDLine[1], $NewContent)
    ConsoleWrite("Search:" & $CMDLine[2] & "Replace:" & $cTMP & @CRLF & "Repace On File:" & & @CRLF & $CMDLine[1] & @CRLF)
Else
    ConsoleWrite('Usage: "' & @ScriptName & '"FileToSearchAndRepaceContent' & '" ' & '"StringToSearch' '" ' & '"StringToReplace' & '"' & @CRLF)
EndIf

 

Thanks @Trong, you have no idea how much helpful it was.

 

Edited by coles
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...