Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: BAT or VB file help


12 Jun 2013   #11

Windows 7 Ultimate x64
 
 

Im really sorry for the confusion. Ur script is exactly what i want.Thanks a lot.I got that syntax from another forum.

I have some files containing time records,
file1.txt,
ABAN,20130611,09:16:00,291.15,292
ABAN,20130611,09:17:00,292.35,292
ABAN,20130611,09:18:00,292.5,292
...................
.....................
file2.txt,
JOHN,20130611,09:17:00,291.15,292
JOHN,20130611,09:18:00,292.35,292
JOHN,20130611,09:19:00,292.5,292
......................
........................
file3,
...............
.................
Im trying to add ".fm" after every name. ie ABAN to ABAN.fm ( as a single word) and JOHN to JOHN.fm.
each file contains only on name(English word) and some numbers separated by "," in different lines.

Hope u understand. Sorry for wasting ur valuable time.


My System SpecsSystem Spec
.

12 Jun 2013   #12

W7 Pro SP1 64bit
 
 

Glad that the script worked. You are welcome.

Now let me explain a bit and then you can see if you can write the ".fm" one.


;AutoIt Version: 3.3.8.1
Using a ";" allows you to add comments to the code, In this case, it just states the AutoIt version that the code was written for. As versions change, some code may no longer work. But this is not a problem since you have the compiled file to use. You can also download old versions if need be - or never update your current version.


$search = FileFindFirstFile("*.txt")
This line puts info in the the variable named $search so that files in a folder can be found based on the filter of "*.txt".

If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
FileClose($search)
Exit
EndIf
This part lets you know if no files matched the filter.


While 1
This starts an infinite loop.

$file = FileFindNextFile($search)
Even though the function includes the words find and next, the first time this is encountered, it will be dealing with the very first file in the list of files that we are working with. The variable named $file contains info needed to work with a file.

If @error Then ExitLoop
Once there are no more files to work with, it is time to exit the infinite loop.

$whole_file = FileRead($file)
The contents of the file we are currently working with is read and put into the variable named $whole_file.

$file_handle = FileOpen($file, 2)
Reading the file (like we did in the previous line of code) does not require that we "open it". Once we open a file, we lock it so that no other process can make changes to it until we close the file. If you look in the help file under FileOpen, you will see what the "2" in ($file, 2) does. This is why your script created a blank file. We want to remove the current contents of the file so that we can write the new contents.

BTW, if you right click on the "au3" script file, one of the context menu options should be to edit the script. Selecting that option should open the customized editor. Click on or arrow over to a function like FileOpen and then press F1. The editor will take note of the function that you are on and will open the help file to the info for that function.


$new_file_info = ""
This is the variable that is going to hold the info that goes into the new file. We want it to start off empty too. A real programmer would create this variable a different way and probably in a different place in the script. I'm just self taught and I like to create them closer to where I use them. I find that easier to read.


$sentence_array = StringSplit($whole_file, @CRLF, 1)
This takes the info in the variable named $whole_file and splits it into lines.
$sentence_array[0] = the number of lines in the array
$sentence_array[1] = the first line of info from the file that we are currently working with
$sentence_array[2] = the second line of info from the file
......


For $i = 1 To $sentence_array[0]
Now we want to loop thru each of the lines from the file

$word_array = StringSplit($sentence_array[$i], " ", 1)
This splits each line into individual words
$word_array[0] = the number of words in the current line
$word_array[1] = the first word
$word_array[2] = the second word
.....

For your ".fm" script, you will want to split the "words" using a comma instead of a space.
$word_array = StringSplit($sentence_array[$i], ",", 1)



For $ii = 1 To $word_array[0]
Now we want to loop thru each of the words from the line that we are working on

If $word_array[$ii] = "and" Then $word_array[$ii] = "&"
If the word that we are working with = and - then replace it with &
This is the area of the script that you will want to modify in order to write the ".fm" script. I'm not positive, but maybe something like:
If $ii = 1 Then $word_array[$ii] &= ".fm"
That would be saying:
If we are working with the first word in the line, replace it with itself plus .fm


This next block of code is was added after I saw flaws in the output file.
I need to explain them out of order, so here is the block and below it is some info about the code
...................
If $ii = $word_array[0] Then
If $i = $sentence_array[0] Then
$new_file_info &= $word_array[$ii]
Else
$new_file_info &= $word_array[$ii] & @CRLF
EndIf
Else
$new_file_info &= $word_array[$ii] & " "
EndIf
...................
For your ".fm" script, you will want to change this line
$new_file_info &= $word_array[$ii] & " "
to
$new_file_info &= $word_array[$ii] & ","

Now back to explaining the script....
$new_file_info &= $word_array[$ii]
Originally, I had that one line of code instead of the block of code that you see above.

