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

Author Topic: search for mutiple txt files and copy file name and path into one txt file  (Read 4541 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    How can I search for multiple  txt files with the partly same name under multiple subfolders and copy that filename and path to that file into one txt file.
    like
    d:\users\mike\log\logfileL22102017.txt
    d:\users\john\log\logfileH12022017.txt
    d:\users\claire\log\logfileH03042017.txt
    d:\users\billy\log\logfileL10112017.txt

    all that files logfile*.txt in new file copied_logfiles.txt
    copied_logfiles.txt will be in folder d:\users\copied_logfiles.txt also batch which will create that file.

    copied_logfiles.txt will look like this when I open.
    d:\users\mike\log\--->L.txt
    d:\users\john\log\--->H.txt
    d:\users\claire\log\--->H.txt
    d:\users\billy\log\--->L.txt


    I need a list of files in one txt file if users logfile is marked with H or L so I need to check users folder and which txt file is currently in L or H.
    Now I need to check every user subfolder and check if there is logfileL22102017.txt  with L in it or logfileH03042017.txt with H in it.
    If it is more than 5 hours L than I need to do something to get for user to have  logfileH03042017.txt with H in it.

    Can someone help me with batch?

    Computer Hope Admin

    • Administrator


    • Prodigy

      Thanked: 248
      • Yes
      • Yes
      • Yes
      • Computer Hope
    • Certifications: List
    • Computer: Specs
    • Experience: Guru
    • OS: Windows 10
    You can use wildcards in a directory command.

    For example, lets say you are in the d:\users directory and wanted to find all files beginning with logfileh that were text files. You could type the following command.

    dir logfileh*.txt /s

    The "/s" will display results for all subdirectories from the d:\users directory.

    If you wanted the results to go to a text file you could use the append symbol to append the results to a text file. Below is an example of the same command.

    dir logfileh*.txt /s > c:\users\copied_logfiles.txt

    Like any command line command this command can be added to any batch file.
    Everybody is a genius. But, if you judge a fish by its ability to climb a tree, it will spend its whole life believing that it is stupid.
    -Albert Einstein

    Hackoo



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    Hi  ;)
    There is another command named  WHERE  : To locate and display files in a directory tree
    Quote
    WHERE (Windows 2003 + )

    Locate and display files in a directory tree.

    The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.

    Syntax
          WHERE [/r Dir] [/q] [/f] [/t] Pattern ...

          WHERE [/q] [/f] [/t] [$ENV:Pattern

    key
       /r      A recursive search, starting with the specified Dir directory.

       /q      Don’t display the files but return either an exit code of 0 for success
               or 1 for failure.

       /f      Display the output file name in quotation marks.

       /t      Display the size, time stamp, and date stamp of the file.

      pattern  The Drive\Directory\file, or set of files to be found.
               you can use wildcard characters ( ? * ) and UNC paths.

       ENV     Path to search where ENV is an existing environment variable containing one or more paths.

    By default, WHERE searches the current directory and the paths specified in the PATH environment variable. Optional search paths (in pattern) should not be used in conjunction with /r Dir.

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      I know but I don't know how to put it in batch for my case

      Blisk

        Topic Starter


        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        I have managed to make a list with paths but cant make it to write at the end of path if file is logH.txt or logL.txt
        Code: [Select]
        for /f  "tokens=*" %a in ('dir .\log /B /S) do echo %~fa %~za >> d:\users\drivers\mike\testfolder\List.txtthis one says can not find file
        Code: [Select]
        for /f  "tokens=*" %a in ('dir .\new /s /b /A-D "*.txt"') do echo %~fa %~za >> d:\users\drivers\mike\testfolder\List.txt

        Hackoo



          Hopeful
        • Thanked: 42
        • Experience: Expert
        • OS: Windows 10
        Hi  ;)
        You can give a try for this batch file : Search_LogFiles.bat
        Code: [Select]
        @echo off
        Title Searching files . . .
        Color 9E & Mode con cols=75 lines=3
        set "Master_Folder=C:\users"
        set "Pattern=logfile*.txt"
        set "LogSearch=%~dpn0.txt"
        echo(
        echo      Please wait ... Searching "%Pattern%" files is in progress ...
        Where /R "%Master_Folder%" "%Pattern%" /F >"%LogSearch%" 2>&1
        start "" "%LogSearch%"

        Blisk

          Topic Starter


          Intermediate

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          I have one problem here.
          loghi*.txt files are also in folder logfiles and in its subfolder new it is only one.
          And I need only file from subfolder new, this script finds also fles in folder logfiles.

          d:\users\john\logfiles\logfileH12022017.txt

          this file I am looking for to write in txt
          d:\users\john\logfiles\new\logfileH12022017.txt


          Hackoo



            Hopeful
          • Thanked: 42
          • Experience: Expert
          • OS: Windows 10
          I have one problem here.
          loghi*.txt files are also in folder logfiles and in its subfolder new it is only one.
          And I need only file from subfolder new, this script finds also fles in folder logfiles.

          d:\users\john\logfiles\logfileH12022017.txt

          this file I am looking for to write in txt
          d:\users\john\logfiles\new\logfileH12022017.txt

          In this case you can try like that if i understood what you mean ?
          Code: [Select]
          @echo off
          Title Searching files . . .
          Color 9E & Mode con cols=75 lines=3
          set "Master_Folder=C:\users"
          set "Pattern=logfile*.txt"
          set "LogSearch=%~dpn0.txt"
          echo(
          echo        Please wait ... Searching "%Pattern%" files is in progress ...
          Where /R "%Master_Folder%" "%Pattern%" /F >"%LogSearch%" 2>&1
          Type "%LogSearch%" | find /I "new" > NewLog.txt
          start "" NewLog.txt

          Blisk

            Topic Starter


            Intermediate

            Thanked: 1
            • Experience: Familiar
            • OS: Windows 7
            I named that batch test.bat and it also creates test.txt where all logfile*.txt are in, from \logfiles folder too which is wrong.
            But at the same time it opens notepad named NewLog.txt where is result which I want, from folder \new

            How to do this to get results saved in NewLog.txt and there is no test.txt created and doesn't open notepad.exe but save directly to NewLog.txt file.

            And is it possible to get result in txt file from this logfileH12022017.txt to this logfileH?

            Hot

            Hackoo



              Hopeful
            • Thanked: 42
            • Experience: Expert
            • OS: Windows 10
            I named that batch test.bat and it also creates test.txt where all logfile*.txt are in, from \logfiles folder too which is wrong.
            But at the same time it opens notepad named NewLog.txt where is result which I want, from folder \new

            How to do this to get results saved in NewLog.txt and there is no test.txt created and doesn't open notepad.exe but save directly to NewLog.txt file.

            And is it possible to get result in txt file from this logfileH12022017.txt to this logfileH?

            Hot
            Did you mean something like that ?
            Code: [Select]
            @echo off
            Title Searching files . . .
            Color 9E & Mode con cols=75 lines=3
            set "Master_Folder=C:\users"
            set "Pattern=logfile*.txt"
            Rem Name it as you want
            set "LogSearch=TextFile_As_You_want_here.txt"
            echo(
            echo        Please wait ... Searching "%Pattern%" files is in progress ...
            Where /R "%Master_Folder%" "%Pattern%" /F >"%LogSearch%" 2>&1
            REM We filter our search into file named NewLog.txt
            Type "%LogSearch%" | find /I "new" > NewLog.txt
            Rem We delete the "%LogSearch%"
            If exist "%LogSearch%" Del "%LogSearch%"

            Blisk

              Topic Starter


              Intermediate

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Thank you this is what I want.