Solved

Batch file to check certain word in a folder full of files (Reports) and copy them to a different folder on the network

Posted on 2014-01-13
7
910 Views
Last Modified: 2014-01-14
Hello Experts

I need a batch file that would check for the word (HP) in a folder full of daily reports, the batch would check inside of every file if there's a word HP . if its found, the batch will copy the file to a different folder that's publicly shared on the network.

This should happen on daily basis.

The folder path if requested is (C:\1-WebServices-1\DeviceUpdateLogs\Server\Audit\imageUpdates)

The Public folder is c:\PublicFolder.

Would appreciate your help.

Thanks
0
Comment
Question by:Mohammed Hamada
  • 4
7 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39776510
Try this batch script; it's currently in test mode and will only display the copy commands it would normally run, so that you can test it.
Remove the uppercase ECHO in line 11 to run it for real.
@echo off
setlocal
set SourcePath=C:\1-WebServices-1\DeviceUpdateLogs\Server\Audit\imageUpdates
set TargetPath=C:\PublicFolder
set FileMask=*.*
set Search=HP
for %%a in ("%SourcePath%\%FileMask%") do (
	echo Processing %%~nxa ...
	findstr.exe /r "\<%Search%\>" "%%a" >NUL
	if not errorlevel 1 (
		ECHO copy "%%a" "%TargetPath%"
	)
)
echo Done.

Open in new window

0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39776528
Question - is HP unique enough to distinguish files?
Can this appear anywhere in the file or only a specific location?

For instance one file could contain

HP Deskjet

And another could contain

index.php

Both will match on the HP part but might not yield the required results.
0
 
LVL 23

Author Comment

by:Mohammed Hamada
ID: 39776548
Hi oBdA

I have tried it but it didn't work. I copy it to notepad and removed the ECHO in line 11 then saved as copy.bat on desktop
run it as administrator... but nothing happened.

I should mention maybe that i'm on Windows 2012! could that affect the batch file? even though no errors were reported. I didn't get anything.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 23

Author Comment

by:Mohammed Hamada
ID: 39776555
No it worked, actually the folder path was just misspelled! I have fixed it and it worked perfectly right.

Thanks a lot
0
 
LVL 23

Author Comment

by:Mohammed Hamada
ID: 39776564
OPppps, sorry I assigned the points to the wrong person, I have requested moderators attention to re-assign points to  oBdA.

Thanks
0
 
LVL 23

Author Closing Comment

by:Mohammed Hamada
ID: 39776786
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

21 Experts available now in Live!

Get 1:1 Help Now