Solved Search string and copy files in the folder.

September 2, 2014 at 02:50:03
Specs: Windows 7
DOS Batch File

1 I want to search string and copy files in the folder but don’t want to overwrite the files if it contains the same name.

2. Search Criteria should be date wise.


Please help how to do this.


See More: Search string and copy files in the folder.

Report •

✔ Best Answer
September 15, 2014 at 17:35:15
::begin script
@echo off & setlocal enabledelayedexpansion
:: set destination here
set dest=c:\abc
:: put search-string in quotes - COMMAND-LINE ONLY. no quotes if prompted.
:: EDIT 9/18 add foll 5 lines - set source-drive here
set src=%4
if not defined src set /p src=drive:
if not defined src goto :eof
set src=%src::=%
cd /d src:\
set x=%~1
if not defined x set /p x=string to find - no dbl quotes, not case sensitive:
if not defined x goto :eof
::get dates 1,2: yyyymmdd
set d1=%2
if not defined d1 set /p d1=earliest date in format yyyymmdd
if not defined d1 goto :eof
set d2=%3
if not defined d2 set /p d2=latest date in format yyyymmdd
if not defined d2 goto :eof
>list.bat echo echo preparing to copy as per the following:
>>list.bat echo @more +3 list.bat
>>list.bat echo @pause
for /f "tokens=*" %%a in ('dir /b /a-d') do (
for /f "tokens=1-3 delims=/ " %%b in ("%%~ta") do set z=%%d%%b%%c
if !z! geq %d1% if !z! leq %d2% (
find /i "%x%"<"%%a">nul&&(
for /f "tokens=2-8 delims=/.: " %%J in ("%date%/%time%") do set stamp=%%L%%J%%K%%M%%N%%O

::Edit 9/18: nxa to pnxa for full path
>>list.bat echo copy /-y "%%~pnxa" "%dest%\%%~na!stamp!%%~xa"
)
)
)
:: optional: call list.bat
call list.bat
::====== end
no provision for possible duplicated filenames, but it should "hang" if it encounters one. Then we'll cross that bridge by adding %%P (tenths of seconds).

message edited by nbrane



#1
September 2, 2014 at 20:11:58

::------- begin batch script 'aja.bat'
@echo off & setlocal
set sourcedir=c:\aaa\bbb
cd /d %sourcedir%
set targetdir=c:\zzz\yyy
set dt=%1
if not defined dt set /p dt=date criteria to search for:
if not defined dt goto :eof
for /f "tokens=5*" %%a in ('dir ^| findstr /r "^%dt%.*"') do if not exist "%targetdir%\%%a" copy "%%a" "%targetdir%"

::===== not tested all the way.


Report •

#2
September 2, 2014 at 22:29:59
I don't know what this means:

"2. Search Criteria should be date wise."

But for the first part:

::====== script starts here ===============
::
:: ajay.bat 2014-09-03 5:12:06.70
@echo off & setLocal enableDELAYedeXpansioN

pushd d:\files

