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

Author Topic: Need command to find a file in subfolders and then move the result.  (Read 4548 times)

0 Members and 1 Guest are viewing this topic.

LevyRM

    Topic Starter


    Starter

    • Experience: Familiar
    • OS: Windows 7
    Hello New here -
    I am in need of a command line to allow me to search for file by extension and after finding it in about 3+ folders deep need to move it to a different folder.

    ==================
    File location: c:\test\temp\variable
    **variable means, the folder can change name**
    New Location: c:\new_location
    ==================

    I can find the file I need to move via dir help.txt /s and it finds it for example: c:\test\temp\variable
    Would also need to narrow down folders to look into. (Some how specify to search within child folders for c:\test\temp directory)
    but, now I need to move the result to c:\new_location
    How can this be accomplished?

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Need command to find a file in subfolders and then move the result.
    « Reply #1 on: February 27, 2014, 07:36:15 PM »
    Try this:
    for /f "delims=" %%A in ('dir test.txt /b /s') do echo copy "%%A" "%new_Loc%"

    The command will loop through the output of 'dir test.txt /b /s' line by line, (with no breaks in the line).  With each line it will temporarily store it in the variable %%A and execute all the commands after "do".  In this case, it will loop through the list that 'dir test.txt /b /s' creates copying each file location to %new_Loc% (your destination).  Be careful though, it might over ride files if there are multiple copies of test.txt in your drive.
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Need command to find a file in subfolders and then move the result.
    « Reply #2 on: February 27, 2014, 08:42:41 PM »
    I ... need ... to search for file by extension and after finding it in about 3+ folders deep need to move it to a different folder.

    about 3+ folders deep doesn't give us a good idea of the actual task.

    If Lemonilla's code isn't what you need then describe the exact task, giving examples of the extensions needed and the folder structure.

    LevyRM

      Topic Starter


      Starter

      • Experience: Familiar
      • OS: Windows 7
      Re: Need command to find a file in subfolders and then move the result.
      « Reply #3 on: February 28, 2014, 05:12:28 PM »
      I am getting a %%A was unexpected at this time. error message when executing:
      for /f "delims=" %%A in ('dir test.txt /b /s') do echo copy "%%A" "%c:\new_Location%"

      Answer to foxidrive:
      about 3+ folders deep = the file I may be looking for may reside in 3 or more folders deep on my path.
      path = c:\test\temp\variable\
      At this time the example has a test.txt file located on (variable) - folder. (3 folders deep on the path)
      The name of the "variable" folder may vary each time I run this command.
      Screen shot below:

      [recovering disk space, attachment deleted by admin]

      Lemonilla



        Apprentice

      • "Too sweet"
      • Thanked: 70
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Need command to find a file in subfolders and then move the result.
      « Reply #4 on: February 28, 2014, 07:54:27 PM »
      You only double the % sign if you are working within a batch file (because % is the escape character for % /mind=blown).  And it doesn't appear you set %C:\new_location% to any value.  (Hint: anything surrounded in % signs is a 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
      Re: Need command to find a file in subfolders and then move the result.
      « Reply #5 on: February 28, 2014, 10:25:46 PM »
      Try this from the cmd prompt.  Does it show the file you are looking for?


      Code: [Select]
         for /r "c:\test\temp" %a in (test.txt) do echo copy "%a" "c:\new\location"

      LevyRM

        Topic Starter


        Starter

        • Experience: Familiar
        • OS: Windows 7
        Re: Need command to find a file in subfolders and then move the result.
        « Reply #6 on: March 01, 2014, 08:36:17 AM »
        Foxidrive - That worked perfectly....

        Thanks a bunch for all who have helped me with the project.