Sign in to follow this  
Followers 0
Driss

StringReplace for all files

7 posts in this topic

Hi,

Once again, I need your help.

I would like to know how to StringReplace all my xml files in folder1 to folder2. I have a code that works with a single file, I need this for all my xml files 100s of them, the example bellow works fine with a single file.

Dim $INFILEHANDLE, $OUTFILEHANDLE

Dim $DATA, $INFILE, $OUTFILE

$INFILE = "W:\web\old_xml_folder\file1.xml"

$OUTFILE = "W:\web\new_xml_folder\file1.xml"

$INFILEHANDLE = FileOpen($INFILE, 0)

$DATA = FileRead($INFILEHANDLE,FileGetSize($INFILE))

$DATA = StringReplace($DATA, "192.168.2.3", "172.16.1.3")

$OUTFILEHANDLE = FileOpen($OUTFILE,2)

FileWrite($OUTFILEHANDLE, $DATA)

FileClose($INFILE)

FileClose($OUTFILEHANDLE)

Thanks in advance

:) Driss

Share this post


Link to post
Share on other sites



You are definitely going to need a couple of loops or so...

Check help file like JdeB said, also is it absolutely imperative that you change that IP number and is it the same on all of them?

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

hi again,

I had this script from a while back, and I used it to change printers ip addresses from a logon script, and I thouth I could use same method to change my xml files. I looked at the help file and searched this forum for example, but Still no clue. Can you please help with an example of how to use filefindfirst and filefindnext with I am trying to do.

Yes all xml files have same ip address that needs to change to the new ip.

Thank you guys

Driss

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here we are. This should solve your problem. As you can see as destination I just set the original name + a '.dest' .

This is because I didn't wanted to play with strings, you should be able to modify as needed anyway.

I removed the handles since I think it is not important if you do only an action (reading/writing) because it does not improve speed.

Dim $C
Dim $DATA, $INFILE, $OUTFILE

$INFILE = _FileSearch("W:\web\old_xml_folder\*.xml", 5)

For $C = 1 To $INFILE[0]
   
   $OUTFILE = $INFILE[$C] & '.DEST'
   
   $DATA = FileRead($INFILE[$C], FileGetSize($INFILE[$C]))
   $DATA = StringReplace($DATA, "192.168.2.3", "172.16.1.3")
   
   FileWrite($OUTFILE, $DATA)
Next


Exit
Func _FileSearch($sIstr, $iSF)
  ; $iSF can sum up.
  ; $iSF = 4 means don't return also folders, only files.
  ; $iSF = 2 means stop at the first found.
  ; $iSF = 1 means looking in subfolders.
   
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sIstr, $iSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   While $sCS <> - 1
      $sCF = FileFindNextFile($sCS)
      If @error Then
         FileClose($sCS)
         ExitLoop
      EndIf
      If $sCF = '.' Or $sCF = '..' Then ContinueLoop
      If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sCP & $sCF), 'd')) Then
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
         If BitAND($iSF, 2) Then ExitLoop
      EndIf
   Wend
   
  ;And after, if needed, in the rest of the folders.
   If (BitAND($iSF, 2) Or BitAND($iSF, 3) And $sOutPut = '') Or (BitAND($iSF, 1) And Not BitAND($iSF, 2)) Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         While $iH <> - 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
               $sFP = $sCP & $sCF & '\';                            for future search
               $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
               While $iH2 <> - 1
                  $sCF2 = FileFindNextFile($iH2)
                  If @error Then
                     FileClose($iH2)
                     ExitLoop
                  EndIf
                  If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                  If Not (BitAND($iSF, 4) And StringInStr(FileGetAttrib($sFP & $sCF2), 'd')) Then
                     $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     If BitAND($iSF, 2) Then
                        FileClose($iH2)
                        FileClose($iH)
                        ExitLoop 3
                     EndIf
                  EndIf
               Wend
            EndIf
         Wend
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch
Edited by ezzetabi

Share this post


Link to post
Share on other sites

Hi,

Thanks for taking time to put this lengthy script together for me, but I am getting Error of Incorrect number of parameter infunction call in line 28. any input

Thanks again

Driss

Share this post


Link to post
Share on other sites

Thank you Larry, bew ver did it

Thank you ezzetabi for the lengthy script, realy appreciate it

Thank you all for the help. Once again you guys saved me a lots of sweat.

Driss

Share this post


Link to post
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
Sign in to follow this  
Followers 0