Sign in to follow this  
Followers 0
vaya

Write a string into all files in a folder

10 posts in this topic

Hello every body,

first of all , happy new year .

I would like to add a string into all files in a folder, I can list all files from this folder, but I can not write them.I would like to search of specific string on the file and write a new string on the same line .

here who I started my Code:

#Include <File.au3>

#Include <Array.au3>

$FileList=_FileListToArray(@ProgramFilesDir & "\PROFILES")

If @Error=1 Then

MsgBox (0,"","No Files\Folders Found.")

Exit

EndIf

FOR $element IN $FileList

;$file = FileOpen($filelist, 1)

;FileWrite($file, "xxxxxxxxxxxxxxxxxxxxxxx")

;FileClose($file)

MsgBox (0,"","files.")

;FileWrite($filelist, "xxxxxxxxxxxxxxxxxxxxxxx")

Next

I need your help

Thanks a lot

Share this post


Link to post
Share on other sites

FileListToArray doesn't return the full path, you must append it to each array element. Also you can use ReplaceStringInFile for the replacement. This will replace all instances of "AAA" with "BBB".

#include <File.au3>
#include <array.au3>

$path = @ProgramFilesDir & "\Autoit3"
$fileArray = _FileListToArray ($path)

For $X = 1 to $fileArray[0]
    _ReplaceStringInFile ($path & "\" & $fileArray[$X], "AAA", "BBB")
Next

Share this post


Link to post
Share on other sites

Thanks a lot,

I do not want to replace a string, but I would like to insert string BBB after AAA with "," on the same line. Any idea ?

I apreciate your help.

FileListToArray doesn't return the full path, you must append it to each array element. Also you can use ReplaceStringInFile for the replacement. This will replace all instances of "AAA" with "BBB".

#include <File.au3>
#include <array.au3>

$path = @ProgramFilesDir & "\Autoit3"
$fileArray = _FileListToArray ($path)

For $X = 1 to $fileArray[0]
    _ReplaceStringInFile ($path & "\" & $fileArray[$X], "AAA", "BBB")
Next

Share this post


Link to post
Share on other sites

I would like would like to insert string "BBB" on the same line where the string "AAA" is. any suggestions??

Thanks

Thanks a lot,

I do not want to replace a string, but I would like to insert string BBB after AAA with "," on the same line. Any idea ?

I apreciate your help.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That gets a little tricky.

#include <File.au3>
#include <Array.au3>

$path = @ProgramFilesDir & "\Autoit3"
$fileArray = _FileListToArray ($path)

For $X = 1 to $fileArray[0]
    ;Read file to array
    $tempArray = _FileReadtoArray($path & "\" & $fileArray[$X])
    
    ;Search for term in array
    ;Builtin ArraySearch will not suffice since it only returns first found instance
    For $Y = 1 to $tempArray[0]
        If StringInStr($tempArray[$Y], "AAA") Then
            ;Append string to to array element
            $tempArray[$Y] &= ",BBB"
        Next
    Next
    
    ;Write modified array to original file path, starting from element 1
    _FileWriteFromArray($fileArray[$X],$tempArray,1)
Next

EDIT: Typo in code

Edited by weaponx

Share this post


Link to post
Share on other sites

Unfortunatly it doesn't work :-(

I hope i ll get some help

Thanks

That gets a little tricky.

#include <File.au3>
#include <Array.au3>

$path = @ProgramFilesDir & "\Autoit3"
$fileArray = _FileListToArray ($path)

For $X = 1 to $fileArray[0]
    ;Read file to array
    $tempArray = _FileReadtoArray($path & "\" & $fileArray[0])
    
    ;Search for term in array
    ;Builtin ArraySearch will not suffice since it only returns first found instance
    For $Y = 1 to $tempArray[0]
        If StringInStr($tempArray[$Y], "AAA") Then
            ;Append string to to array element
            $tempArray[$Y] &= ",BBB"
        Next
    Next
    
    ;Write modified array to original file path, starting from element 1
    _FileWriteFromArray($fileArray[$X],$tempArray,1)
Next

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Try it like this

#include <File.au3>
#include <Array.au3>
Dim $tempArray[1]
$path = "C:\inwork"
$fileArray = _FileListToArray($path)
For $X = 1 To $fileArray[0]
    ;Read file to array
    _FileReadToArray($path & "\" & $fileArray[$X], $tempArray)
    ;Search for term in array
    ;Builtin ArraySearch will not suffice since it only returns first found instance
    For $Y = 1 To $tempArray[0]
        If StringInStr($tempArray[$Y], "AAA") Then
            ;Append string to to array element
            $tempArray[$Y] &= ",BBB"
            ;MsgBox(0, "", $tempArray[$Y])
        EndIf
    Next
    ;Write modified array to original file path, starting from element 1
    _FileWriteFromArray($path & "\" & $fileArray[$X], $tempArray, 1)
Next
Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

SpookMeister -

I'm not sure what you changed but I had a typo in my code above

Share this post


Link to post
Share on other sites

SpookMeister -

I'm not sure what you changed but I had a typo in my code above

@weaponx .......Check your PMs

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!"

Share this post


Link to post
Share on other sites

Thanks a lot, it works fine.

Great Help.

@weaponx .......Check your PMs

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