Announcement

Collapse
No announcement yet.

DOS Batch file for searching a specific file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • DOS Batch file for searching a specific file

    Below is the dos script which I have write to search files in specific folder:

    @echo off
    c:

    if exist c:\Data\*.txt (echo FILE_EXIST) else (echo FILE DOES NOT EXIST)
    PAUSE

    Now I am getting message FILE DOES NOT EXIST all the time even if the file is there.

    if i change
    if exist c:\Data\*.* (echo FILE_EXIST) else (echo FILE DOES NOT EXIST)

    i*.* instead of *.txt then I get FILE_EXIST message all the time even if the file is not there.

    I am not getting correct message. Any help?
    Thanks in advance.

  • #2
    Re: DOS Batch file for searching a specific file

    Hi Pmg:

    I use biterscripting (http: //www .biterscripting .com for free download) instead of DOS. You may want to try it too. Here are a couple of suggestions to get you started.

    The following command in biterscripting will list all files with .txt extension in directory C:/Data .

    find -n "C:/Data/*.txt"

    If you want to recursively search subdirectories, use the -r flag, as follows.

    find -r -n "C:/Data/*.txt"

    For more control, use something like the following.

    find -r -n "*.txt" "C:/Data" ($fctime > "20090226")

    The above will list all files with extension .txt in directory C:/Data (and subdirectories) that were created after Feb 26, 2009.

    To do something with each file in the list of files, use something like the following:

    var str file, list
    find -r -n "*.txt" "C:/Data" ($fctime > "20090226") > $list
    while ($list <> "")
    do
    lex "1" $list > $file
    cat $file
    done

    The above just types the contents of each file to screen, using the cat command. But you can do other things with each file in the while-do-done loop.

    Hope this helps.

    Patrick Mc
    Last edited by Rems; 29th June 2010, 18:17. Reason: live link removed by mod

    Comment


    • #3
      Re: DOS Batch file for searching a specific file

      Well, the command you suggested works in my Vista system.

      c:\>if exist psh\*.ps1 (echo FILE_EXIST) else (echo FILE DOES NOT EXIST)
      FILE_EXIST

      So I'd guess you have made a typo or the directory is not accessible. Try taking a dir list of the directory contents and see if dir fails too.

      FWIW, I have no knowledge about the biterScripting tool. The web page didn't have much info like online manual or the like. While biterScripting might have its uses, I'd advise to stick with more a mainstream tools like VBScript and Powershell. Way more support available for them.

      -vP

      Comment


      • #4
        Re: DOS Batch file for searching a specific file

        You can change the script a little and achieve a similar result.

        Code:
        if exist dumps.txt goto exists
        echo file.not.found
        do something
        pause
        goto continue
        
        :exists
        echo file.found
        pause
        goto continue
        
        :continue
        echo continue script
        Last edited by ahinson; 26th February 2009, 21:57.
        Andrew

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        Comment


        • #5
          Re: DOS Batch file for searching a specific file

          Just my 2 cts.

          However Wildcards are not disallowed for "If exist" statements you should be aware that the result when using a wildcard in DOS commands might not always turn out like expected.

          e.g.
          - create a test folder c:\test
          - create a test file in the folder with the extension .txtzzz.
          - Make sure no files with the extension .txt exists in that folder.
          - Now when you run this:
          If EXIST "c:\test\*.txt" (echo FILE_EXIST) else (echo FILE DOES NOT EXIST)
          The result will rather surprizingly be: "FILE_EXIST"!

          Same you can test with other DOS commands like Dir and Del:
          Dir /b "c:\test\*.txt"
          Del "c:\test\*.txt"
          (It didn't asked for confirmation for the deletion of each file because of the fact a file extention was entered here)
          oeps! it deleted also the files from the folder with the extension .txtzzz

          This happens when a wildcard is used instead of the name of a file while a file extension did was entered.
          In this case the pattern of the file extension is assumed to be not more than 3 characters long.
          For the first three characters an expected 'text compare' is used (the extension .tx does not match .txt), for the 4th and other characters that could exist an additional 'start_with compare' is used on these characters (therefore the entered extension .txt will also maches files that have the extension .txtzzz).
          Luckily there aren't much filetypes with a file extension longer than three characters and which is containing the first three characters that is associated with an other filetype.

          Anyways , that does still not explain why your first sample returns "FILE DOES NOT EXIST".
          Could it perhaps be that in your actual batch c: is infact a driveletter one that is mapped to a network path - and, what you showed was also not the exact code from the batch?

          About the results you have with the second batch sample:
          The condition when using If Exist "c:\test\*" to check whether files exist in a folder is always TRUE when the folder exist, even when no files were located in that folder.
          Because the 'if exist *.*' does also hit on a device file (NUL) then when examining the target folder (this does not occour on every kind of volume format though).

          To check the existence of files (or specific file types) in a folder or tree you could use for example something like:

          Code:
          @echo off & Setlocal
          
          :: Search files just in the Folder
          Set "sFolder=c:\test"
          Set "sFileType=txtzzz"
          PushD "%sFolder%" &&(
            FOR %%i IN (*) DO (
              If /i "%%~xi"==".%sFileType%" (
                Set TypeEXIST=TRUE & GoTo:EndLoop)
            )
          )
          :EndLoop
          PopD
          
          If Defined TypeEXIST (echo FILE EXIST) else (
             echo FILE DOES NOT EXIST)
          
          pause
          =Or=
          Code:
          @echo off & Setlocal
          
          :: Search for files in the Tree
          Set "sFolder=c:\test"
          Set "sFileType=txtzzz"
          PushD "%sFolder%" &&(
            FOR /R "." %%i IN (*) DO (
              If /i "%%~xi"==".%sFileType%" (
                Set TypeEXIST=TRUE & GoTo:EndLoop)
            )
          )
          :EndLoop
          PopD
          
          If Defined TypeEXIST (echo FILE EXIST) else (
             echo FILE DOES NOT EXIST)
          
          pause
          \Rems
          Last edited by Rems; 28th February 2009, 21:55.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Can someone help me with this script please.

            This script is exactly what I need, but little more of it.
            I need script to search for configure.cfg under C:\Users\mike\AppData\Local\Trd
            And if there script not finding configure.cfg than it must copy it from
            c:\script\configure.cfg folder to folder C:\Users\mike\AppData\Local\Trd

            Problem is because copying will be for differend users in diferred subfolder tree.
            Script will be runned before user login.
            So script must scan for folder Trd if there is that folder than check if configure.cfg exist
            than skip, if configure.cfg not exist than copy configure.cfg from c:\script to C:\Users\mike\AppData\Local\Trd

            Comment

            Working...
            X