String search and then move the files

August 30, 2010 at 10:55:14
Specs: Windows 7
I'm trying to find a string in a file and if that string is found then it should move the file to another directory. Example:

FindStr "12345" and it finds it in D:\Test\Number.txt then move to D:\Test2\


See More: String search and then move the files

Report •


#1
August 30, 2010 at 11:24:06
find all the extension under '.txt'
c:\>dir *.txt /s | more

or search for filename

c:\>dir filename.* /s | more


Report •

#2
August 30, 2010 at 11:58:42
I don't need to know how many files there are, the batch just needs to know what files had that string in it and then move thoses files to another directory. The following statement works in the command line but not in a batch file. What's going on?

For %G IN (*.dat) do (findstr /n /i "12345" "%G") && move %G D:\Test2


Report •

#3
August 30, 2010 at 14:22:24
Try this:

@ECHO OFF

FOR /f "tokens=*" %%a IN ('FINDSTR /i /m "STRING" "P:\a\t\h\*.txt"') DO (
	IF "%ERRORLEVEL%"=="0" MOVE %%a "P:\a\t\h">Nul
)

Can I put a question here?


Report •

Related Solutions

#4
August 31, 2010 at 07:06:09
Hi Matt123,

This worked great, thank you so much. It's been a long time since I did batch files. Could you explain a little about what the / and tokens mean?


Also how would I have it search for another string also in the same directory. Let's say that string is "9876" and it's in other files. Thanks


Report •

#5
August 31, 2010 at 07:28:07
tokens are hard to explain, but ill do my best.

say you had "tokens=2 delims= ", the script would get the second "item" ( again, I can't really expain it) using " " as a separator (delimiter).

ex. The quick brown fox jumped over the lazy dog.

The word quick is the seconds word with a space seperating it, so it would set quick as the variable.

the /i switch of findstr makes the string to search not case-sensitive. The /m switch prints the file name, allowing you to set it as a variable (to move).

----

To find another string, change where i wrote "STRING" to what you want to search for. Also, change the "P:\a\t\h" to the path of your choice.

Can I put a question here?


Report •

#6
August 31, 2010 at 07:40:54
Can't I do it in the same batch file with a ELSE statement? There is going to be several files in the directory with one or the other string in them and it would be better if I could just have one batch file firing off at a time to do the task. Thanks

Report •

#7
August 31, 2010 at 07:54:47
Hi Matt123,

Thanks for explaning your code, it makes sense now. Also with searching for another string in the same batch I just copied your code and pasted below the ) and changed the string and path and it working, but is that the most effective way of doing the batch? Thanks


Report •

#8
August 31, 2010 at 09:24:06
I don't know what I'm doing wrong but the code doesn't work for long paths. My paths are: "D:\Program Files\nsoftware\AS2 Connector V2\as2data\08925141CORP\Incoming\*.*" for the search path and "D:\Program Files\nsoftware\AS2 Connector V2\as2data\08925141CORP\Logs\S Files\" for the move path. Any Ideas? Thanks

Report •

#9
August 31, 2010 at 15:15:15
You could use XCOPY, and Then put a DEL following it.

XCOPY %%a "P:\a\t\h"
DEL %%a
)

Can I put a question here?


Report •

#10
September 3, 2010 at 08:19:58
Hi Matt123,

Thanks for your help. The long path issue was because of the Program Files. That's a system env. var. I have it all working now and automated and it's running great. Thank you!!!


Report •

#11
September 3, 2010 at 11:37:32
Not a problem. Glad I was able to help!

Can I put a question here?


Report •

Ask Question