How to find file in whole drive which has particular string?
Moderator: DosItHelp
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
How to find file in whole drive which has particular string?
Hi All,
I want to find the file (specifically *.bat file only) in whole D drive which has the string "ABC"
"ABC" string should presnt in that file content and I want to print the path along with that file name in one txt file.
I am able to get all the *.bat files in D drive (from all folder & subfolder) & able to find the file with "ABC" as a content using FIND command. But not able to combine this both logics and build one batch file which will do this together and provide me one txt file which will have list of all the *.bat files along with path which has ABC" string presnt in that file content
I want to find the file (specifically *.bat file only) in whole D drive which has the string "ABC"
"ABC" string should presnt in that file content and I want to print the path along with that file name in one txt file.
I am able to get all the *.bat files in D drive (from all folder & subfolder) & able to find the file with "ABC" as a content using FIND command. But not able to combine this both logics and build one batch file which will do this together and provide me one txt file which will have list of all the *.bat files along with path which has ABC" string presnt in that file content
Re: How to find file in whole drive which has particular str
If there are hundreds of thousands of batch files then tell us so we can adjust the technique.
remove the /i to make it case sensitive.
remove the /i to make it case sensitive.
Code: Select all
@echo off
for /f "delims=" %%a in ( ' dir d:\*.bat /a-d /b /s ' ) do (
echo testing "%%a"
find /i "ABC" <"%%a" >nul && >>"c:\file.log" echo %%a
)
pause
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
Re: How to find file in whole drive which has particular str
Excellent..!! this is called minute understanding. I tried my logic whcih was around 15 lins whcih could not work, But this 4 line worked nicely.
Yes. Need some more help. The batch files are more than thousands. what change you were talking, please let me know,.
Also If I want to replace that string with "XYZ" then what needs to be done..??
Yes. Need some more help. The batch files are more than thousands. what change you were talking, please let me know,.
Also If I want to replace that string with "XYZ" then what needs to be done..??
Re: How to find file in whole drive which has particular str
This is the only change - it makes the batch file start quicker if there are a very large number of files. (it can take 20 minutes or more to start with huge file numbers).
@echo off
dir d:\*.bat /a-d /b /s >file.txt
for /f "delims=" %%a in ( file.txt ) do (
echo testing "%%a"
find /i "ABC" <"%%a" >nul && >>"c:\file.log" echo %%a
)
del file.txt 2>nul
pause
@echo off
dir d:\*.bat /a-d /b /s >file.txt
for /f "delims=" %%a in ( file.txt ) do (
echo testing "%%a"
find /i "ABC" <"%%a" >nul && >>"c:\file.log" echo %%a
)
del file.txt 2>nul
pause
Re: How to find file in whole drive which has particular str
This is untested, it uses GNUsed, and it should replace every occurrence of ABC with XYZ and is case insensitive in the replacement.
The /i in the find command makes the search case insensitive - and that will pass files with ABC and abc to the SED command.
If you ONLY want to change ABC and not change abc then remove the /i and remove the I from Ig.
Make sure you test it on copies of your files because if it changes abc to XYZ and you didn't expect it, then you need to be able to restore from backup.
The /i in the find command makes the search case insensitive - and that will pass files with ABC and abc to the SED command.
If you ONLY want to change ABC and not change abc then remove the /i and remove the I from Ig.
Make sure you test it on copies of your files because if it changes abc to XYZ and you didn't expect it, then you need to be able to restore from backup.
Code: Select all
@echo off
dir d:\*.bat /a-d /b /s >file.txt
for /f "delims=" %%a in ( file.txt ) do (
echo testing "%%a"
find /i "ABC" <"%%a" >nul && (
echo altering "%%a"
sed "s/ABC/XYZ/Ig" "%%a" >temp.tmp
move /y temp.tmp "%%a" >nul
)
)
del file.txt 2>nul
pause
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
Re: How to find file in whole drive which has particular str
The replacing script was not good..
...It deleted all the contents from all the files where ABC was present. all the files it made blank..
I ran it as it is. and I am not woried about CASE. it should actully search for ABC as well as abc and replace it with XYZ.
Please suggest.

