Please register or login. There are 0 registered and 1071 anonymous users currently online. Current bandwidth usage: 326.30 kbit/s December 14 - 11:21pm EST 
Hardware Analysis
      
Forums Product Prices
  Contents 
 
 

  Latest Topics 
 

More >>
 

    
 
 

  You Are Here: 
 
/ Forums / Programming /
 

  Batch code to read, then move files from one folder to another 
 
 Author 
 Date Written 
 Tools 
Brandon Spencer Sep 28, 2011, 01:36pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List Replies: 6 - Views: 4241
I'm new to batch coding, so I was hoping someone could help me out. I need to read several .txt files(all in the same directory) and search them for a specific string, lets say "key1" and "key2." If the file has key1, then it needs to be moved into directory1. If the file has key2, then it needs to be moved to directory2. There's no telling ahead of time what line the string is going to be on, so I'm guessing I could use "Find" to search for the key? Then how do I loop through all the files, read them, then sort them into the different directories?
Any help is much appreciated! :)


Want to enjoy fewer advertisements and more features? Click here to become a Hardware Analysis registered user.
john albrich Sep 28, 2011, 02:51pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Batch code to read, then move files from one folder to another
Brandon Spencer said:
I'm new to batch coding, so I was hoping someone could help me out. I need to read several .txt files(all in the same directory) and search them for a specific string, lets say "key1" and "key2." If the file has key1, then it needs to be moved into directory1. If the file has key2, then it needs to be moved to directory2. There's no telling ahead of time what line the string is going to be on, so I'm guessing I could use "Find" to search for the key? Then how do I loop through all the files, read them, then sort them into the different directories?
Any help is much appreciated! :)


If I understand your post correctly, this is probably the most straight-forward approach. No recursion or advanced "tricks" are used. It's brute force. And I'm assuming you are only going through a single input file per string (e.g. you are NOT wanting to go through multiple different input files looking for "key1" and outputting the results of all searches into a single output file.)
For a CASE SENSITVE search you could do something like this, where "c:\temp\test.txt" is the input file.
Where quotes are shown below they are required.

This assumes your input file ("test.txt") is located in c:\temp directory

find "key1" c:\temp\test.txt > c:\directory1\key1.txt
find "key2" c:\temp\test.txt > c:\directory2\key2.txt
find "key3" c:\temp\test.txt > c:\directory3\key3.txt

Specific details on using the "find" found here:
http://www.microsoft.com/resources/documentation/windows/xp/al...x?mfr=true

For safety while developing your exact commands, do not use your actual files involved. Either use copies or create test input files until you get things right.
These kinds of commands are not "smart". They will over-write existing files so be sure you use output names that don't match existing files unless that is what you intend.

john albrich Sep 28, 2011, 03:14pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Sep 28, 2011, 03:28pm EDT

 
>> Re: Batch code to read, then move files from one folder to another
Brandon Spencer said:
I'm new to batch coding, so I was hoping someone could help me out. I need to read several .txt files(all in the same directory) and search them for a specific string, lets say "key1" and "key2." If the file has key1, then it needs to be moved into directory1. If the file has key2, then it needs to be moved to directory2. There's no telling ahead of time what line the string is going to be on, so I'm guessing I could use "Find" to search for the key? Then how do I loop through all the files, read them, then sort them into the different directories?
Any help is much appreciated! :)


