Solved Renaming file based on reference in the file

April 15, 2014 at 06:18:26
Specs: Windows 7
I have a .txt file that contains a file reference on line 3. I would like to change the filename and extension of this .txt file to the reference that is on line 3 and the extension to .xml.

For eg: "Test.txt" is the file name of the file, which contains a file reference of "0000123" on line 3. I would like this .txt file to be renamed: "0000123.xml", and any other .txt files within the same folder to be renamed in the same way.

Can anyone help?


See More: Renaming file based on reference in the file

Report •

✔ Best Answer
April 15, 2014 at 10:20:53
Hmm... again! I tested again my script and that worked looping through the folder, renaming correctly. Anyway try the following variant slightly modified (replacing tokens=2 with 3 in your case)

@echo off
for %%i in (*.txt) do call :REN "%%i"
exit /B

:REN
for /F "skip=2 tokens=2 delims=><" %%j in ('type "%~1"') do (
  ren "%~1" "%%j.xml"
  exit /B
)



#1
April 15, 2014 at 08:16:11

.txt and .xml are extensions that apply to different file structures. Are you sure you want to change filename's extention?

That stated, what is *exactly* the structure of the reported line 3?


Report •

#2
April 15, 2014 at 08:23:49
Thanks for quick response..

Yes, need to convert from .txt to xml.

Each .txt file contains a line on row 3 as follows:
<FileReference>0001234</FileReference>

It is the filereference number that I would like to use as the file name, this file reference number will be different for each .txt file in the folder.

Thanks


Report •

#3
April 15, 2014 at 08:53:59
@echo off
for %%i in (*.txt) do call :REN "%%i"
goto :EOF

:REN
for /F "skip=2 tokens=2 delims=><" %%j in ('type "%1"') do (
  ren "%1" "%%j.xml"
  goto :EOF
)
goto :EOF


Report •

Related Solutions

#4
April 15, 2014 at 09:29:49
Thanks - this seems to be changing the file name to "FileReference" not the number within the field.

The lines in the .txt file are as follows:

<example exml code text line 1>
<example exml code text line 2>
<FileReference>000123</FileReference>
<example exml code text line 2>

Its the 000123 that I need in the filename.

Also, the batch file does not seem to be loop though the folder. It changes one file, but not the others.

Thanks again for your help



Report •

#5
April 15, 2014 at 09:40:45
Hmm... I tested my script before posting it and it seemed to work correctly. Now I am going to revise the code to solve the issue.

Back as soon as possible and sorry for the problem I caused.


Report •

#6
April 15, 2014 at 09:43:54
No problem, actually I just changed the 'tokens' to 3 and now this is picking up the correct field in the .txt file.

It works for one file at a time now, but that is ok, although would be great if it could loop through the files.


Report •

#7
April 15, 2014 at 10:20:53
✔ Best Answer
Hmm... again! I tested again my script and that worked looping through the folder, renaming correctly. Anyway try the following variant slightly modified (replacing tokens=2 with 3 in your case)

@echo off
for %%i in (*.txt) do call :REN "%%i"
exit /B

:REN
for /F "skip=2 tokens=2 delims=><" %%j in ('type "%~1"') do (
  ren "%~1" "%%j.xml"
  exit /B
)


Report •

#8
April 16, 2014 at 00:57:30
Thats great, works perfectly - thanks for your help!

Report •

Ask Question