Solved

Dos script to search multiple txt files created yesterday containing certain string and moving to another folder.

Posted on 2011-05-13
16
663 Views
Last Modified: 2012-08-13
I need a dos script that can search multiples txt files created the previous day that contains a particular string and then move these txt files to another directory?
0
Comment
Question by:DarchVader
  • 9
  • 5
  • 2
16 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35753479
OK.  How can we identify these files.  Are they all the files from yesterdays date in one directory, or anywhere in subdirs under a start point etc?

Can soon do this with yesterdays date but one classic way of dealing with such things is to schedule a task before midnight and then it is actually "todays" documents you are looking for.

Likewise are these added into a directory with a load of other files, or could you for instance:

start with an empty dir
files get created
you already want certain files moved to a different dir.  If you moved the other files too to another dir (or deleted them if not needed say) then the next day you just need to process all files again.

Anyway please give a few examples and one of us here can write a script around that.
0
 

Author Comment

by:DarchVader
ID: 35753533
Dragon,

the files are all in the same directory and will have the pattern col*.txt.I will be looking for all files with string "XXX04" in them. The files I need to move will be amongst other files with similar file name pattern. This will need to be executed on a daily basis as new files are created daily.

Hope this helps.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 125 total points
ID: 35753653
Ok, sorry to be a pain then but one more thing.... if you are moving all the file with XXX04 in them then the only ones left with XXX04 in them are the ones you need?  I only say because if there is no need to specifically check a date from batch file is simpler :-)

if it was just all the files with xxx04 in then you can do:

findstr /M /L /I "XXX04" col*.txt

to return all the filenames that match.  You can then run that through a move with:

@echo off
setlocal enabledelayedexpansion
set sourcedir="C:\sourcedir"
set searchfor="XXX04"
set destdir="C:\moveto"
set error=
cd /d %sourcedir%
for /f "tokens=*" %%a in ('findstr /M /L /I %searchfor% col*.txt') do (
echo Moving %%a to %destdir%
move /Y "%%a" %destdir%
if errorlevel 1 set error=!error! %%a
)
if not "%error%"=="" echo There was an error copying %error% if you want to deal with this or email it etc...

Steve
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:DarchVader
ID: 35753759
Steve,

Also sorry to be a pain. On reflection it would be helpful if we could pick up just previous days files and copy instead of move. Off hand do you know if this search could be extended to search pdf's?

Many thanks,

Jon
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35753823
OK... searching PDf's will probably work as long as they actually contain those text characters in the file.... it could be split up by formatting characters, or the PDF could be produced graphically and not contain the text at all?  Does it get found if you run a manual FIND against the PDF file, i.e.:

find /i "XXX04" yourfile.pdf


0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35753908
OK.  Haven't been able to test this one out yet but here is a first pass:

@echo off
setlocal enabledelayedexpansion

REM Get yesterdays date
echo wscript.echo right(100 + month(date-1),2) ^& "-" ^& right(100+day(date-1 ),2) ^& "-" ^& year(date - 1) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo %yesterday% in format mm-dd-yyyy ready for xcopy

REM Set configuration
set sourcedir=C:\sourcedir
set searchfor="XXX04"
set destdir="C:\moveto"
set tempdir=%temp%\check
set error=

REM Create and remove previous files from tempdir
mkdir "%tempdir%"
del "%tempdir%\*.*" 2>NUL

REM Copy yesterday or newer files to temporary dir - col*.txt and col*.pdf, change as needed
xcopy /d %yesterday% "%sourcedir%\col*.txt" %tempdir%
xcopy /d %yesterday% "%sourcedir%\col*.pdf" %tempdir%

REM Check all files in temporary dir, move required ones to destination, delete others
cd /d %tempdir%
for /f "tokens=*" %%a in ('findstr /M /L /I %searchfor% *.*') do (
echo Moving %%a to %destdir%
move /Y "%%a" %destdir%
if errorlevel 1 set error=!error! %%a
)

if not "%error%"=="" echo There was an error moving %error% if you want to deal with this or email it etc...
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35753915
0
 

Author Comment

by:DarchVader
ID: 35754102
Steve,

The find command searched the pdf's but findstr does not seem to and therefore your first solution produces no results. Any idea why?

Regards.
0
 

Author Comment

by:DarchVader
ID: 35754143
Steve,

Sorry, on further investigation, the find command is returning all pdf names not just those that contain the string.

Regards.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35754194
Hmm, I've just ran a FINDSTR command against an old dir of PDF's:

findstr /M /I /L "dragon" *.pdf
and it returned only 5 files out of hundreds created with pdf995.

Can you post / email (see my profile, click dragon-it above for email address) one of the PDF files in question that DOES and one that does NOT have the string in at all?
0
 

Author Comment

by:DarchVader
ID: 35754336
Steve,

The 2 pdf's have been sent to contactus at your email domain.

regards.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35755270
Jon,

Well I had always assumed the text bits would most likely be plain text within the PDF but clearly they aren't so the text you mentioned for the account number does not appear.  Will see if there are any PDF searching command line tools available unless anyone else gets there first!
Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35769732
I have failed so far to find any sensible third party command line application that can search a PDF for it's contents -- I had always assumed that within the file the text parts would be stored as normal text and searchable with FIND therefore but it seems they aren't... if you can find a suitable util. before I do that allows searching from the command line then we can add to it.
0
 
LVL 2

Expert Comment

by:hari_b85
ID: 35800336
Hi,

You can do this by using robocopy command, the command will loook something like this,

robocopy "source" "destination"  col*.txt /s /v /r:0 /w:0 /MAXAGE:1
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35800413
Can robocopy search the contents of a PDF file to identify  string within it?
0
 
LVL 2

Expert Comment

by:hari_b85
ID: 35800419
Robocopy can't read the content. I gave the idea just in case it would help using the filename.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now