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

Author Topic: [Batch script] How to locate an .ini file with a name which starts with "nsc"  (Read 8376 times)

0 Members and 1 Guest are viewing this topic.

mangolzy

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7

    My script:

    Code: [Select]
    @echo off
    for %%f in ('dir /s /b H:\nsc*') do (
         set file=%%f
         set type=%file:~-3%
         if "%type%"=="ini"  echo good&%file% >> H:\allfinding.txt
    )

    WHY the cut string doesn't work??

    as in the attachment implies, the code works seperately but not in a file batch?

    can somebody tell me why???

    or any solution for finding the path of a file called "nsc*.ini" in C:\, behind several folder perhaps,

    thank you!!!


    [recovering disk space, attachment deleted by admin]

    Salmon Trout

    • Guest
    If you want to set and use variables inside a bracket structure you need delayed expansion and use exclamation marks (!) instead of percent signs (%) when you expand them.

    1. Study the parts in red

    @echo off
    setlocal enabledelayedxpansion
    for %%f in ('dir /s /b H:\nsc*') do (
         set file=%%f
         set type=!file:~-3!

         if "!type!"=="ini"  echo good&!file! >> H:\allfinding.txt
    )


    2. THIS IS IMPORTANT: what is the ampersand (&) doing here? Please explain what you think it will do.

    Quote
    good&%file%

    Did you mean good%file% .... ? I assume in the next example you want to concatenate the string good and the file name variable.

    3. You don't really need the intermediate variables (file and type). You can get the file extension directly like this

    if %%f is a filename with extension, %%~xf is just the extension (with dot). Type FOR /? at the prompt to find out about the ~ variable modifiers.

    @echo off
    for %%f in ('dir /s /b H:\nsc*') do (
         if "%%~xf"==".ini"  echo good%%f >> H:\allfinding.txt
    )


    In fact it will all go on one line

    @echo off
    for %%f in ('dir /s /b H:\nsc*') do if "%%~xf"==".ini" echo good%%f >> H:\allfinding.txt


    Finally, the script as it stands will ignore files whose extension is .INI or InI or .Ini or .iNi unless you make the IF test case insenstive with the /i switch.

    for %%f in ('dir /s /b H:\nsc*') do if /i "%%~xf"==".ini" echo good%%f >> H:\allfinding.txt

    Anyhow why don't you just do this?

    dir /s /b H:\nsc*.ini >> H:\allfinding.txt

    « Last Edit: July 09, 2014, 01:29:11 PM by Salmon Trout »

    shiverbob



      Beginner

      Thanked: 1
      • Yes
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows XP
    Wow...that's what is said in a 2hour lecture  :)
    Pff computers are easy, math is hard.

    Salmon Trout

    • Guest
    Wow...that's what is said in a 2hour lecture  :)

    No, a 1 minute brief explanation.

    Salmon Trout

    • Guest
    or any solution for finding the path of a file called "nsc*.ini" in C:\

    If the file is in C:\ or a folder under it, why does your script look in H:\ .... ?

    mangolzy

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Very glad with this reply so effective,

      i am really a new hand, i try this to avoid some repetitive work.

      1. once a variable is used twice, it should be !XXX!   ?

      2. oh  that just my imagination of combine the string &...so it will be printed directly in fact

      3. thank you for the improvement, but i have to find out all the file qualified, and make some modification in them, so it will be a simple batch... i think...


      i suddenly found out a fact that you are the one who help me with the replacement of configuration file ...


      u r really an expert!!

      so what i am doing now is to find out the path of the file i am going to modify,

      Code: [Select]
      @echo off
      setlocal Enabledelayedexpansion
      set name=10.23.96.46
      for /f "tokens=1* delims=:" %%a in ( ' findstr /n /r "^" "nsc.ini" ' ) do (
         set var=%%b
         set var=!var:10.23.96.98=%name%!
         echo !var!>>newnsc.ini
      )
      endlocal

      of cause, it does the change as planned...yeah,, it still has the problem of blank line, it will put out 10.23.96.98=10.23.96.46 when come across a blank line, is it possible to improve this??

      thank u very much!!

      btw, how can you be so familiar with all these?

      Lemonilla



        Apprentice

      • "Too sweet"
      • Thanked: 70
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      btw, how can you be so familiar with all these?
      Practice makes perfect.
      Wow...that's what is said in a 2hour lecture  :)
      I hope not, or I'll be really bored in class.
      Quote from: patio
      God Bless the DOS Helpers...
      Quote
      If it compiles, send the files.

      mangolzy

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 7
        Hello,

        I still have a problem, with this command line,

        Code: [Select]
        for %f in (dir /s /b H:\nsc*) do if /i "%~xf"==".ini" echo good%f >> H:\allfinding.txt

        why it ignore those files in  a subfolder??

        I mean with dir /s /b H:\nsc* itself, we have

        Code: [Select]
        H:\nscini1019551.docx
        H:\nsc.ini
        H:\nscini101951151.docx
        H:\NSClient++ Reference Manual.pdf
        H:\nsclient.ini
        H:\nscnew.ini
        H:\nsc.vbs
        H:\nsc.exe
        H:\nsclient.bat
        H:\nscre.vbs
        H:\nscre.exe
        H:\nsclientpp.vbs
        H:\nsclientpp.exe
        H:\NSClient.txt
        H:\NSC
        H:\nscpplist.txt
        H:\nsc.log
        H:\nscfind.txt
        H:\Script\nsc.ini
        H:\Script\nsclient.ini
        H:\Script\nsc.vbs
        H:\Script\nsc.txt
        H:\NSC\nsclient.ini
        H:\NSC\nscnew.ini
        H:\NSC\nsc.ini
        H:\success_server_tse\nscpplist.txt
        H:\success_server_tse\nsclient.bat


        but with the for sentence, i got only
        Code: [Select]
        goodH:\nsc.ini
        goodH:\nsclient.ini
        goodH:\nscnew.ini

         ???

        I know it's a simple question ; but i have no experience at all..................

        Thank you all

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Try this:

        Code: [Select]
        dir /s /b /a-d h:\nsc* |findstr /i "\.ini$" >h:\nsc-ini-files.txt
        and if you only want .ini files then this will work as well:

        Code: [Select]
        dir /s /b /a-d h:\nsc*.ini >h:\nsc-ini-files.txt

        mangolzy

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 7
          Try this:

          Code: [Select]
          dir /s /b /a-d h:\nsc* |findstr /i "\.ini$" >h:\nsc-ini-files.txt
          and if you only want .ini files then this will work as well:

          Code: [Select]
          dir /s /b /a-d h:\nsc*.ini >h:\nsc-ini-files.txt

          Thank you for your reply first, but it doesn't match to my problem.

          YES, they help to find out all the path I want to know,

          but the problem lies in the for syntax, once i apply the dir.................... sentence in a for sentence, it returns only the files in H:\ BUT NOT IN H:\NSC by exemple.

          WHY i want this effect?? because i want to know all these paths and then make modification to these files, their paths should be in a for loop~~

          or perhaps there are other solutions

           ::)

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Code: [Select]
          @echo off
          for /f "delims=" %%a in (' dir /s /b /a-d h:\nsc*.ini ') do (
             echo "%%a"
          )
          pause

          it returns only the files in H:\ BUT NOT IN H:\NSC by exemple.

          What does this mean?  Did you mention H:\NSC in any of your posts?  Is this a folder?  Please explain.

          The code above will show any file that starts with NSC and ends in .INI

          mangolzy

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 7
            Code: [Select]
            @echo off
            for /f "delims=" %%a in (' dir /s /b /a-d h:\nsc*.ini ') do (
               echo "%%a"
            )
            pause

            What does this mean?  Did you mention H:\NSC in any of your posts?  Is this a folder?  Please explain.

            The code above will show any file that starts with NSC and ends in .INI

            thank you foxidrive!!

            it works like this:

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set newip=10.23.96.98
            for /f "delims=" %%f in ('dir /s /b /a-d H:\nsc*.ini') do (
                    echo %%f
            pause
            for /f "tokens=1* delims=:" %%a in (' findstr /n /r "^" "%%f" ') do (
            set var=%%b
            set var=!var:10.23.96.46=%newip%!
            echo !var!>>sucess.txt
                    )
            echo *********************finish one*******************>>sucess.txt
            echo good%%f>> H:\ok.txt
            )
            endlocal

            i have the modification done in all the files concerned,

            the only one problem that left is, they don't treat the blank line correctly as i have said before~~

            can you help me with this delicate problem too?? :)

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            It is a real problem in batch scripting when people don't describe what they are trying to do.

            Run this and see if it is what you want to do, because I am guessing.

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set newip=10.23.96.98
            for /f "delims=" %%f in ('dir /s /b /a-d H:\nsc*.ini') do (
            set "var=%%~nxf"
            set "var=!var:10.23.96.46=%newip%!"
            >>success.txt echo %%~dpf!var!
            )
            endlocal
            pause

            mangolzy

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 7
              It is a real problem in batch scripting when people don't describe what they are trying to do.

              Run this and see if it is what you want to do, because I am guessing.

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              set newip=10.23.96.98
              for /f "delims=" %%f in ('dir /s /b /a-d H:\nsc*.ini') do (
              set "var=%%~nxf"
              set "var=!var:10.23.96.46=%newip%!"
              >>success.txt echo %%~dpf!var!
              )
              endlocal
              pause

              Let me make my problem clear:

              I want to search in the whole system all the file begins with nsc and type of .ini;

              once we found one path of this kind of file, we replace all the 10.23.96.46 with 10.23.96.98 in the file.

              that's all the functionality that i want,

              up to now with your help. there is no problem in locating all the files qualified,

              1.   but still a little problem in the treatment of file, that's to say in the replacement when i try to read the file line by line, then when it comes to a blank line, the code i provided yesterday, will fill in the blank line with  "10.23.96.46=10.23.96.98"

              2.   do you know how to get the folder where a file is in with a completed path of this file??

                    that means:  for a file   H:\NSC\Test\nsc.ini, with the string "H:\NSC\Test\nsc.ini"  How can we return a string like this    "H:\NSC\Test\"??

              Thank you again!

              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              There were 14 posts in this thread before you told us what you wanted.


              patio

              • Moderator


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
              " Anyone who goes to a psychiatrist should have his head examined. "

              mangolzy

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 7
                There were 14 posts in this thread before you told us what you wanted.

                Sorry for the misleading,

                but as a newhand, i try to break my problem down, and solve them one by one, until i suddenly found out the first one who answered this question actually helped me before, but his solution still has a little problem....

                i don't know, perhaps this problem seems easy for you, but as i haven't ever learned about the batch, i have no idea except following other's code......

                so do you think it's possible to solve the problem of blank line and the return of parted path?

                Voila

                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                i don't know, perhaps this problem seems easy for you, but as i haven't ever learned about the batch, i have no idea except following other's code......

                You have all the people who monitor the topics here and who are willing to help for free, if you only tell them what you are doing and how you want the task done.

                Those who have offered suggestions already have just wasted their time - because you did not describe the actual task in the very first post.
                We like coding, we enjoy writing scripts to solve a task - we hate being frustrated because people don't give us the correct information to do so.



                mangolzy

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Windows 7
                  You have all the people who monitor the topics here and who are willing to help for free, if you only tell them what you are doing and how you want the task done.

                  Those who have offered suggestions already have just wasted their time - because you did not describe the actual task in the very first post.
                  We like coding, we enjoy writing scripts to solve a task - we hate being frustrated because people don't give us the correct information to do so.

                  Sincerely excuse me.

                  But it will be more efficient if I haved stated the problem I want to solve clearer, I thought I have open a thread for it otherwhere but not many reply...................

                  On the other hand, I don't think all these replies are wasting time, because they allow me to learn something about batch, there are so many parameters or options and I don't really get a manuel ....................

                  Thank you all the same!

                  mangolzy

                    Topic Starter


                    Rookie

                    • Experience: Beginner
                    • OS: Windows 7
                    so this is the link to my original problem:

                    http://www.computerhope.com/forum/index.php/topic,145039.msg910673.html#msg910673

                    no one reply, i don't know if it's because of my bad explaination or it's complicate or perhaps people don't want to give all the code to such a problem freely, that means i got the answer so easily...............


                    BUT ANYWAY, I thought this forum is one of the most active forums that I have looked for help.

                    I really beg your pardon, for those pass their time reading all these........... :'(

                    Lemonilla



                      Apprentice

                    • "Too sweet"
                    • Thanked: 70
                    • Computer: Specs
                    • Experience: Experienced
                    • OS: Windows 7
                    Let me make my problem clear:

                    I want to search in the whole system all the file begins with nsc and type of .ini;

                    once we found one path of this kind of file, we replace all the 10.23.96.46 with 10.23.96.98 in the file.

                    that's all the functionality that i want,

                    up to now with your help. there is no problem in locating all the files qualified,

                    1.   but still a little problem in the treatment of file, that's to say in the replacement when i try to read the file line by line, then when it comes to a blank line, the code i provided yesterday, will fill in the blank line with  "10.23.96.46=10.23.96.98"

                    2.   do you know how to get the folder where a file is in with a completed path of this file??

                          that means:  for a file   H:\NSC\Test\nsc.ini, with the string "H:\NSC\Test\nsc.ini"  How can we return a string like this    "H:\NSC\Test\"??

                    Thank you again!

                    If you are working in a for loop, you use this to find the path:

                    %%~pdA
                    or it's %~pdA, can't remember if you still have to escape.

                    assuming you used %%A as your variable
                    Quote from: patio
                    God Bless the DOS Helpers...
                    Quote
                    If it compiles, send the files.

                    foxidrive



                      Specialist
                    • Thanked: 268
                    • Experience: Experienced
                    • OS: Windows 8
                    so this is the link to my original problem:

                    http://www.computerhope.com/forum/index.php/topic,145039.msg910673.html#msg910673

                    no one reply, i don't know if it's because of my bad explaination or it's complicate or perhaps people don't want to give all the code to such a problem freely

                    Your explanation is not clear, it has no details but uses XXX.EXE and XXX.INI plus there is the language difficulty.
                    We don't know how you have to log in to the remote machines, which versions of Windows are being used, what program you will use to restart the service, which folder tree the files are in.

                    For your original post, I didn't reply because there are so few details and the task is very unclear.

                    foxidrive



                      Specialist
                    • Thanked: 268
                    • Experience: Experienced
                    • OS: Windows 8
                    Perhaps this thread should be locked - here is a follow up to your original post and which has a link to this thread:

                    http://www.computerhope.com/forum/index.php?topic=145039.msg911889#msg911889