Jump to content

error


Recommended Posts

Try this line:

$Search = FileFindFirstFile($SourceFolder & "*.pcf")

replacing:

$Search = FileFindFirstFile("*.pcf")

Edit ========>>

I saw that it did not want to work in the "Examples Forum"

Please make sure that the first line:

Dim $FolderName = "C:\Documents and Settings\hnaveed\Desktop\Profiles"

Is spelled correctly and valid.

Edited by JoHanatCent
Link to comment
Share on other sites

Try this line:

$Search = FileFindFirstFile($SourceFolder & "*.pcf")

replacing:

$Search = FileFindFirstFile("*.pcf")

Edit ========>>

I saw that it did not want to work in the "Examples Forum"

Please make sure that the first line:

Dim $FolderName = "C:\Documents and Settings\hnaveed\Desktop\Profiles"

Is spelled correctly and valid.

yes it is spelled correctly and valid but it still doesent works

Link to comment
Share on other sites

What I read from your code is you search for files and replace the content (string) with another string.

Try this:

Look at post #19

I hope it helps you!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • Moderators

hassankhokhar11,

Please do not remove the code that was causing you a problem from the first post of a thread. :graduated:

It makes the rest of the thread meaningless. :(

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

can i do it without using array???

Yes you can!

Did the code I posted work for you?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

If the string is not found, do you just want it appended to the end of a file or do you want it written to a particular location in the file?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

@UEZ

Nice code but you may want to correct the function header for SearchAndReplace(). You left $replace out of the parameters section.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

@UEZ

Nice code but you may want to correct the function header for SearchAndReplace(). You left $replace out of the parameters section.

Indeed, thanks for the hint.

Corrected.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

can i get the corrected version??

Look at post #4. I forgot the $replace description in function header, so no impact to the function itself!

You can modify the code I wrote by adding the string to the last line easily!

Try it, if you need help just post it.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Just off the top of my head.

$sFile = "C:\some\path\somefile.txt"
$sStr = _Replace($sFile, "some string", "some other string");; or _Replace($sFile, "some string", "some other string", True) if you want it to be case sensitive.
If @Extended Then
    $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, $sStr)
    FileClose($hFile)
EndIf

Func _Replace($sString, $sFind, $sReplace, $bCaseSense = False)
    If FileExists($sString) Then $sString = FileRead($sString)
    Local $sSre = $sFind
    $sString = StringStripWS($sString, 2);; gets rid of trailing whitespace.  comment it out if you don't need it
    If $bCaseSense Then $sSre = "(?i)" & $sSre)
    $sString = StringRegExpReplace($sString, $sSRE, $sReplace);; Comment this line out if you instead want the next line
    ;;$sString = StringRegExpReplace($sString, "(?m:^).*" & $sSre & ".*", $sReplace);; Use this one if you want to replace an entire line if it contains $sFind
    If NOT @Extended Then $sString &= @CRLF & $sReplace
    Return SetError(0, 1, $sString)
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Look at post #4. I forgot the $replace description in function header, so no impact to the function itself!

You can modify the code I wrote by adding the string to the last line easily!

Try it, if you need help just post it.

Br,

UEZ

ive tried but its not working i might not be doing it the right way can u plz send the code so i can add that to ur old code...code for if the string is not found so it should write the string in that file....

Link to comment
Share on other sites

Try this:

#include <Array.au3>
Opt("MustDeclareVars", 1)
Dim $aFiles[1]

Local $path = "C:\Documents and Settings\hnaveed\Desktop\Profiles"
Local $file = ".pcf"
$aFiles = File_Seeker($path, $file, $aFiles, 1)
;~ _ArrayDisplay($aFiles)
Local $n, $i, $hFile
Local $search = "SDIUseHardwareToken=1"
Local $replace = "SDIUseHardwareToken=0"
For $i = 1 To UBound($aFiles) - 1
    $n = SearchAndReplace($aFiles[$i], $search, $replace)
    If $n = 0 Then
        $hFile = FileOpen($aFiles[$i], 1)
        FileWriteLine($hFile, $replace)
        FileClose($hFile)
        ConsoleWrite("Added line " & $replace & " to " & $aFiles[$i] & @CRLF)
    Else
        ConsoleWrite("Replaced " & $n & " times in file " & $aFiles[$i] & " " & $search & " with " & $replace & @CRLF)
    EndIf
Next

Exit