Remember that $new_file_info starts out empty. The line of code above appends the first word of the first line to the variable named $new_file_info as we rebuild the file. As the loops continue, each word from the file being worked on is added to the end of the variable named $new_file_info. Maybe you already see the problem. The words came out all crammed together - without a space between each word.


So, the code was changed to add a space after each word.
$new_file_info &= $word_array[$ii] & " "
But that also created a problem.
If the original file looked like this:
We were stranded and had to walk.
The new file would look like this:
We were stranded & had to walk. <there would be an extra space at the end

So, I had to add a condition like this:
If $ii = $word_array[0] Then
If the loop was working on the last word in a line, then do not put the space after it.

The same is true for the last line of the file. I don't want a carriage return line feed added after the last line of the new file; so, this condition was added:
If $i = $sentence_array[0] Then

I cannot explain that block of code much better than that - but the good news is, you don't have to change much if that code for your ".fm" script.

Next
This lets us work on the next word in a line until the line is finished.

Next
This lets us work on the next line in the file until the file is finished.

FileWrite($file_handle, $new_file_info)
This writes the new info to the empty file.

FileClose($file_handle)
This closes and unlocks the file that we just wrote to.

WEnd
This send the script back to the While line of code so that we can find and work with the next file.

FileClose($search)
After all files have been worked on, end the search.

MsgBox(0, "AutoIt", "Finished!")
Let the human know that the script has completed.


You've not wasted any of my time and my time is not all that valuable :-)
My System SpecsSystem Spec
12 Jun 2013   #13

Windows 7 Ultimate x64
 
 

Thanks for the point to point simple tutorial.Il try it myself.
My System SpecsSystem Spec
.


12 Jun 2013   #14

W7 Pro SP1 64bit
 
 

You are welcome.

I've corrected some typos and added a bit of info to that tutorial since your post above - so you might want to reload the page/post.
My System SpecsSystem Spec
12 Jun 2013   #15

Windows 7 Ultimate x64
 
 

yeh! Its working, i have to replace only a line. you are really a tech guru.
once again thank you for your valuable time.Expecting more support from you.
My System SpecsSystem Spec
12 Jun 2013   #16

W7 Pro SP1 64bit
 
 

Does the output file of your ".fm" script have spaces where the commas used to be?

I changed the tutorial to prevent such spaces.
My System SpecsSystem Spec
12 Jun 2013   #17

Windows 7 Ultimate x64
 
 

Hope you are online

adding these lines solve my script but it remove all "," in the file.Is there any solution for that.


$word_array = StringSplit($sentence_array[$i], ",", 1)
For $ii = 1 To $word_array[0]
If $ii = 1 Then $word_array[$ii] &= ".fm"
My System SpecsSystem Spec
12 Jun 2013   #18

W7 Pro SP1 64bit
 
 

Reload this page and look at the tutorial for this line:
$new_file_info &= $word_array[$ii] & ","
My System SpecsSystem Spec
12 Jun 2013   #19

Windows 7 Ultimate x64
 
 

im little anxious that make the problem.

This is the final script we made,

;AutoIt Version: 3.3.8.1

$search = FileFindFirstFile("*.txt")
If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
FileClose($search)
Exit
EndIf

While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop

$whole_file = FileRead($file)
$file_handle = FileOpen($file, 2)
$new_file_info = ""
$sentence_array = StringSplit($whole_file, @CRLF, 1)

For $i = 1 To $sentence_array[0]
$word_array = StringSplit($sentence_array[$i], ",", 1)
For $ii = 1 To $word_array[0]
If $ii = 1 Then $word_array[$ii] &= ".fm"
If $ii = $word_array[0] Then
If $i = $sentence_array[0] Then
$new_file_info &= $word_array[$ii]
Else
$new_file_info &= $word_array[$ii] & @CRLF
EndIf
Else
$new_file_info &= $word_array[$ii] & ","
EndIf
Next
Next
FileWrite($file_handle, $new_file_info)
FileClose($file_handle)
WEnd
FileClose($search)
MsgBox(0, "AutoIt", "Finished!")


Work well but it make".fm" at the end of last line in the file. Do i miss any line in the script?
My System SpecsSystem Spec
12 Jun 2013   #20

W7 Pro SP1 64bit
 
 

Starting with this:
ABAN,20130611,09:16:00,291.15,292
ABAN,20130611,09:17:00,292.35,292
ABAN,20130611,09:18:00,292.5,292

I get this when I run the script above:
ABAN.fm,20130611,09:16:00,291.15,292
ABAN.fm,20130611,09:17:00,292.35,292
ABAN.fm,20130611,09:18:00,292.5,292


Is that not the desired outcome?

BTW, when you post code to the forum, you press the icon with this symbol "#" and then paste the code between those code tags. That should let the code keep the indentions. And (if you are using the full script editor that I mentioned earlier) you can press Ctrl-t to tidy up those indentions as well as have it check for certain errors.
My System SpecsSystem Spec
Reply

 BAT or VB file help




Thread Tools




Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 07:57 AM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33