Jump to content

Constraint on size of variable used as $szSearchString within _ReplaceStringInFile?


krista611
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

I'm trying to change (one time event) hundreds of .csv files (each >10mb).  Within each file, there is a text string unique to it which I am attempting to find. The function will find the string value but only if I use the "text" in the function, not when I use $text.  Are there different sizes permitted for the $szSearchString argument used within the _ReplaceStringInFile function depending upon a string or variable used within the function? 

I've been building the script using a sample file.  When I use the string ("cmp_2008.6.20_thscc-2-06.20.2008-12.16.50") within _ReplaceStringInFile, the script works fine.  When I use a variable to which the string has been assigned, the function does not find the string value within the file.  The string value is pulled from an array built using _FileListToArray.  NB it's searching a clean version of the file each run

This works and the string value is replaced as desired.  $retval is > 0:

local $retval = _ReplaceStringInFile($File2Open, "cmp_2008.6.20_thscc-2-06.20.2008-12.16.50", $AfterStr)

This does not work.  String is not found.  $retval = 0

Global $BeforeStr =$ArrayFileName

Global $retval = _ReplaceStringInFile($File2Open, $BeforeStr, $AfterStr)

Have validated the string value against the variable value in testing with msgbox's.  Validated the array value is complete, not truncated.  There is something missing but I can't quite put my eyes on it.  Had hoped typing this post would trigger something but hasn't.

Link to comment
Share on other sites

I'm going to assume that the variable $BeforeStr contains the same text as your first example? What error code is returned when you run that? Is there an error when you run that?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you for the kind welcome, FireFox. 

The $ArrayFileName matches that string, cmp_2008.6.20_thscc-2-06.20.2008-12.16.50.  I compared the stringlen on both the full text of the string as well as the variable pulled out of the array and they match.  Thought maybe I'd dropped or gained a stray character or whitespace.

Hi,

Welcome to the autoit forum :)

What is the content of $ArrayFileName? If it's an array, you have to specify an index between brackets to access its contents.

Br, FireFox.

 

I'm going to assume that the variable $BeforeStr contains the same text as your first example? What error code is returned when you run that? Is there an error when you run that?

Yes, you assume correctly.  No error returned.  The $retval captures the number of occurences found which is 0.  No error - simply nothing found.

Link to comment
Share on other sites

We'd need a redacted data file and the actual script you're using, or a reasonable reproducer script, to figure out what is going on.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

We'd need a redacted data file and the actual script you're using, or a reasonable reproducer script, to figure out what is going on.

Was hoping to hide my amateur code ;)  New member so can't yet post a file but pasted a couple of sample lines from the .csv at the bottom of this post.

#include <file.au3>

#include <array.au3>

Global $FileList = 0

;populate array with files to be processed

;all files are .csv

;file count is 680

Global $FileList = _FileListToArray("C:Data", "*.csv", 0)

Global $FileDir = "C:Data"

;for $i = 1 to $FileList[0]

Global $testtext1 = "cmp_2008.6.20_thscc_2_06.20.2008_12.16.50.csv"

Global $FileName = $testtext1

;prd file name

;$FileName = $FileList[$i]

Global $sFileName = StringTrimRight($FileName, 4)

Global $NameStr = StringSplit($sFileName, "_")

;test array with file names 

;_ArrayDisplay($NameStr,"$NameStr")

;build new columns as strings

Global $newcols = ","& _ArrayToString($NameStr, ",",1)&","

;MsgBox(0,"new columns: ",$newcols)

Global $File2Open = $filedir&$FileName

MsgBox(0,"File to Open",$File2Open)

MsgBox(0,"filename: ",$FileName)

;search string

Global $BeforeStr = $sFileName

;Global $BeforeStr = "cmp_2008.6.20_thscc-2-06.20.2008-12.16.50"

MsgBox(0,"s filename and BeforeStr following: ",$sFileName & "  "& $BeforeStr)

Local $len1 = StringLen($BeforeStr)

Local $len2 = StringLen($BeforeStr)

MsgBox(0, "String length Before & After is:", $len1 & " " & $len2)

;replace string

Global $AfterStr = $sFileName & $newcols

;local $retval = _ReplaceStringInFile($filedir&$FileName, "cmp_2008.6.20_thscc-2-06.20.2008-12.16.50", $filename & "," & $newcols)

;local $retval = _ReplaceStringInFile($File2Open, "cmp_2008.6.20_thscc-2-06.20.2008-12.16.50", $AfterStr)

Global $retval = _ReplaceStringInFile($File2Open, $BeforeStr, $AfterStr)

If $retval = -1 Then

    MsgBox(0, "ERROR", "The pattern could not be replaced in file: " & $FileName & " Error: " & @error)

    Exit

Else

    MsgBox(0, "INFO", "Found " & $retval & " occurances of the pattern: " & $BeforeStr & " in the file: " & $FileName)

EndIf

;Next

 

*************************

First couple of lines from cmp_2008.6.20_thscc-2-06.20.2008-12.16.50.csv file

*************************

0.00   ,0,0.200      ,4.443      ,-4.399     ,,21.57  ,,,,,,,,,,,,,,,,,none,,,,,none,none,none,none,,373.49 ,,135.20 ,0.20   ,0.00   ,,9.00   ,,none,,,,,,,,,,,,,,,     0.000,     0.000,none,none,,cmp_2008.6.20_thscc-2-06.20.2008-12.16.50,

0.05   ,0,0.250      ,5.612      ,-5.350     ,  2448,21.22  ,  0.02,, 16.034,,   1,0.00   ,,,,,0.10   ,1630.76 ,8.75   ,,-3.50  ,   0,none,,,,,none,none,none,none,2  ,373.49 ,,133.79 ,0.25   ,0.00   ,,9.00   ,-6157.37,none,1.00 ,,,1.09 ,1.00 ,,0.00  ,0.00   , -0.0,115.35,-115.35,-158.055,2.32   , 14.42,     0.050,     0.050,none,none,     0,cmp_2008.6.20_thscc-2-06.20.2008-12.16.50,

0.10   ,0,0.300      ,6.760      ,-6.442     ,  2440,21.28  ,  0.01,  0.13, 16.069, 14.0,   0,0.00   ,0.01   ,0.00   ,0.13,0.13 ,-1.10  ,3797.12 ,40.50  ,-10.75 ,0.00   ,   0,none,,,,,none,none,none,none,2  ,373.46 ,0.02   ,133.80 ,0.30   ,0.00   ,-0.05  ,9.00   ,-6121.04,none,1.00 ,,0.51   ,1.09 ,1.00 ,,0.00  ,0.00   , -0.0,114.67,-114.67,-157.130,2.31   , 13.99,     0.100,     0.100,none,none,     0,cmp_2008.6.20_thscc-2-06.20.2008-12.16.50,

 

Link to comment
Share on other sites

  • Solution

You're looking for this ; "cmp_2008.6.20_thscc_2_06.20.2008_12.16.50" but the file contains this - "cmp_2008.6.20_thscc-2-06.20.2008-12.16.50" See the difference?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

The solution is usually in the last place you'd think to look. :)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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