Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Search file and edit it  (Read 14631 times)

0 Members and 1 Guest are viewing this topic.

Apexi

    Topic Starter


    Rookie

    Search file and edit it
    « on: March 24, 2011, 10:08:01 AM »
    Hi guys. I need some help with a batchfile i working on.
    Lets say i want to search for a file that is located somewere in C:\
    The file is a .vdf and editable with notepad. Simply i want to edit some text to that file at the end of all other text.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Search file and edit it
    « Reply #1 on: March 24, 2011, 11:10:21 AM »
    Here is a start
    Code: [Select]
    C:
    cd \
    dir *.vdf /s /b >list.txt
    The above makes a list of all such files with the path.
    That is as much as I know.

    Apexi

      Topic Starter


      Rookie

      Re: Search file and edit it
      « Reply #2 on: March 24, 2011, 12:15:04 PM »
      No you misunderstood me  :)
      Let me put it this way. I have a file named test.vdf contain some text and i dont know where the file is located.
      First of all the test.vdf file have to be found, then automaticly insert some text into the test.vdf file at the end after the original text. Not rewrite.

      And if this is possible. Whats the command to change directory automaticly to the file when its found by dir *text.vdf*.* /s
      « Last Edit: March 24, 2011, 12:25:11 PM by Apexi »

      col. sanders



        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: Search file and edit it
        « Reply #3 on: March 24, 2011, 12:28:10 PM »
        Here ya go.  You needed two ">>".  This will find it, and "append" to the end of the original text you use, not replace it.

        cd\
        dir/s/p/b \*.vdf >> test.vdf


        Apexi

          Topic Starter


          Rookie

          Re: Search file and edit it
          « Reply #4 on: March 24, 2011, 01:16:07 PM »
          Does not work properly.

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: Search file and edit it
          « Reply #5 on: March 24, 2011, 01:28:03 PM »
          I was trying to dereference Null Pointers before it was cool.

          Salmon Trout

          • Guest
          Re: Search file and edit it
          « Reply #6 on: March 24, 2011, 01:36:18 PM »
          What kind of file is this vdf? Is it an Avira virus definition file? You don't where it's located, but you want to modify it. Hmmmm......
           

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Search file and edit it
          « Reply #7 on: March 24, 2011, 01:44:08 PM »
          What kind of file is this vdf? Is it an Avira virus definition file? You don't where it's located, but you want to modify it. Hmmmm......
          It must be a conspiracy.   >:(

          Salmon Trout

          • Guest
          Re: Search file and edit it
          « Reply #8 on: March 24, 2011, 01:45:08 PM »
          It must be a conspiracy.   >:(

          Not necessarily, but it makes me curious.

          Apexi

            Topic Starter


            Rookie

            Re: Search file and edit it
            « Reply #9 on: March 24, 2011, 02:31:27 PM »
            No worries. I dont really know what .vdf stands for, but it belongs to Steam. All of the saved online servers ends up in that file. Sometimes the saved server dissapear, a well known bug. Since me and my friends have Steam installed on different directories the batch script need to find the place first.

            Salmon Trout

            • Guest
            Re: Search file and edit it
            « Reply #10 on: March 24, 2011, 02:36:11 PM »
            No worries. I dont really know what .vdf stands for, but it belongs to Steam. All of the saved online servers ends up in that file. Sometimes the saved server dissapear, a well known bug. Since me and my friends have Steam installed on different directories the batch script need to find the place first.

            If you are talking about masterserver.vdf, I read this...

            Quote
            Making the VDF read-only prevents your game or Steam from overwriting it in some cases.




            Apexi

              Topic Starter


              Rookie

              Re: Search file and edit it
              « Reply #11 on: March 24, 2011, 03:09:29 PM »
              Not really what i looking for but thanks anyway. Since no one can help me out i'll have to ask another question.
              Whats the command to change directory automaticly to the file when its found by dir *text.vdf*.* /s

              Salmon Trout

              • Guest
              Re: Search file and edit it
              « Reply #12 on: March 24, 2011, 03:14:30 PM »
              Not really what i looking for but thanks anyway. Since no one can help me out i'll have to ask another question.
              Whats the command to change directory automaticly to the file when its found by dir *text.vdf*.* /s

              for /f "delims==" %%A in ('dir *text.vdf*.* /s') do (
                  set filefolder="%%~dpA"
                  set filepathandname="%%~dpnxA"
                  )


              Apexi

                Topic Starter


                Rookie

                Re: Search file and edit it
                « Reply #13 on: March 24, 2011, 03:48:28 PM »
                Did not either work. I just end up in the same directory i begin at.

                Salmon Trout

                • Guest
                Re: Search file and edit it
                « Reply #14 on: March 24, 2011, 03:55:58 PM »
                Did not either work. I just end up in the same directory i begin at.

                what happens if you put

                echo "%%~dpnxA"

                in the script?

                and what's with the second wildcard in this?

                *text.vdf*.*

                I suspect it's not finding anything, as it's looking for a file with 2 dots



                ghostdog74



                  Specialist

                  Thanked: 27
                  Re: Search file and edit it
                  « Reply #15 on: March 24, 2011, 07:43:40 PM »
                  Here ya go.  You needed two ">>".  This will find it, and "append" to the end of the original text you use, not replace it.

                  cd\
                  dir/s/p/b \*.vdf >> test.vdf

                  did you understand what the question is about? 

                  Apexi

                    Topic Starter


                    Rookie

                    Re: Search file and edit it
                    « Reply #16 on: March 29, 2011, 12:14:25 PM »
                    what happens if you put

                    echo "%%~dpnxA"

                    in the script?

                    and what's with the second wildcard in this?

                    *text.vdf*.*

                    I suspect it's not finding anything, as it's looking for a file with 2 dots

                    The same as before. I end up in C:\ And i know that the file is there, i'm looking at it right now :P

                    Salmon Trout

                    • Guest
                    Re: Search file and edit it
                    « Reply #17 on: March 29, 2011, 12:27:40 PM »
                    You didn't answer this question:

                    Quote from: Me, before
                    and what's with the second wildcard in this?

                    *text.vdf*.*

                    and did you read this?

                    Quote
                    I suspect it's not finding anything, as it's looking for a file with 2 dots

                    You wrote:

                    Quote
                    And i know that the file is there, i'm looking at it right now

                    Go on, take a giant step for mankind and tell us what it is called!



                    Apexi

                      Topic Starter


                      Rookie

                      Re: Search file and edit it
                      « Reply #18 on: March 29, 2011, 12:33:49 PM »
                      You didn't answer this question:

                      and did you read this?

                      You wrote:

                      Go on, take a giant step for mankind and tell us what it is called!

                      Forget the other wildcard. Just me who made a misstake. Dis'nt matter iaf i remove the second wildcard. Same as before. Also, if i just type *text.vdf*.* in cmd it will find the file :P

                      Salmon Trout

                      • Guest
                      Re: Search file and edit it
                      « Reply #19 on: March 29, 2011, 12:37:22 PM »
                      Why aren't you telling us the name of the file you were "looking at"? Are you playing a game with us? If you want to get help here, it is good manners to answer the questions that are asked by the people who are trying to help you, and who, for the time being, are still inclined to treat you as a genuine questioner and not anything else.

                      By the way, in your original post, you mentioned a file called "test.vdf", with an 's', but all your search wildcards seem to be looking for a file with "text", with an 'x', in the name. Perhaps you could talk us through what this means?


                      Apexi

                        Topic Starter


                        Rookie

                        Re: Search file and edit it
                        « Reply #20 on: March 29, 2011, 12:46:37 PM »
                        Well i dis'nt seemes like you want to help. What more do you need to know, it is a vdf file named test. Anyhow, then i have to look for another forum.

                        Salmon Trout

                        • Guest
                        Re: Search file and edit it
                        « Reply #21 on: March 29, 2011, 12:48:48 PM »
                        it is a vdf file named test.

                        So why are you looking for a file called text*.vdf?


                        Apexi

                          Topic Starter


                          Rookie

                          Re: Search file and edit it
                          « Reply #22 on: March 29, 2011, 12:50:55 PM »
                          So why are you looking for a file called text*.vdf?

                          Because i want to edit it.

                          Salmon Trout

                          • Guest
                          Re: Search file and edit it
                          « Reply #23 on: March 29, 2011, 12:56:10 PM »
                          Because i want to edit it.

                          Are you serious? If the file is called test.vdf, and your search batch script is looking for text.vdf, then your batch script is never going to find it. Don't you understand that? It's like calling out "Jim!" when there is nobody called Jim in the room, and you actually wanted John to answer anyway. A waste of time. (Like this thread)



                          Apexi

                            Topic Starter


                            Rookie

                            Re: Search file and edit it
                            « Reply #24 on: March 29, 2011, 01:03:32 PM »
                            It never accord to you that it could be a misspelling? Or it doesnt matter what the file is named. Or maybe that i could change the script later on. Do me a favour. If you cant help, dont say any. Instead of being a jerk with no helpfull answer.

                            Allan

                            • Moderator

                            • Mastermind
                            • Thanked: 1260
                            • Experience: Guru
                            • OS: Windows 10
                            Re: Search file and edit it
                            « Reply #25 on: March 29, 2011, 01:12:27 PM »
                            Okay guys, let's knock it off.

                            ST - you always have the option of not posting in this thread anymore - your choice

                            apexi - I can't follow what you want to do either - you've been less than clear and consistent in this thread. Also, let's be overtly rude to anyone, okay?

                            Salmon Trout

                            • Guest
                            Re: Search file and edit it
                            « Reply #26 on: March 29, 2011, 01:35:57 PM »
                            Deep in a sub folder on my S drive is a file called test.vdf. This script finds it and shows its name and folder and adds 2 lines of text to it.

                            test.vdf (located in folder S:\Oldfolders\T-Drive\upix\sub1\sub2\sub3\sub4)

                            Code: [Select]
                            ; this is test.vdf
                            ; it has 2 lines

                            The batch script:

                            Code: [Select]
                            @echo off
                            for /f "delims==" %%A in ('dir /b /s S:\*.vdf') do (
                                echo file name:   %%~nxA
                                echo file folder: %%~dpnxA
                                echo Before:
                                type "%%~dpnxA"
                                echo ; extra line 1 >> %%~dpnxA
                                echo ; extra line 2 >> %%~dpnxA
                                echo After:
                                type "%%~dpnxA"
                                )

                            The console output:
                            Code: [Select]
                            S:\>findvdf.bat
                            file name:   Test.vdf
                            file folder: S:\Oldfolders\T-Drive\upix\sub1\sub2\sub3\sub4\Test.vdf
                            Before:
                            ; this is test.vdf
                            ; it has 2 lines
                            After:
                            ; this is test.vdf
                            ; it has 2 lines
                            ; extra line 1
                            ; extra line 2

                            Salmon Trout

                            • Guest
                            Re: Search file and edit it
                            « Reply #27 on: March 29, 2011, 03:51:17 PM »
                            [Edit]

                            Code: [Select]
                            @echo off
                            for /f "delims==" %%A in ('dir /b /s S:\*.vdf') do (
                                echo file name:   %%~nxA
                                echo file folder: %%~dpA
                                echo Before:
                                type "%%~dpnxA"
                                echo ; extra line 1 >> %%~dpnxA
                                echo ; extra line 2 >> %%~dpnxA
                                echo After:
                                type "%%~dpnxA"
                                )   

                            Code: [Select]
                            S:\>findvdf.bat
                            file name:   Test.vdf
                            file folder: S:\Oldfolders\T-Drive\upix\sub1\sub2\sub3\sub4
                            Before:
                            ; this is test.vdf
                            ; it has 2 lines
                            After:
                            ; this is test.vdf
                            ; it has 2 lines
                            ; extra line 1
                            ; extra line 2

                            BC_Programmer


                              Mastermind
                            • Typing is no substitute for thinking.
                            • Thanked: 1140
                              • Yes
                              • Yes
                              • BC-Programming.com
                            • Certifications: List
                            • Computer: Specs
                            • Experience: Beginner
                            • OS: Windows 11
                            Re: Search file and edit it
                            « Reply #28 on: March 29, 2011, 06:15:39 PM »
                            It never accord to you that it could be a misspelling?
                            Which one was the misspelled one? test or text?
                            I was trying to dereference Null Pointers before it was cool.

                            Apexi

                              Topic Starter


                              Rookie

                              Re: Search file and edit it
                              « Reply #29 on: March 31, 2011, 11:51:22 AM »
                              Everything is solved. After some conversation with Salmon Trout and a big pile of help the script works properly.

                              Salmon Trout

                              • Guest
                              Re: Search file and edit it
                              « Reply #30 on: March 31, 2011, 11:59:29 AM »
                              1. Test.vdf (before)

                              Code: [Select]
                              {
                              {
                              Section 1
                              }
                              {
                              Section 2
                              }
                              {
                              Section 3
                              }
                              {
                              Section 4
                              }
                              }

                              2. Script

                              Code: [Select]
                              @echo off
                              setlocal enabledelayedexpansion
                              for /f "delims==" %%A in ('dir /b /s S:\*.vdf') do (
                                      set filepath=%%~dpA
                                      set filename=%%~nA
                                      set fileExtn=%%~xA
                              )
                              Echo Found file
                              Echo Path    %filepath%
                              Echo Name    %filename%%fileExtn%
                              cd /d "%filepath%"
                              set linecount=0
                              for /f "delims=" %%A in ('type "%filename%%fileExtn%"') do set /a linecount+=1
                              if exist "%filename%-new%fileExtn%" del "%filename%-new%fileExtn%"
                              set linenumber=0
                              for /f "delims=" %%A in ('type "%filename%%fileExtn%"') do (
                              set /a linenumber+=1
                              if !linenumber! equ %linecount% (

                              echo { >> "%filename%-new%fileExtn%"
                              echo                 Section 5 >> "%filename%-new%fileExtn%"
                              echo } >> "%filename%-new%fileExtn%"
                              echo } >> "%filename%-new%fileExtn%"

                              ) else (
                              echo %%A >> "%filename%-new%fileExtn%"
                              )
                              )
                              echo Rename
                              echo ren "%filename%%fileExtn%" "%filename%-old%fileExtn%"
                              ren "%filename%%fileExtn%" "%filename%-old%fileExtn%"
                              echo ren "%filename%-new%fileExtn%" "%filename%%fileExtn%"
                              ren "%filename%-new%fileExtn%" "%filename%%fileExtn%"
                              echo done

                              1. Test.vdf (after)

                              Code: [Select]


                              Section 1 


                              Section 2 


                              Section 3 


                              Section 4 

                              {
                                    Section 5
                              }
                              }