How to rename files based on a string in the file?

Viewed 44

I want to rename a couple of thousands Files. The content of the file is text and i like to rename the file after the x string in x row. Is this possible? I am on a windows system. For Example: Here is the content of the file i have to rename:

Blender v2.78 (sub 0) OBJ File: '' 
# www.blender.org
mtllib Grate-001.mtl

I like to rename the file after the 3 row and safter the 7th character? How is this possible as a batch file or a powershell command?

I posted an answer but i have a little problem with it. Some files contain in the 3rd row a "(" or a "#"I do not know what is the cause is there. i got the error message with:

")" cannot be processed syntactically at this point.

Here the file that will make the error message:

# Exported from MakeClothes (TM)
# author Unknown
# license AGPL3 (see also http://www.makehuman.org/doc/node/external_tools_license.html)
# homepage http://www.makehuman.org/  

Can someone help with this?

1 Answers

I found that one here:

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\example-path"
SET "destdir=c:\errror-destination"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*.obj" '
 ) DO (
 SET "filename=%%a"
 FOR /f "tokens=1*delims=[]" %%h IN ('find /v /n "" "%sourcedir%\%%a"') DO (
  IF "%%h"=="3" SET "first=%%i" &CALL :rentxt

 )
)
)>"%outfile%"

GOTO :EOF

:rentxt
FOR %%p IN (%first%) DO SET "part1=%%~p"

REN "%sourcedir%\%filename%" "%part1%.obj"
GOTO :eof
Related