Rename txt-files using part of first textline in file

Viewed 176

I have a need of a code used in a batch file that renames bankfiles created from SAP (I am a SAP-man), stored in a location on the server.

Problem: All bank-files get a name from a sequence table in SAP (date + number). Before I send them to the bank they have to have a certain name structure.

I have a code and this has worked fine up to now. The problem now is that i send a "batch" (several) of files from SAP and they are named randomly.
In the first line of each file there is a unique batch ID, that is a bank sequence number and the files has to be named in this order. I have done a lot of VBA programming, but i am not to strong in this subject.

Needed solution: What i need is that for each file it should read the first line of the file and fetch position 71 and 4 positions forward, save this in a variable and add it to the end of the file name.

Example: If the original file name from SAP is 20160301-001.txt I want to rename it to "P.00934681758.005.TBII.00000xxxx.txt" (where "xxxx" is the position 71 to 74 in the first line of the file.

It looks through a lot of bank directories today, but below you can find todays code (that work, except that in this code it renumbers "a", starting with no 1 - never more than 9 files) and i want to replace it with these 4 digits from the file:

Todays name: P.00934681758.005.TBII.00000!a!.dat ("a" beeing a variable)

New name: P.00934681758.005.TBII.00xxxx.dat ("xxxx" the digits from the file)

Todays code (part of it - showing 2 "scanned" directories):

    @echo off & setlocal EnableDelayedExpansion 

REM Start by renaming all files in each folder to the namerule of the bank

cd PL270\UT01

set a=1

for /f "delims=" %%i in ('dir /b *') do (
    if not "%%~nxi"=="%~nx0" (
        ren "%%i" "P.00934681758.002.TBII.00000!a!".dat 
        set /a a+=1
 )
) 

cd..
cd..\PL570\UT01

set a=1

for /f "delims=" %%i in ('dir /b *') do (
    if not "%%~nxi"=="%~nx0" (
        ren "%%i" "P.00934681758.005.TBII.00000!a!".dat 
        set /a a+=1
 )
) 

The code is run in a *.bat-file today, scheduled every 5th minute on the server.

All help will be VERY much appreciated:-)

B.r. Solve

2 Answers
Related