I ran it as it is. and I am not woried about CASE. it should actully search for ABC as well as abc and replace it with XYZ.
Please suggest.
Re: How to find file in whole drive which has particular str
You haven't got SED have you?
You need to install GNUsed for Windows - the script works fine here.
You need to install GNUsed for Windows - the script works fine here.
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
Re: How to find file in whole drive which has particular str
No. We dont have this. I wan tot make as generic as possible. Sicne I need to run this on 180 servers whcih are different from each other. So can this be most simple way done. As in we just need DOS and easily it will run without any pre-requiests.
those 180 servers are some win 2003 and some are win 2008. So SED will not be there on it.
those 180 servers are some win 2003 and some are win 2008. So SED will not be there on it.
Re: How to find file in whole drive which has particular str
Do you have WSH (Windows scripting host) available on the servers?
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
Re: How to find file in whole drive which has particular str
2008 its present but not on 2003. And out 180 more than 60% are 2003.
Re: How to find file in whole drive which has particular str
ok.
Unless you haven't been totally honest and it is not bat files, but some other type of file without poison characters, then batch isn't suited to handle poison characters and blank lines that will be in bat files.
Unless you haven't been totally honest and it is not bat files, but some other type of file without poison characters, then batch isn't suited to handle poison characters and blank lines that will be in bat files.
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
Re: How to find file in whole drive which has particular str
No No.It is a bacth file. Only thing I used, where i was not honest is the word. Infact i can tell you the word as well. the word in US97UAP03IBMT. This is actully DNS name where we connect and that need to be found on the server whcih all bacth script has. Another thing we are going to migrate it other server so this DNS will be changed. There are batch script which needs to be identified and then replace this word. And i need simliest solution for that.
Re: How to find file in whole drive which has particular str
ok.
Batch files alone aren't suited for editing text where there are poison characters.
Someone else might like to have a stab at it but you might get a solution that corrupts your bat files in other ways, until all the bugs are ironed out.
Blank lines and poison characters are a headache to deal with so it's best to use the right tool for the job.
Can you map each server to a drive letter with admin permissions and then run the batch I create above, and only install SED on one machine?
With a list of server IP addresses then that can be added to the batch file.
Batch files alone aren't suited for editing text where there are poison characters.
Someone else might like to have a stab at it but you might get a solution that corrupts your bat files in other ways, until all the bugs are ironed out.
Blank lines and poison characters are a headache to deal with so it's best to use the right tool for the job.
Can you map each server to a drive letter with admin permissions and then run the batch I create above, and only install SED on one machine?
With a list of server IP addresses then that can be added to the batch file.
-
- Posts: 7
- Joined: 27 Oct 2012 07:15
Re: How to find file in whole drive which has particular str
Mapping can not be done all the servers. we had that optiona as well. But its not going to work for all 180 servers. Bec they reside in different different domains.
what do you thing. BATCH should be the right thing for this or VB Script. But again for VB there might be some limitations come whcih i need to find. But i felt BATCH file only will be suitable to handale batch files. But stil pls suggest between .bat and .vbs...??
But your first solution has sloved my majour work wher i find the script for particualr name..
ur suggestions wud help to proceed further.
what do you thing. BATCH should be the right thing for this or VB Script. But again for VB there might be some limitations come whcih i need to find. But i felt BATCH file only will be suitable to handale batch files. But stil pls suggest between .bat and .vbs...??
But your first solution has sloved my majour work wher i find the script for particualr name..
ur suggestions wud help to proceed further.
Re: How to find file in whole drive which has particular str
.vbs scripts can work - that is Windows scripting host.
If all the servers can run .vbs scripts then it can be done.
Cscript.exe is the host file which runs the .vbs scripts.
If all the servers can run .vbs scripts then it can be done.
Cscript.exe is the host file which runs the .vbs scripts.