Solved BAT or VB file help

thanku

New member
Local time
6:51 AM
Messages
21
Hi,
Some one please help me to make a bat or VB file to open all text files in a folder, delete last four lines from it and save it.I have know knowledge in programming,I am a typist.It will help a lot in my life.Thanks in advance.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
Hi,
Some one please help me to make a bat or VB file to open all text files in a folder, delete last four lines from it and save it.I have know knowledge in programming,I am a typist.It will help a lot in my life.Thanks in advance.
Welcome to the Seven Forums.

Some forum member might be able to do what you want using a batch file or a VB script. I'm more familiar with a scripting tool names AutoIt3... so I've written a little script that might do what you want.

You can download and install AutoIt3 from here.
Create a temporary folder on your desktop

Set your Windows (file) Explorer to show most file extensions so that you can rename the file attached to this post. [See this tutorial.]

Download the text (script) file attached to this post.
Rename that file - removing the last 4 characters ".txt".
(The file name should be now "1-remove-last-4.au3".)

COPY a few test files to be modified into the the same temporary folder that the script is in.
Double click on the the script file.

The script should make a window appear. that window should show you the contents of the first file found that ends in ".txt". The first line in that window is the number of lines in the file (including blank lines). Once you close that window, another one should appear. It should be showing the same info minus the last 4 lines. Close that window too.

A pair of windows (before edit and after edit) should appear for each file in the temporary folder. After the script completes, look thru each of the edited text files and make really sure that the results are to your liking. If the script removed the correct lines, then MOVE these newly edited file back too their original folder - replacing the original files with the edited ones.

After you have used the script a few times and after you have carefully checked the results a few times, then you can turn off the displaying of those before/after windows via the steps:
open notepad
drag and drop the script into notepad
change this line
$display = "yes"
to
$display = "no"
and save the file.

Now when you double click on the script, it will work thru each file without you having to interact with it.

Let me know if you have any problems getting this to work for you.
 

Attachments

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
Thank you very much for your effort.
My office computer not allow me to install any programs.thats why im looking for some bat files.currently im using some bat files for semi automation.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
Can you run software that does not have to be installed? I've compiled the script into an EXE.

1-remove-last-4-display.zip uncompresses to 1-remove-last-4-display.exe and like the name implies, that exe will display the contents of the files - before and after.

1-remove-last-4.zip
uncompresses to 1-remove-last-4.exe and does not display the file content when you run it.

You should always upload files like this to a website like virustotal.com. The links above are to virustotal scans. You will probably get false positives on most AutoIt scripts and I would not blame you if you did not feel comfortable running the files. Too bad - since the files accomplish what you want done. :-(
 

Attachments

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
You are really amazing thank you very much for your hard effort and quick reply.It works well for me.you really save a lot of time in my life. is there any way to change the last four lines to last five or eight lines by my self.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
Here ya go :-)

The file named 1-remove-last-lines.zip should uncompress to 1-remove-last-lines.exe.

The instructions are pretty much the same - only this version asks how many lines to remove and if you want to display the contents of the files.
 

Attachments

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
Thank you very much for your hard effort, I wold like to know more about you.What u do for living. How can i become like you?
 
Last edited:

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
You are welcome.

What I do for a living is not related to computing per se... but I do use several computers for most of my workday.

I mostly learn stuff by spending time in various forums. If you want to learn about Windows 7, then spend time in these forums. If you want to learn about AutoIt or AutoHotKey, then spend time in their forums. There are several scripting languages that can help you to write small tools like the one that I wrote for you. They do not require too much in the way of programming skills. AutoIt and AutoHotKey are two of many that I've used to help me do tasks at work and home.

It is not hard to compile a script into an EXE file - the second video in this post shows me doing that:
http://www.sevenforums.com/general-...ear-after-explorer-restart-2.html#post1841812

There are plenty of little tasks that people run into where a script could help out:
http://www.sevenforums.com/microsof...-txt-file-existing-extension.html#post2336160

http://www.sevenforums.com/microsof...eak-office-2010-windows-7-hp-dv4t-laptop.html

http://www.sevenforums.com/software/246353-calculator-there-way-add-always-top.html#post2048910

This script only took one line of code to solve the problem that the OP was having:
http://www.sevenforums.com/general-...dow-cannot-resized-nor-moved.html#post2302224

:-)
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
I installed autoit in my home PC and learning it.currently i try to make a script which replace "and" with "&" in all files in a folder following is the script i wrote but it completely delete all the contents of file.Pls help me to find out the mistake.


#include <array.au3>
$search = FileFindFirstFile("*.txt")
While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop
$whole_file = FileRead($file)
$whole_file_array = StringSplit($whole_file, @CRLF, 1)
$file_handle = FileOpen($file, 2)
$Text = FileRead($File,FileGetSize($File))
$Text = StringReplace($Text, "and", "&")
WEnd
MsgBox(0, "AutoIt", "Finished!")


what is the sintax to add "&" after every ENGLISH word.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
Rather than tell you were you went wrong, let's first clarify what it is that you want to do and then I'll explain the code needed to do that. If the goal is to make a script which replace "and" with "&" in all files in a folder, then you picked a pretty hard task to take on for your first script - but you had no way of knowing how hard that is :-)

Here is what comes to my mind when you described the script above.
One file might start off with these lines:
We were stranded and had to walk.
Then it started to rain and we got wet.

Once you run the script against that file, it would become:
We were stranded & had to walk.
Then it started to rain & we got wet.

BTW, depending on how you use StringReplace, you might have ended up replacing the "and" in stranded:
We were str&ed & had to walk.
Then it started to rain & we got wet.

The code to do what I think that you described above is here:
Code:
;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 $word_array[$ii] = "and" Then $word_array[$ii] = "&"
            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!")
I don't mind attempting to explain what each line of code does - but first I want to make sure that the code above does what you wanted done because you ended your post with this question:
what is the syntax to add "&" after every ENGLISH word

That would turn this:
We were stranded and had to walk.
Then it started to rain and we got wet.
into this:
We& were& stranded& and& had& to& walk&.
Then& it& started& to& rain& and& we& got& wet&.
or into this:
We & were & stranded & and & had & to & walk &.
Then & it & started & to & rain & and & we & got & wet &.

It is probably best that you provide a sample input file and a sample output file...
...then maybe I can write/explain some code.


You should probably download/install the
AutoIt Script Editor from here:
http://www.autoitscript.com/site/autoit/downloads/

Use that to write scripts. The regular installation of AutoIt3 includes a lite version of the editor, but you will like the full (still free) version.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
Thanks for the point to point simple tutorial.Il try it myself.
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
Reload this page and look at the tutorial for this line:
$new_file_info &= $word_array[$ii] & ","
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Dell
OS
Windows 7 Ultimate x64
CPU
intel
Motherboard
intel
Memory
8 GB
Graphics Card(s)
2 GB
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 Computer

Computer type
Laptop
Computer Manufacturer/Model Number
Employer provided Dell Latitude
OS
W7 Pro SP1 64bit
CPU
i7
Memory
8GB
Graphics Card(s)
Intel HD Graphics
Hard Drives
crappy SSD
Antivirus
Employer mandated Symantec Endpoint Protection
Browser
Pale Moon 64bit, IE11 64bit & Chrome 64bit
Back
Top