batch file to rename based on file contents

Microsoft Windows xp professional w/sp2
May 14, 2010 at 09:12:15
Specs: DOS
I'd like to rename files in a directory based on the contents of the file matching the str's below. If it found a match it would rename the file to a set name such as 527.X12. The str's I'm searching for could be in any position in the file.

:: inbound files
set inputfiles=C:\EDI_Files\Inbound\InProcess\IN*.X12
set string527="ST*527*"
set string824="ST*824*"
set string846="ST*846*"
set string888="ST*888*"
set string940="ST*940*"
set string997="ST*997*"

Thanks!


See More: batch file to rename based on file contents

Report •

#1
May 14, 2010 at 09:31:18
You use the FIND command.

Should post this in the Programming Forum.


Report •

#2
May 17, 2010 at 08:00:58
Need some help with the syntax...thanks.

Report •

#3
May 17, 2010 at 12:40:11
@echo off >temp.bat & setlocal enabledelayedexpansion
:: read in the search strings (numeric part only)
set xx=0
for /f %%a in (strings) do (
set /a xx+=1
set v!xx!=%%a
)
for %%a in (*.x12) do (
for /L %%i in (1 1 !xx!) do (
set ss=ST.*!v%%i!
findstr /r "!ss!" %%a
if %errorlevel% equ 0 >> temp.bat echo move %%a !v%%i!.x12
)
)
::------ end
not tested yet. file "strings" looks like this (example only):
527
997
826

Report •

Related Solutions

#4
May 17, 2010 at 13:30:01
nbrane - almost there...

It's doing the move for every file type in strings.txt for each file in the inprocess dir. If the the \inprocess dir only has one 527 file INxxxxx.X12 in it I only need it renamed to 527.X12.

:: inbound files
:: read in the search strings (numeric part only)
set xx=0
for /f %%a in (strings.txt) do (
set /a xx+=1
set v!xx!=%%a
)
for %%a in (C:\EDI_Files\Inbound\InProcess\IN*.x12) do (
for /L %%i in (1 1 !xx!) do (
set ss=ST.*!v%%i!
findstr /r "!ss!" %%a
if %errorlevel% equ 0 >> temp.bat echo move %%a !v%%i!.x12
)
)
::------ end
strings.txt looks like this:
527
824
846
888
940
997

temp.bat looks like this:
move C:\EDI_Files\Inbound\InProcess\INW01271.X12 527.x12
move C:\EDI_Files\Inbound\InProcess\INW01271.X12 824.x12
move C:\EDI_Files\Inbound\InProcess\INW01271.X12 846.x12
move C:\EDI_Files\Inbound\InProcess\INW01271.X12 888.x12
move C:\EDI_Files\Inbound\InProcess\INW01271.X12 940.x12
move C:\EDI_Files\Inbound\InProcess\INW01271.X12 997.x12
move C:\EDI_Files\Inbound\InProcess\INW01272.X12 527.x12
move C:\EDI_Files\Inbound\InProcess\INW01272.X12 824.x12
move C:\EDI_Files\Inbound\InProcess\INW01272.X12 846.x12
move C:\EDI_Files\Inbound\InProcess\INW01272.X12 888.x12
move C:\EDI_Files\Inbound\InProcess\INW01272.X12 940.x12
move C:\EDI_Files\Inbound\InProcess\INW01272.X12 997.x12
move C:\EDI_Files\Inbound\InProcess\INW02051.X12 527.x12
move C:\EDI_Files\Inbound\InProcess\INW02051.X12 824.x12
move C:\EDI_Files\Inbound\InProcess\INW02051.X12 846.x12
move C:\EDI_Files\Inbound\InProcess\INW02051.X12 888.x12
move C:\EDI_Files\Inbound\InProcess\INW02051.X12 940.x12
move C:\EDI_Files\Inbound\InProcess\INW02051.X12 997.x12
move C:\EDI_Files\Inbound\InProcess\INW09221.X12 527.x12
move C:\EDI_Files\Inbound\InProcess\INW09221.X12 824.x12
move C:\EDI_Files\Inbound\InProcess\INW09221.X12 846.x12
move C:\EDI_Files\Inbound\InProcess\INW09221.X12 888.x12
move C:\EDI_Files\Inbound\InProcess\INW09221.X12 940.x12
move C:\EDI_Files\Inbound\InProcess\INW09221.X12 997.x12


Report •

#5
May 17, 2010 at 16:02:49
apologies, i should have coded for multiple occurances of
the string. i think if you add /m switch it will work the way we want:
findstr /r /m "!ss!" %%a

since it should only output one filename per find.


Report •

#6
May 18, 2010 at 07:56:02
Hmm. It's still writing out the each input file 6 times.

:: inbound files
:: read in the search strings (numeric part only)
set xx=0
for /f %%a in (strings.txt) do (
set /a xx+=1
set v!xx!=%%a
)
for %%a in (C:\EDI_Files\Inbound\InProcess\IN*.x12) do (
for /L %%i in (1 1 !xx!) do (
::set ss=ST.*!v%%i!
set ss=ST*!v%%i!*0001
::findstr /r "!ss!" %%a
findstr /r /m "!ss!" %%a
if %errorlevel% equ 0 >> temp.bat echo move %%a !v%%i!.x12
)
)


Report •

#7
May 18, 2010 at 08:12:36
ok, you have six strings, and it's apparently finding all six
in each file, it seems like. Are all six strings in the files?
If so, then you either need just the first one, or a better way
to discriminate which string is wanted... let me know, maybe
post contents of one of the files.

Report •

#8
May 18, 2010 at 08:25:12
I'd like to have findstr search for "ST*527*0001" and rename the file to 527.X12 if it finds a match in the directory.

Here's the contents of one of the input files:
ISA*00*NONE *00*NONE *ZZ*DTDN *ZZ*6027719100 *100127*1754*U*00401*325813919*0*T*
GS*MD*S36TST*6027719100*20100127*1754*325813919*X*004010
ST*527*0001
BR*00*DE*20100127******131704
N1*KA**M4*SMS**FR
LIN*N*FS*5360000076553
N9*TN*SL470100270AAE
RCD*1*12*EA
G62*17*20100227
....
...


Report •

#9
May 18, 2010 at 08:44:39
oh, i think i see:
if %errorlevel% equ 0 >> temp.bat echo move %%a !v%%i!.x12

change it to:
if !errorlevel! equ 0 >> temp.bat echo move %%a !v%%i!.x12

(of course, you have this at top:)
setlocal enabledelayedexpansion


Report •

#10
May 18, 2010 at 09:18:06
It works now. Thanks a lot nbrane!

Report •

Ask Question