;==================================================================================================
; Function Name:    SearchAndReplace($file, $search, $replace, $CaseSensitive = False)
; Description:      searchs for a give string and replace it with another string.
;                   Overwrites file afterwards if replacement > 0!
; Parameter(s):     $file           filename to search for string
;                   $search         search for string
;                   $replace        replace it with this string
;                   $CaseSensitive  enable / disable case sensitivity
; Return Value(s):  amount of replacements
; Version:          v0.50 Build 2010-11-08 Beta
; Author(s):        UEZ
;==================================================================================================
Func SearchAndReplace($file, $search, $replace, $CaseSensitive = False)
    If Not FileExists($file) Then Return SetError(1, 0, "File not found!")
    Local $sReplace, $no
    Local $string = FileRead($file)
    If @error Then Return SetError(2, 0, "Could not read file!")
    If $CaseSensitive Then
        $sReplace = StringRegExpReplace($string, $search, $replace)
    Else
        $sReplace = StringRegExpReplace($string, "(?i)" & $search, $replace)
    EndIf
    Local $no = @extended
    If $no > 0 Then
        Local $hFile = FileOpen($file, 2)
        If @error Then Return SetError(3, 0, "Could not create file " & $file & "!")
        FileWrite($hFile, $sReplace)
        If @error Then Return SetError(4, 0, "Unable to write file!")
        FileClose($hFile)
    EndIf
    Return SetError(0, 0, $no)
EndFunc

