I have multiple Invoice_Term1........txt files in a directory, e.g. Invoice. The content of each of file contains:
Invoice No : B23335 ...
Account Code : ABDU16 ...
How do I loop through each txt file, FINDSTR Invoice No : , but copy B23335 and FINDSTR Account Code : , and copy ABDU16?
Concerns are that each Invoice No and Account Code are different in character length and there are other words after the number and code strings, (represented as ... in my above example content), separated from it with a space character.
The intention is to rename the files, using the above example, to Invoice_Term1.......B23335_ABDU16.txt.
I tried using AI, but definitely need a lot of changes, or even a full rewrite.
@ECHO OFF
FOR %%F IN (*.txt) DO (
FINDSTR /R /C:"Invoice No :" /C:"Account Code :" \"%%F\" > temp.txt
SET /P KEYWORD1=<temp.txt
SET /P KEYWORD2=<temp.txt
FOR /F "tokens=3 delims= " %%A IN ("%KEYWORD1%") DO (
FOR /F "tokens=3 delims= " %%B IN ("%KEYWORD2%") DO (
REN \"%%F\" %%A%%B.txt
)
)
DEL temp.txt
)
I need some expert assistance.
Noting that
Invoice No : B23335is in the before-file andB233335is in the desired new name (one extra3- presumed typo)It is important to specify the form of
......since the presence of, eg. spaces in the filename may affect the code that's required to process it.Note that renaming a file to samefilenameplusextracharacters.ext is likely to cause
for…(mask)…to reprocess the file. Thefor /f…('dir…method builds the filelist first in memory, then processes the filelist.Switch directories [pushd]
dir /b /a-dproduces a list of filenames-only.Read each line from file, Butt-up the first 3 tokens as, eg.
InvoiceNo:for testing and grab the fourth token (using the default delimiters) for the variables.Switch back to original directory [popd]
The required REN commands are merely
ECHOed for testing purposes. After you've verified that the commands are correct, changeECHO RENtoRENto actually rename the files.