Search and Sort the files.....?

P

Pal

Hello,
I have over 500 txt files in my C:\Temp folder. I need a
batch file to search all the files for the word "PERSON1"
and create a folder C:\Temp\Person1\ and put that file
there. Then it should search all other files
for "PERSON2" and create a folder C:\Temp\Person2\ and
put that file there. At the end, there should be six
folders Person1...Person6 and all the 500 txt files
should be sorted in their respective folders.

Thanks.
 
P

Paul R. Sadowski

Pal said:
Hello,
I have over 500 txt files in my C:\Temp folder. I need a
batch file to search all the files for the word "PERSON1"
and create a folder C:\Temp\Person1\ and put that file
there. Then it should search all other files
for "PERSON2" and create a folder C:\Temp\Person2\ and
put that file there. At the end, there should be six
folders Person1...Person6 and all the 500 txt files
should be sorted in their respective folders.

if not exist C:\Temp\Person1 md C:\Temp\Person1
if not exist C:\Temp\Person2 md C:\Temp\Person2
if not exist C:\Temp\Person3 md C:\Temp\Person3
if not exist C:\Temp\Person4 md C:\Temp\Person4
if not exist C:\Temp\Person5 md C:\Temp\Person5
if not exist C:\Temp\Person6 md C:\Temp\Person6

move c:\temp\*person1*.txt C:\Temp\Person1
move c:\temp\*person1*.txt C:\Temp\Person2
move c:\temp\*person1*.txt C:\Temp\Person3
move c:\temp\*person1*.txt C:\Temp\Person4
move c:\temp\*person1*.txt C:\Temp\Person5
move c:\temp\*person1*.txt C:\Temp\Person6

Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file.
Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are
moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.

/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.
 
P

Pal

Paul, I think, I haven't communicated the requirement
properly. The command should search for the
content "PERSON1" in all of the 500 xyz.txt files and
move only those files into C:\Temp\Person1\* folder.
Similarly for all of the other strings. (Using the
findstr command...)
-----Original Message-----

Hello,
I have over 500 txt files in my C:\Temp folder. I need a
batch file to search all the files for the word "PERSON1"
and create a folder C:\Temp\Person1\ and put that file
there. Then it should search all other files
for "PERSON2" and create a folder C:\Temp\Person2\ and
put that file there. At the end, there should be six
folders Person1...Person6 and all the 500 txt files
should be sorted in their respective folders.

if not exist C:\Temp\Person1 md C:\Temp\Person1
if not exist C:\Temp\Person2 md C:\Temp\Person2
if not exist C:\Temp\Person3 md C:\Temp\Person3
if not exist C:\Temp\Person4 md C:\Temp\Person4
if not exist C:\Temp\Person5 md C:\Temp\Person5
if not exist C:\Temp\Person6 md C:\Temp\Person6

move c:\temp\*person1*.txt C:\Temp\Person1
move c:\temp\*person1*.txt C:\Temp\Person2
move c:\temp\*person1*.txt C:\Temp\Person3
move c:\temp\*person1*.txt C:\Temp\Person4
move c:\temp\*person1*.txt C:\Temp\Person5
move c:\temp\*person1*.txt C:\Temp\Person6

Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file.
Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are
moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.

/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.


.
 
P

Paul R. Sadowski

Sorry, one of those days.

I think this will do ya:

@echo off
setlocal
for %%c in (PERSON1 PERSON2 PERSON3) do (
for /f "tokens=*" %%d in ('dir /b /-s c:\temp\*.txt') do (
call :ChkString %%c "c:\temp\%%d"
)
)
Goto :EOF

:ChkString
for /f "usebackq" %%e in (`findstr "%1" %2`) do (
set foundone=True
)
if "%foundone%" NEQ "" (
echo if not exist "c:\temp\%1" md "c:\temp\%1"
echo move %2 "c:\temp\%1"
set foundone=
)

Try it to see if it's what you want then remove the word echo from these two
lines to make it work
echo if not exist "c:\temp\%1" md "c:\temp\%1"
echo move %2 "c:\temp\%1"


Pal said:
Paul, I think, I haven't communicated the requirement
properly. The command should search for the
content "PERSON1" in all of the 500 xyz.txt files and
move only those files into C:\Temp\Person1\* folder.
Similarly for all of the other strings. (Using the
findstr command...)
-----Original Message-----

Hello,
I have over 500 txt files in my C:\Temp folder. I need a
batch file to search all the files for the word "PERSON1"
and create a folder C:\Temp\Person1\ and put that file
there. Then it should search all other files
for "PERSON2" and create a folder C:\Temp\Person2\ and
put that file there. At the end, there should be six
folders Person1...Person6 and all the 500 txt files
should be sorted in their respective folders.

if not exist C:\Temp\Person1 md C:\Temp\Person1
if not exist C:\Temp\Person2 md C:\Temp\Person2
if not exist C:\Temp\Person3 md C:\Temp\Person3
if not exist C:\Temp\Person4 md C:\Temp\Person4
if not exist C:\Temp\Person5 md C:\Temp\Person5
if not exist C:\Temp\Person6 md C:\Temp\Person6

move c:\temp\*person1*.txt C:\Temp\Person1
move c:\temp\*person1*.txt C:\Temp\Person2
move c:\temp\*person1*.txt C:\Temp\Person3
move c:\temp\*person1*.txt C:\Temp\Person4
move c:\temp\*person1*.txt C:\Temp\Person5
move c:\temp\*person1*.txt C:\Temp\Person6

Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file.
Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are
moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.

/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.


.
 
M

Matthias Tacke

Paul R. Sadowski said:
Sorry, one of those days.

I think this will do ya:
Hello Paul,
if you use the features of findstr no call neccessary. Should be a bit
faster.

@echo off
cd /D c:\TEMP
for %A in (Person1 Person2 Person3 Person4 Person5 Person6) do (
if not exist "%%A" MD "%%A"
for /F "delims=" %%B in ('findstr /I /M "%%A" *.txt') do
echo Move "%%B" "%%A"
)
)
 
P

Paul R. Sadowski

Yes, thanks, much better. I still use [ef]grep for such things so I'm not
that experienced with findstr yet. Trying to break that habit but it takes a
while...
 
P

Pal

Thanks to Paul and Matthias.

Pal.
-----Original Message-----
Yes, thanks, much better. I still use [ef]grep for such things so I'm not
that experienced with findstr yet. Trying to break that habit but it takes a
while...

Matthias Tacke said:
Hello Paul,
if you use the features of findstr no call neccessary. Should be a bit
faster.

@echo off
cd /D c:\TEMP
for %A in (Person1 Person2 Person3 Person4 Person5 Person6) do (
if not exist "%%A" MD "%%A"
for /F "delims=" %%B in ('findstr /I /M "%%A" *.txt') do
echo Move "%%B" "%%A"
)
)

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top