;==================================================================================================
; Function Name:    File_Seeker($path, $file, $array)
; Description:      searchs for a file recursively on given drive
; Parameter(s):     $path   path where to search
;                   $file   filename to search for
;                   $array  to save the search results
;                   $ext    search for file extension, e.g. .mp3, .txt, .log
;                   $exact  to search for exact file name (not case sensitive)
;                   $rec    to search recursively for files and folders
;                   $folder include also folder names to search pattern
; Return Value(s):  an array with the full path of the files, $array[0] = amount of found files
; Version:          v0.82 Build 2010-02-13 Beta
; Author(s):        UEZ
;==================================================================================================
Func File_Seeker($path, $file, ByRef $aResults, $ext = 0, $exact = 0, $rec = 1, $folder = 0)
    Local $extended
    If $file <> "" And IsArray($aResults) And BitAND($ext, $exact) <> 1 Then
        If StringRegExp(StringLeft($path, 1), "(?i)[a-z]") And StringMid($path, 2, 1) = ":" Then
            If StringRight($path, 1) <> "\" Then $path &= "\"
            Local $seek, $suffix = ""
            Local $search = FileFindFirstFile($path & "*")
            If $search = -1 Then
                Return SetError(1, 0, 0)
            EndIf
            While 1
                $seek = FileFindNextFile($search)
                $extended = @extended
                If @error Then ExitLoop
                If $extended And $rec Then File_Seeker($path & $seek & "\", $file, $aResults, $ext, $exact, $rec, $folder)
                If $exact And Not $ext Then
                    If $seek = $file Then _ArrayAdd($aResults, $path & $seek)
                Else
                    If $ext Then
                        If StringRegExp($seek, "(?i:\" & $file & "+$\b)") And StringLeft($file, 1) = "." Then
                            If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                        EndIf
                    Else
                        If $file = "*" Then
                            If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                        Else

                            If StringInStr($file, ".") Then
                                If StringRegExp($seek, "(?i:\" & $file & ")+") Then
                                    If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                                EndIf
                            Else
                                If StringRegExp($seek, "(?i:" & $file & ")+") Then
                                    If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
            WEnd
            FileClose($search)
            $aResults[0] = UBound($aResults) - 1
            Return SetError(0, 0, $aResults)
        Else
            Return SetError(1, 0, 0)
        EndIf
    Else
        Return SetError(2, 0, 0)
    EndIf
EndFunc

I did not test it!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Try this:

#include <Array.au3>
Opt("MustDeclareVars", 1)
Dim $aFiles[1]

Local $path = "C:\Documents and Settings\hnaveed\Desktop\Profiles"
Local $file = ".pcf"
$aFiles = File_Seeker($path, $file, $aFiles, 1)
;~ _ArrayDisplay($aFiles)
Local $n, $i
Local $search = "SDIUseHardwareToken=1"
Local $replace = "SDIUseHardwareToken=0"
For $i = 1 To UBound($aFiles) - 1
    $n = SearchAndReplace($aFiles[$i], $search, $replace)
    If $n = 0 Then
        $hFile = FileOpen($aFiles[$i], 1)
        FileWriteLine($hFile, $replace)
        FileClose($hFile)
        ConsoleWrite("Added line " & $replace & " to " & $aFiles[$i] @CRLF)
    Else
        ConsoleWrite("Replaced " & $n & " times in file " & $aFiles[$i] & " " & $search & " with " & $replace & @CRLF)
    EndIf
Next

Exit

;==================================================================================================
; Function Name:    SearchAndReplace($file, $search, $replace, $CaseSensitive = False)
; Description:      searchs for a give string and replace it with another string.
;                   Overwrites file afterwards if replacement > 0!
; Parameter(s):     $file           filename to search for string
;                   $search         search for string
;                   $replace        replace it with this string
;                   $CaseSensitive  enable / disable case sensitivity
; Return Value(s):  amount of replacements
; Version:          v0.50 Build 2010-11-08 Beta
; Author(s):        UEZ
;==================================================================================================
Func SearchAndReplace($file, $search, $replace, $CaseSensitive = False)
    If Not FileExists($file) Then Return SetError(1, 0, "File not found!")
    Local $sReplace, $no
    Local $string = FileRead($file)
    If @error Then Return SetError(2, 0, "Could not read file!")
    If $CaseSensitive Then
        $sReplace = StringRegExpReplace($string, $search, $replace)
    Else
        $sReplace = StringRegExpReplace($string, "(?i)" & $search, $replace)
    EndIf
    Local $no = @extended
    If $no > 0 Then
        Local $hFile = FileOpen($file, 2)
        If @error Then Return SetError(3, 0, "Could not create file " & $file & "!")
        FileWrite($hFile, $sReplace)
        If @error Then Return SetError(4, 0, "Unable to write file!")
        FileClose($hFile)
    EndIf
    Return SetError(0, 0, $no)
EndFunc

;==================================================================================================
; Function Name:    File_Seeker($path, $file, $array)
; Description:      searchs for a file recursively on given drive
; Parameter(s):     $path   path where to search
;                   $file   filename to search for
;                   $array  to save the search results
;                   $ext    search for file extension, e.g. .mp3, .txt, .log
;                   $exact  to search for exact file name (not case sensitive)
;                   $rec    to search recursively for files and folders
;                   $folder include also folder names to search pattern
; Return Value(s):  an array with the full path of the files, $array[0] = amount of found files
; Version:          v0.82 Build 2010-02-13 Beta
; Author(s):        UEZ
;==================================================================================================
Func File_Seeker($path, $file, ByRef $aResults, $ext = 0, $exact = 0, $rec = 1, $folder = 0)
    Local $extended
    If $file <> "" And IsArray($aResults) And BitAND($ext, $exact) <> 1 Then
        If StringRegExp(StringLeft($path, 1), "(?i)[a-z]") And StringMid($path, 2, 1) = ":" Then
            If StringRight($path, 1) <> "\" Then $path &= "\"
            Local $seek, $suffix = ""
            Local $search = FileFindFirstFile($path & "*")
            If $search = -1 Then
                Return SetError(1, 0, 0)
            EndIf
            While 1
                $seek = FileFindNextFile($search)
                $extended = @extended
                If @error Then ExitLoop
                If $extended And $rec Then File_Seeker($path & $seek & "\", $file, $aResults, $ext, $exact, $rec, $folder)
                If $exact And Not $ext Then
                    If $seek = $file Then _ArrayAdd($aResults, $path & $seek)
                Else
                    If $ext Then
                        If StringRegExp($seek, "(?i:\" & $file & "+$\b)") And StringLeft($file, 1) = "." Then
                            If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                        EndIf
                    Else
                        If $file = "*" Then
                            If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                        Else

                            If StringInStr($file, ".") Then
                                If StringRegExp($seek, "(?i:\" & $file & ")+") Then
                                    If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                                EndIf
                            Else
                                If StringRegExp($seek, "(?i:" & $file & ")+") Then
                                    If ($extended And $folder) Or Not $extended Then _ArrayAdd($aResults, $path & $seek)
                                EndIf
                            EndIf
                        EndIf
                    EndIf
                EndIf
            WEnd
            FileClose($search)
            $aResults[0] = UBound($aResults) - 1
            Return SetError(0, 0, $aResults)
        Else
            Return SetError(1, 0, 0)
        EndIf
    Else
        Return SetError(2, 0, 0)
    EndIf
EndFunc

I did not test it!

Br,

UEZ

its saying variable $hFile is used without declaration

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