:main
for /f "tokens=* delims= " %%a in ('dir/b N*.txt') do (
find /i "wiki-chiangmai" < "%%a" > nul
if not errorlevel 1 if not exist "%TMP%\%%a" copy "%%a" %TMP%\ > nul
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
September 3, 2014 at 07:05:56
Thanks for your reply...

1. I want to search string from entire drive and copy files in other destination folder but don’t want to overwrite the files if it contains the same name then it will rename duplicate files.

Source Drive is C:\ or N:\ etc.
Batch file will search string from entire C: drive. User will specify three parameter value while runing batch file i.e. search string name, from date and to date as parameter.

For e.g.
1. My batch file name is Test.bat

Then user will specify below parameter like this.

C:\Test.bat <<Search String>> <<Form Date>><<To Date>>

C:\Test.bat "Error retrieving customer details" From Date(dd/mm/yyyy) To Date(dd/mm/yyyy)

else it will ask Three questions.
C:\>Test.Bat <<Enter>>
Please enter string: "Hello!" <<Enter>>
Please enter from date: "dd/mm/yyyy" <<Enter>>
Please enter to date: "dd/mm/yyyy" <<Enter>>

message edited by ajayrohan


Report •

Related Solutions

#4
September 4, 2014 at 04:40:35
Please reply my query

Report •

#5
September 4, 2014 at 19:10:58
This:
Please enter string: "Hello!" <<Enter>>

string I'm assuming is partial content of the file itself, and not file-name? so you want to scan all the files in a certain date-range that contain a specific string...
::begin script
@echo off & setlocal enabledelayedexpansion
:: set destination here
set dest=c:\xxx
::get dates: yyyymmdd
set d1=%1
if not defind d1 set /p d1=earliest date in format yyyymmdd
if not defined d1 goto :eof
set d2=%2
if not defined d2 set /p d2=latest datee in format yyyymmdd
if not defined d2 goto :eof
:: put search-string in quotes
set x=%~3
if not defined x set /p x=string to find - no dbl quotes, not case sensitive:
if not defined x goto :eof
for /f "tokens=*" %%a in ('dir /b /s /a-d c:\*.*') do (
:: assumed mm/dd/yyyy output of %%~ta
for /f "tokens=1-3 delims=/" %%b in ("%%~ta") do set z=%%d%%b%%c
if !z! geq %d1% if !z! leq %d2% (
find /i "%x%"<"%%a">nul&&(
if not exist "%dest%\%%a" (>>list.bat echo copy /-y "%%a" "%dest%\%%a") else (>>renames echo "%%a")
)
)
:: optional: call list.bat
::====== end script - NOT TESTED, results in "list.bat", "renames"

message edited by nbrane


Report •

#6
September 8, 2014 at 03:52:38
Thanks nbrane for your reply. Sorry for trouble you again. I am facing difficulty to execute this script.


Can you please bit modify as below?

1. Open the file i.e. C:\Test.bat <<Enter>>
2. It will ask first question. Enter String: "Hello!" <<Enter>> (User input)
3. It will ask second question. Enter From Date: dd/mm/yyyy <<Enter>>
4. It will ask third question. Enter To Date: dd/mm/yyyy <<Enter>>

.... Script will process..


Report •

#7
September 8, 2014 at 18:31:34
I didn't realize the order was all that critical, but that could well be the case.
:: excerpt, changed to reflect 'string' start-date end-date
:: put search-string in quotes - COMMAND-LINE ONLY. no quotes if prompted.
set x=%~1
if not defined x set /p x=string to find - no dbl quotes, not case sensitive:
if not defined x goto :eof
set d1=%2
if not defined d1 set /p d1=earliest date in format yyyymmdd
if not defined d1 goto :eof
set d2=%3
if not defined d2 set /p d2=latest date in format yyyymmdd
if not defined d2 goto :eof
::------- all the rest before and after is the same.

Report •

#8
September 9, 2014 at 07:36:16
I am getting below error while executing file.

----------------------------------------------------
C:\SearchTest>ajay.bat "Hello"
d1 was unexpected at this time.

C:\SearchTest>
----------------------------------------------------
I not familiar with dos script but I need it urgently.

Please modify the code as per my requirement. It will be great help for me.

message edited by ajayrohan


Report •

#9
September 9, 2014 at 19:17:35
::begin script
@echo off & setlocal enabledelayedexpansion
:: set destination here
set dest=c:\abc
:: put search-string in quotes - COMMAND-LINE ONLY. no quotes if prompted.
set x=%~1
if not defined x set /p x=string to find - no dbl quotes, not case sensitive:
if not defined x goto :eof
::get dates 1,2: yyyymmdd
set d1=%2
if not defined d1 set /p d1=earliest date in format yyyymmdd
if not defined d1 goto :eof
set d2=%3
if not defined d2 set /p d2=latest date in format yyyymmdd
if not defined d2 goto :eof

>list.bat echo echo preparing to copy as per the following:
>>list.bat echo @more +3 list.bat
>>list.bat echo @pause
for /f "tokens=*" %%a in ('dir /b /a-d') do (
for /f "tokens=1-3 delims=/ " %%b in ("%%~ta") do set z=%%d%%b%%c
if !z! geq %d1% if !z! leq %d2% (
find /i "%x%"<"%%a">nul&&(
if not exist "%dest%\%%~nxa" (>>list.bat echo copy /-y "%%~nxa" "%dest%\%%~nxa") else (>>renames echo "%%~nxa")

)
)
)
:: optional: call list.bat
call list.bat
::====== end
fixed the bug. Result will be in LIST.BAT, whereever you want to path that file, and "renames" to make a list of ones that need renamed. The renaming could be automated, but not sure how you want to handle it.

message edited by nbrane


Report •

#10
September 10, 2014 at 04:38:41
Thank you so much nbrane for your prompt reply.

I have tried above code but nothing happened. For testing this code I have created one text file and written "Hello" and saved in sub folder under C drive. I modified destination folder inside the script as C:\XXX to C:\abc.

After running above script I have not found any single file in the destination folder i.e. "C:\abc".

I want to search all text files from C: drive which is having search string and copy into destination folder.

For date please consider "Date Modified" which is showing in windows file explorer.

Please check your code and reply back.

Sorry for the trouble


------------------------------------------

C:\SearchTest>ajay.bat
string to find - no dbl quotes, not case sensitive: Hello
earliest date in format yyyymmdd 20140730
latest date in format yyyymmdd 20140910

C:\SearchTest>
------------------------------------------


Report •

#11
September 10, 2014 at 19:09:59
My mistake (as usual. apologies!) Fix this line:
for /f "tokens=1-3 delims=/" %%b in ("%%~ta") do set z=%%d%%b%%c
to be:
for /f "tokens=1-3 delims=/ " %%b in ("%%~ta") do set z=%%d%%b%%c

Or cut/paste from my previous reply where I made this fix. I admit to a really dumb mistake. Let me know - my limited test seemed to work.


Report •

#12
September 11, 2014 at 03:42:53
Hi friend its not working.. Event I tried with your fix but not found any result in destination folder.

Please look into it. Its very ugent.

Sorry to disturb you.

message edited by ajayrohan


Report •

#13
September 11, 2014 at 18:44:07
from resp#9:
"Result will be in LIST.BAT, whereever you want to path that file, and "renames" to make a list of ones that need renamed. The renaming could be automated, but not sure how you want to handle it."
Did you look for/examine these files? they should be wherever you ran your batch from. Nothing will happen until you find and examine them, unless the script gets changed. Run #9 (modified) again and watch for any results.

Report •

#14
September 12, 2014 at 08:03:15
I have followed below steps for testing your code.
1. Created file "findone.txt" file and saved in "C:\SearchTest(Folder)"
2. Copied your code and saved in "Test.bat" file which is under "C:\SearchTest(Folder)"
3. Opened "findone.txt" file and written text hello inside the file and closed. Created date of file is today i.e. 12th of Sep. So I can use date range between 20140901 to 20140914
4. Created destination folder "abc" under c: drive and modified in your code i.e. C:\xxx to C:\abc
5. Executed batch file. i.e. Test.bat
-----------------------------------------------------------------
C:\SearchTest>Test.bat
string to find - no dbl quotes, not case sensitive: Hello
earliest date in format yyyymmdd 20140901
latest date in format yyyymmdd 20140914

C:\SearchTest>
-----------------------------------------------------------------
6. Opened destination folder and found nothing. What I expect that "findone.txt" file should be inside folder C:\abc because that file is having string value i.e. "Hello"

Please let me know how you tested your code.


Report •

#15
September 12, 2014 at 17:57:02
You have been very patient. I apologize for this ordeal - it is my fault. Resp #9 should work now. The main fault was a missing final "(", which must have got lost in the trans-scription. That's why there was no output from your test. :-( Try a "clean" cut-paste of number nine, with your local adjustments as needed, and let me know.

Report •

#16
September 15, 2014 at 05:46:30
Thank you so much ‘nbrane’ it’s working.
But this code is not handling duplicate file name as I expected. For duplicate file name I found ‘rename’ file inside destination folder which I dont want.

My expectation is as below.
e.g.
1. First file name is Test.txt and inside that file text is "Hello".
Location C:\Jon\Test.txt
2. Second file name is also same i.e.Test.txt and inside that file text is "Hello".
Location C:\Merry\Test.txt
In this case I want both the files in destination folder.
For duplication file name.
Is it possible to append date and time suffix for unique identifications?
First File name will store like this C:\abc\Test20140915602.txt
Second File will store like this C:\abc\Test20140915603.txt

Keep extension of file name as same.

Sorry to trouble you again.


Report •

#17
September 15, 2014 at 17:35:15
✔ Best Answer
::begin script
@echo off & setlocal enabledelayedexpansion
:: set destination here
set dest=c:\abc
:: put search-string in quotes - COMMAND-LINE ONLY. no quotes if prompted.
:: EDIT 9/18 add foll 5 lines - set source-drive here
set src=%4
if not defined src set /p src=drive:
if not defined src goto :eof
set src=%src::=%
cd /d src:\
set x=%~1
if not defined x set /p x=string to find - no dbl quotes, not case sensitive:
if not defined x goto :eof
::get dates 1,2: yyyymmdd
set d1=%2
if not defined d1 set /p d1=earliest date in format yyyymmdd
if not defined d1 goto :eof
set d2=%3
if not defined d2 set /p d2=latest date in format yyyymmdd
if not defined d2 goto :eof
>list.bat echo echo preparing to copy as per the following:
>>list.bat echo @more +3 list.bat
>>list.bat echo @pause
for /f "tokens=*" %%a in ('dir /b /a-d') do (
for /f "tokens=1-3 delims=/ " %%b in ("%%~ta") do set z=%%d%%b%%c
if !z! geq %d1% if !z! leq %d2% (
find /i "%x%"<"%%a">nul&&(
for /f "tokens=2-8 delims=/.: " %%J in ("%date%/%time%") do set stamp=%%L%%J%%K%%M%%N%%O

::Edit 9/18: nxa to pnxa for full path
>>list.bat echo copy /-y "%%~pnxa" "%dest%\%%~na!stamp!%%~xa"
)
)
)
:: optional: call list.bat
call list.bat
::====== end
no provision for possible duplicated filenames, but it should "hang" if it encounters one. Then we'll cross that bridge by adding %%P (tenths of seconds).

message edited by nbrane


Report •

#18
September 17, 2014 at 07:28:58
Thanks mate for your efforts.
Below loophole I found while performing testing.

1. Created two folders under C:\ i.e. a) SearchTest b) CodeTesting
2. I kept your code file inside C:\SearchTest\Test.bat
3. Created two files with same name i.e. Test01.txt and saved in two different folders. Search text also same in both files.
4. After executing I found only one file copied in the destination folder i.e. that file which has stored in same location where batch file stored. It means code not searching files which is stored in other location.

