Computer Hope

Microsoft => Microsoft DOS => Topic started by: LevyRM on February 27, 2014, 05:09:03 PM

Title: Need command to find a file in subfolders and then move the result.
Post by: LevyRM on February 27, 2014, 05:09:03 PM
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?
Title: Re: Need command to find a file in subfolders and then move the result.
Post by: Lemonilla 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.
Title: Re: Need command to find a file in subfolders and then move the result.
Post by: foxidrive 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.
Title: Re: Need command to find a file in subfolders and then move the result.
Post by: LevyRM 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]
Title: Re: Need command to find a file in subfolders and then move the result.
Post by: Lemonilla 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.
Title: Re: Need command to find a file in subfolders and then move the result.
Post by: foxidrive 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"
Title: Re: Need command to find a file in subfolders and then move the result.
Post by: LevyRM on March 01, 2014, 08:36:17 AM
Foxidrive - That worked perfectly....

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