Articles

Solved Batch file to Rename and Move files based on text

December 4, 2012 at 14:59:42
Specs: Windows Server 2003 & 2008

I'm in need of a batch file that will do the following:

File name I_ABCDE.XYZ
I_ is a constant
ABCDE are variable
XYZ corresponds to the numeric value of the date (001=Jan 01, etc).

Files are a single line, variable length.

If the file contains the text "Duplicate Dispatch" in anywhere in the file, rename the file to "K_ABCDE.XYZ" and move it to C:\NEWDIR

If it does not contain the text "Duplicate Dispatch" move it to C:\NEWDIR without renaming.

Any help will be appreciated.


See More: Batch file to Rename and Move files based on text

Report •


#1
December 4, 2012 at 18:37:21
✔ Best Answer

This might have some holes, and it's not tested:
::===== begin batchscript
@echo off & setlocal
for /f "tokens=1* delims=_" %%a in ('dir /b I_*') do (
find "Duplicate Dispatch" %%a && move %%a c:\newdir\I_%%b || move %%a c:\newdir\K_%%b
)
::==== end batchscript

Report •

#2
December 5, 2012 at 10:19:10

Thanks!! It was close enough and just needed a bit of tweaking.

I ended up with:

::===== begin batchscript
@echo off & setlocal
for /f "tokens=1*" %%a in ('dir /b I_*') do ( find "Duplicate Call Request Recd" %%a && move %%a c:\newdir\K_%%a || move %%a c:\newdir\%%a
)
::==== end batchscript

Thanks again!


Report •

Related Solutions


Ask Question