1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using batch file to open multiple text files using Notepad simultaneously

Discussion in 'Operating System' started by terrylau, Jul 2, 2009.

  1. terrylau

    terrylau New Member

    Joined:
    Jul 2, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Not sure whether it's relevant here but here goes...

    I want to open multiple text files in a folder using Notepad, all at the same time. Using command line window

    Start ->Run -> Cmd
    cd C:\Test
    for %X in (*.txt) do notepad %X

    will enable all the files open on top of one another.


    I want to create a batch file to automate this process but by doing :

    cmd
    for %%X in ("C:\Test\"*.txt) do notepad %%X

    will only open the first file File1.txt. After closing File1.txt, then only the second file File2.txt will open and so on.

    Is there a way for me to automate this process using batch file or any other programming language that'll be more suitable e.g. VB, Perl, C/C++, etc.

    Need your input and suggestions as still new to programming. Thanks.
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Hi and welcome to G4EF :)

    So, you want all txt files to be open simultaneously, right ?

    This is an easy task and can be done in any language. But I will prefer VBScript as you need not compile it and you can directly run it like a batch file. :)

    Code:
    Dim objFSO
    Dim MyFile
    Dim MyFolder
    Dim objShell
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = objFSO.GetFolder("C:\Test")
    Set objShell = WScript.CreateObject("WScript.Shell")
    
    For Each MyFile In MyFolder.Files
    	If Right(MyFile.Path,4) = ".txt" Then
    		objShell.Run("notepad " + MyFile.Path)
    	End If
    Next
    Copy the code above, paste it in notepad and save the file as "Test.vbs". Run it .. it will work :)

    The code is quite self explanatory, so you won't have difficulty understanding it. If you need to change the target folder, change the variable MyFolder.
     
  3. terrylau

    terrylau New Member

    Joined:
    Jul 2, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the welcome. You've got exactly what I need!!! :) Thanks again.
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My pleasure ! :)
     
  5. RandiR

    RandiR New Member

    Joined:
    Mar 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Nice solution. Nice forum. Here is an alternate script to open all .txt files in C:\Test one by one with notepad.

    Code:
     
    # Script txtfiles.txt
    cd "C:\Test" ; var str list ; lf -n "*.txt" > $list
    while ($list <> "")
    do
        var str file ; lex "1" $list > $file ; system ("\""+$file+"\"")
    done
    
    The script is in biterscripting ( biterscripting.com ) . You can flex the lf command to do different things.

    lf -rn "*.txt"
    Find files recursively in subfolders and subsubfolders.

    lf -rn "*.txt" "C:\folder"
    Find files in C:\folder".

    lf -rn "*.txt" "C:\folder" ($fsize > 5000)
    Find files whose size is greater than 5000 bytes.

    lf -rn "*.txt" "C:\folder" ( ($fsize > 5000) AND ($fctime > "20090101") )
    Find files whose size is greater than 5000 bytes AND whose creation time is after Jan 1, 2009.

    etc.


    Randi
     
  6. terrylau

    terrylau New Member

    Joined:
    Jul 2, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Wow.. nice... thanks..

    Btw, that looks like Perl scripting isit?
     
  7. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    That's bitterScripting, as he mentioned.
     
  8. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Nice code SP ;) in VB....I would like to implement same in .NET ;)
     
  9. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    i tried this vbs file to open multiple text files and get programming error on line 7 character 6 expected '='

    what does this mean
     
  10. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    The VBS file that I have posted works perfectly fine.
    Which VBS file are you referring to ? Could you (re-)post it here ?
     
  11. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Dim objFSO
    Dim MyFile
    Dim MyFolder
    Dim objShell

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = objFSO.GetFolder("C:\Test")
    Set objShell = WScript.CreateObject("WScript.Shell")

    For Each MyFile In MyFolder.Files
    If Right(MyFile.Path,4) = ".txt" Then
    objShell.Run("notepad " + MyFile.Path)
    End If
    Next
     
  12. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    got it working. i changed myfolder rather than c:\test and it failed now changed c:\test and it works.

    question. i cant program vb or most things. can i make this program date selective.
    when running it to open all text files in folder it locks the computer as there are over a 100 files. can i be date specific.
     
  13. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    e.g. files named 0310fle.txt can i make the program run all programs 0310*.txt
     
  14. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Of course you can. :)

    Code:
    Dim objFSO
    Dim MyFile
    Dim MyFolder
    Dim objShell
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = objFSO.GetFolder("C:\Test")
    Set objShell = WScript.CreateObject("WScript.Shell")
    
    For Each MyFile In MyFolder.Files
        If Right(MyFile.Path,4) = ".txt" And Left(MyFile.Name,4) = "0310" Then
            objShell.Run("notepad " + MyFile.Path)
        End If
    Next
    
    Explanation :
    In VB (and VBS), you use the Right, Left and Mid functions to extract sub-strings of specific lengths from specific positions in a string.
    So, what I basically do is; I check the last 4 characters of the file-path with Right(MyFile.Path,4). If it matches with ".txt", I open the file.

    Your question was to open 0310*.txt.
    How I modified the code is : I kept the previous check for txt extension + I added one more condition, where I check if the first 4 characters of the file-name (NOT file-path) is 0310. :)

    I hope this made the point clear. Here are some further examples :

    CRITERIA .......... CONDITION
    *.txt%%|%.......... Right(MyFile.Path,4) = ".txt"
    0310*.txt |.......... Right(MyFile.Path,4) = ".txt" And Left(MyFile.Name,4)="0310"
    *A?.txt |$$.......... Right(MyFile.Path,4) = ".txt" And Left(Right(MyFile.Path,6),2) = "A"

    :)
     
  15. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    works great. now one more question. can i input 0310 or any date from the desktop to the vbs file when i run it. will it stop and aske me for "0310" and how do i code it.?
     
  16. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Well, you can do it this way :

    Code:
    Dim objFSO
    Dim MyFile
    Dim MyFolder
    Dim objShell
    Dim Prefix
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = objFSO.GetFolder("D:\Test")
    Set objShell = WScript.CreateObject("WScript.Shell")
    
    Prefix = InputBox("(*) Enter prefix for file-names (e.g. for opening 0310*.txt files, enter 0310) :"+vbcrlf+vbcrlf+"(*) Leaving this blank or, clicking cancel will open *.txt files ...", "File-Name Prefix")
    For Each MyFile In MyFolder.Files
        If Right(MyFile.Path,4) = ".txt" And Left(MyFile.Name,Len(Prefix)) = Prefix Then
            objShell.Run("notepad " + MyFile.Path)
        End If
    Next
    
    This works for prefix only. So, you can open files like 0310*.txt, 0612*.txt, ABC*.txt etc.. but not anything like *ABC.txt or *0310.txt.
    Feel free to modify the code to suit your needs. :)
     
  17. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    you are so clever. that works perfectly. thank you. i wish i could program vbs.
    I am very greatful for your assistance. it has simplified a problem that was working on through dos batch files but couldnt get them working.
     
  18. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
  19. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Ref the above vb program working perfectly. could it be adapted to search specific htm files. in same folder e.g. ran24041.htm

    would be interested in your thoughts.
     
  20. topsy99

    topsy99 New Member

    Joined:
    Oct 4, 2009
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    also how can i reward you?
     

Share This Page