Please check once again.
Other thing I want to know if user wants to change drive location then which part of code need to change i.e. instead of C: drive want to search other drive i.e. E: drive


Report •

#19
September 17, 2014 at 17:25:48
That was the way I wrote it,
"that file which has stored in same location where batch file stored"
"I want to search string and copy files in the folder "
That was the scope of my batch, as written. I will need to know the scope of your search: One entire drive? Multiple drives? Just subfolders of specific folder? A list of folders? Let me know, and I'll try to fix it. The script I presented only searched the directory it was running from.

Report •

#20
September 18, 2014 at 04:57:09
Thanks 'nbrane'. You are very nice...

Below is my requirement.

1. I want to search files throughout entire drive. As per my choice I can change the drive name i.e. C:\ or D:\ or E:\.

So please let me know which part of code need to change for drive location.

2. File name may be same but it will not overwrite and extension of file will preserve. For unique identification you can suffix creation of date timestamp if you can or unique number as per your convenience.


message edited by ajayrohan


Report •

#21
September 18, 2014 at 18:57:23
Modified #17, as per. Not tested...

Report •

#22
September 22, 2014 at 08:01:24
I have executed script but not found any result. I have tried various ways to enter drive location but failed.

For e.g C:\, C:, C (With or without quotes)

Below Message in Command Screen.
drive: C:
The filename, directory name, or volume label syntax is incorrect.
string to find - no dbl quotes, not case sensitive:


