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


08 Jun 2013   #1

Windows 7 Ultimate x64
 
 
BAT or VB file help

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 System SpecsSystem Spec
.

08 Jun 2013   #2

W7 Pro SP1 64bit
 
 

Quote   Quote: Originally Posted by thanku View Post
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.


Attached Files
File Type: txt 1-remove-last-4.au3.txt (1.1 KB, 6 views)
My System SpecsSystem Spec
09 Jun 2013   #3

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
.


09 Jun 2013   #4

W7 Pro SP1 64bit
 
 

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


Attached Files
File Type: zip 1-remove-last-4-display.zip (340.7 KB, 2 views)
File Type: zip 1-remove-last-4.zip (340.8 KB, 0 views)
My System SpecsSystem Spec
09 Jun 2013   #5

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
09 Jun 2013   #6

W7 Pro SP1 64bit
 
 

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.


Attached Files
File Type: zip 1-remove-last-lines.zip (341.1 KB, 2 views)
My System SpecsSystem Spec
10 Jun 2013   #7

Windows 7 Ultimate x64
 
 

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?
My System SpecsSystem Spec
10 Jun 2013   #8

W7 Pro SP1 64bit
 
 

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-d...ml#post1841812

There are plenty of little tasks that people run into where a script could help out:
Stop Excel 2010 from appending .txt to file with existing extension

ctrl break in office 2010, windows 7, hp dv4t laptop

Calculator - is there a way to add "Always on top"?

This script only took one line of code to solve the problem that the OP was having:
Window cannot be resized nor moved.

:-)
My System SpecsSystem Spec
11 Jun 2013   #9

Windows 7 Ultimate x64
 
 

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 System SpecsSystem Spec
12 Jun 2013   #10

W7 Pro SP1 64bit
 
 

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[s] "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 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 10:48 PM.
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