IF on the other hand you want to actually MOVE the files containing the strings you are looking for, then this is probably the most straight-forward approach. No recursion or advanced "tricks" are used. It's brute force. And I'm assuming once you have found a file with "key1" for example, you are NOT going to search it also for "key2", etc.
For a CASE SENSITIVE search you could do something like this, where "c:\temp\test.txt" is the input file.
Where quotes are shown below they are required.
Oh yeah...and the "test1", "test2", "test3", etc. directories need to be PRE-created manually before the batch file is run (or you could add an "md" command in the batch file for each directory to be created.

This assumes your input files ("test1.txt", "test2.txt", etc) are located in c:\temp directory

find "key1" c:\temp\test1.txt
if errorlevel 0 move c:\temp\test1.txt c:\temp\test1
find "key2" c:\temp\test2.txt
if errorlevel 0 move c:\temp\test2.txt c:\temp\test2
find "key3" c:\temp\test3.txt
if errorlevel 0 move c:\temp\test3.txt c:\temp\test3

I would assume that IF you do plan to search through a single file for all key(n) then you would need to replace the "move" command with a "copy" command.


Specific details on using the "find", "if", and other commands are found here:
http://www.microsoft.com/resources/documentation/windows/xp/al...x?mfr=true

For safety while developing your exact commands, do not use your actual files involved. Either use copies or create test input files until you get things right.
These kinds of commands are not "smart". They will over-write existing files so be sure you use output names that don't match existing files unless that is what you intend.


edit:
fixed important typos due to copying lines from earlier post, but failing to remove unneeded specific output filenames...since input files are being moved...not copied or created.
edit to add: details of "if" command reference to the microsoft website

john albrich Sep 28, 2011, 08:05pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Sep 28, 2011, 08:05pm EDT

 
>> Re: Batch code to read, then move files from one folder to another
.
Brandon,

Some other items you might be very interested in is a freeware utility called "bgrep". It basically provides much of the flexibility and power of the Linux "grep" command.

and freeware XXCOPY.

Depending on what you want to do, using either of these it's much easier to do some very sophisticated stuff in batch files.

And of course being more flexible they can be massively dangerous to use even if you know what you're doing. Either utility can be incredibly unforgiving of mistakes.


Brandon Spencer Sep 29, 2011, 01:17am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Batch code to read, then move files from one folder to another
Thanks so much for your help, John! The second reply you gave me was the one closer to home. That was very helpful, however, I need to put this in some kind of loop, because I need this to check hundreds to thousands of .txt files for these keys. Also, I'm guessing I'll have to nest an If statement inside that loop, because if the file being read doesn't have, say "key1", then it needs to be check for "key2", "key3", etc. Then, once the key is identified, it then needs to be moved into the proper directory. So, I'm guessing I can simply put the find statements that you demoed for me inside a for loop?

Thank you so much for your help, You've been awesome!

Edit: in the second reply you gave me, the sample code has "if errorlevel 0..." what does this code do? Thanks again!

john albrich Sep 29, 2011, 03:02am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Sep 29, 2011, 03:32am EDT

 
>> Re: Batch code to read, then move files from one folder to another
.
The "if errorlevel 0" statement is described in the microsoft reference page I mentioned, but basically it tests the result of the last executed command. in this case, an errorlevel=0 means that the searched for "key1" string was found. And since it was found it then moves the file to the specified directory. If "key1" had not been found in the file, then it would have returned a non-zero "errorlevel" and the file wouldn't be moved...it would just proceed to the next line in the script.

Of course, loops and other conditionals can be used.

Given the quantity of work you're looking at, I'd suggest using list files for your search string parameters, and I'd use the redirect capabilities of the command line for both input and output. Also note the difference between ">" vs ">>".

I'm quite sure you can find examples of almost exactly what you are asking for on the internet. This is almost classic introductory search and conditional file manipulation.

There are tutorials on using the commands, nested loops, conditionals, etc. I'd search on things similar to these:
batch file string searches tutorials
batch file string searches examples
batch file manipulation
batch file processing
In a very quick glance, some of them appear to be written well enough and fairly comprehensive.


edit to add:
Glad I could help. And don't forget the microsoft reference.
http://www.microsoft.com/resources/documentation/windows/xp/al...x?mfr=true
There are a lot of FOR/NEXT command examples there having to do with file manipulation. Glancing just now through the "FOR" webpage it looked like there were some examples similar enough to your situation that they should give you a good start.

Note that these command change a bit from one version of Windows to another so I'll just caution again to use practice files and directories before you start working on the real files and real directories. Don't even run commands from within your "real" data directories even if your scripts don't explicitly reference them. I'd set up some special "temp" or "test" folders. As always make sure your "real" data are all backed up or easily recreated before manipulating their files/folders.

edit to add:
added a sentence describing what happens if "key1" wasn't found in the "if errorlevel" test.

Brandon Spencer Sep 29, 2011, 07:13pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Batch code to read, then move files from one folder to another
Ok, this is what I have:

for %%f in (*.txt) do find /i "Tc-section" %%f
if errorlevel 0 move %%f c:\Users\brandon_spencer\Desktop\ais

However, when I run the .bat in cmd, I find that it is cycling through my example files, showing me which files in fact have that string, and then at the end it tries to run the if statement and says "The system cannot find the file specified."

is it not reading the variable %%f? Tried to look at other examples, but most are sorting things based on file name.


Write a Reply >>


 

    
 
 

  Topic Tools 
 
RSS UpdatesRSS Updates
 

  Related Articles 
 
 

  Newsletter 
 
A weekly newsletter featuring an editorial and a roundup of the latest articles, news and other interesting topics.

Please enter your email address below and click Subscribe.