Report •

#23
September 22, 2014 at 18:35:48
Once again, my mistake. This line (line #11):
cd /d src:\
should have been:
cd /d %src%:\

see if that fixes the problem. You have been very patient!


Report •

#24
September 25, 2014 at 08:40:17
Hi nbrane,

I am very much thankful to you that you are putting your best efforts to resolve my issue.

I have tested your code.

Below Steps I followed.

1. Created Test.txt file and saved in C:\
2. Created Second file with same name Test.txt and saved in C:\XYZ\Test.txt
In both files string is same i.e. "Hello"
3. After executing batch file I expect two files in “C:\abc” folder.

Output:
I found only one file in C:\abc folder i.e. that file which is stored directly in "C:\" drive location.
I said in my earlier comment I need to search entire drive location means it include sub folder as well.

Please modify your code as per my requirement.


Report •

#25
September 30, 2014 at 07:41:43
Hi nbrane,

I am waiting for your reply. Please help me.


Report •

#26
September 30, 2014 at 18:33:02
Here is the "work" portion of #17, with the fix:
...
if not defined d2 goto :eof
>list.bat echo echo preparing to copy as per the following:
>>list.bat echo @more +3 list.bat
>>list.bat echo @pause
for /f "tokens=*" %%a in ('dir /b /s /a-d') do (
for /f "tokens=1-3 delims=/ " %%b in ("%%~ta") do set z=%%d%%b%%c
if !z! geq %d1% if !z! leq %d2% (
find /i "%x%"<"%%a">nul&&(
for /f "tokens=2-8 delims=/.: " %%J in ("%date%/%time%") do set stamp=%%L%%J%%K%%M%%N%%O
::Edit 9/18: nxa to pnxa for full path
>>list.bat echo copy /-y "%%~pnxa" "%dest%\%%~na!stamp!%%~xa"
)
)
)
:: optional: call list.bat
call list.bat
::====== end

This line was changed - added /s to the commandline:
for /f "tokens=*" %%a in ('dir /b /S /a-d') do (

Theoretically, this should fix, but giving my batting average so far... I'll wait to see.


Report •

#27
October 8, 2014 at 06:45:09
Hi nbrane,

I have tested but its not working. Please check ..

********************
drive: Y
string to find - no dbl quotes, not case sensitive: workstation
earliest date in format yyyymmdd 20140101
latest date in format yyyymmdd 20141010
The system cannot find the file specified.
The system cannot find the file specified.

********************

message edited by ajayrohan


Report •

Ask Question