|
Answer» Hey there.
This is my first post so I apologize ahead of time if I completely missed where someone might have asked a similar question. I've search but haven't really come up with results.
I work as a technical assistant at a small business doing general fixings on whatever needs to be done at the time. However, I am not a programmer. I can't really stress this enough to people. But, this time they want me to create a batch file that will do a search for any number of keywords, output it to a file, and then copy the results found in the file to specified location while preserving the file structure. I've got the searching done, i'm able to toss it out into a text file...
Problem comes when I'm told to copy the files specified in the results. I'm pretty much just stuck and got told to use robocopy.
I've posted what I have so far. If anyone has a recommendation I'd be more than happy to take anything into consideration. Again, programming, not something I do.
Finddata.txt is just a file listing search terms so the person using it can switch it up a bit.
@echo off
::Variables set default=/s /i /m /g:finddata.txt
echo Welcome to the string search and copy batch! echo.
REM This is to specify the queries and create an output file of results
set /p drive=Please specify the location of the search:
echo Searches are contained in the file finddata.txt. Make changes as necessary and save the text file. @pause
findstr %default% %drive%\*.* > results.txt
results.txt
set /p answer=Would you like to perform a copy of the results now? (Y/N):
if %answer% == Y robocopy.exe if %answer% == y robocopy.exe if %answer% == N exit if %answer% == n exitI admit I did not test this, so you get the honor.
Code: [Select]@echo off
set default=/s /i /m /g:finddata.txt
echo Welcome to the string search and copy batch! echo.
REM This is to specify the queries and create an output file of results
set /p drive=Please specify the location of the search:
echo Searches are contained in the file finddata.txt. Make changes as necessary and save the text file. pause
findstr %default% %drive%\*.* > results.txt
set /p answer=Would you like to perform a copy of the results now? (Y/N):
if /i %answer%==Y ( for /f tokens=* delims=" %%v in (results.txt) do robocopy.exe %%v %destination% ) else ( exit )
You mentioned that you get the results.txt file OK, so I just added some code after the findstr instruction. You will have to define or replace the %destination% variable with a drive:\path. Robocopy requires a souce and destination as a minimum. Why not use just plain copy?
Good luck. Using robocopy so I can preserve the structure of the folders. Not really sure how to accomplish the same thing with copy. In any case, THANKS for the added lines you gave me but it's not copying anything... as soon as I TYPE in "Y" it closes cmd and nothing happens.
Also, how do I exclude folders from a search, such as the system folder and program files? I can see where I can REMOVE them from the results after I copy them but I'm beginning to get some machines that error out when the file paths get too long (basically hidden files)I found a potential error which has been fixed. There was also a problem (fixed) with the for LOOP which threw an error. That was an important piece information to know.
Code: [Select]@echo off
set default=/s /i /m /g:finddata.txt
echo Welcome to the string search and copy batch! echo.
REM This is to specify the queries and create an output file of results
set /p drive=Please specify the location of the search:
echo Searches are contained in the file finddata.txt. Make changes as necessary and save the text file. pause
findstr %default% %drive%\*.* > results.txt
set /p answer=Would you like to perform a copy of the results now? (Y/N):
if /i .%answer%==.Y ( for /f "tokens=* delims=" %%v in (results.txt) do robocopy.exe "%%v" %destination% ) exit
You're right, COPY will not meet your needs. XCOPY can keep the directory structure and you can use an exclude file for filtering out program files and switches for ignoring system files/folders. ROBOCOPY is also an option. Both XCOPY and ROBOCOPY have a truckload of options. Type XCOPY /? or ROBOCOPY /? at a command prompt and pick your poison.
Good luck.
|