0

A very simple one, see if you can give me a hand...

I have multiple files, extension .tfm (which is actually a simple text file). The format inside each of them is always like this:

-2.90913045e-003   -6.36480816e-001   -7.71287046e-001    2.09995523e+001
-7.30901280e-004    7.71291457e-001   -6.36481700e-001    5.08653785e+002
 9.99995501e-001   -1.28787360e-003   -2.70899210e-003    1.04250041e+003
 0.00000000e+000    0.00000000e+000    0.00000000e+000    1.00000000e+000
Units: mm
# rotation x: 9.02438607e+001
# rotation y: -5.04696056e+001
# rotation z: 9.02618771e+001
# translation x: 2.09995523e+001
# translation y: 5.08653785e+002
# translation z: 1.04250041e+003

All I want to do is to remove the rotation, translate writing and stuff and leave it like this:

-2.90913045e-003   -6.36480816e-001   -7.71287046e-001    2.09995523e+001
-7.30901280e-004    7.71291457e-001   -6.36481700e-001    5.08653785e+002
 9.99995501e-001   -1.28787360e-003   -2.70899210e-003    1.04250041e+003
 0.00000000e+000    0.00000000e+000    0.00000000e+000    1.00000000e+000
Units: mm
9.02438607e+001
-5.04696056e+001
9.02618771e+001
2.09995523e+001
5.08653785e+002
1.04250041e+003

At the end, change the format to ".trm" and I don`t have to keep the original file.

Help?

THank you very much in advance.

3
  • Do you have to keep any blank lines that are in the original file? Commented Feb 15, 2016 at 23:33
  • the format of the matrix has to stay the same. but there is no blank lines. Commented Feb 15, 2016 at 23:35
  • Have you tried anything on your own? please post your code... Commented Feb 16, 2016 at 1:29

4 Answers 4

1
@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%r IN (
'dir /b /a-d "%sourcedir%\*.tfm"'
) DO (
 (
 FOR /f "usebackqdelims=" %%s IN ("%sourcedir%\%%r") DO (
  FOR /f "tokens=1-3*delims=: " %%a IN ("%%s") DO (
   IF "%%a"=="#" (
    IF "%%b"=="rotation" ECHO(%%d
    IF "%%b"=="translation" ECHO(%%d
   ) ELSE ECHO(%%s
  )
 )
 )>"%destdir%\%%~nr.trm"
)

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances.

Read the directory for filenames matching *.tfm into %%r

Read each of those files line-by-line to %%s

Examine %%s for tokens using space and : as delimiters. Assign the tokens to %%a..%%d

If %%a is not #, simply reproduce the line, otherwise reproduce %%d (the part following the first 3 tokens) if the second token is one of the keywords.

Destination Filename is constructed from the destination directoryname plus the name-part of the original file + .trm

Sign up to request clarification or add additional context in comments.

Comments

0

I have used this in the past: http://fart-it.sourceforge.net/

FART, Find And Replace Text, works well.

Might be your best bet if you're happy to use 3rd party software.

1 Comment

Just tried using fart with your file example using this code c:\scripts\fart\fart.exe test.txt -aC "# rotation x: " " " And the problem is, it does change the file but leaves a space.
0

Other than FART that I suggested I would recommend a PowerShell Script.

found this page Replace File contents on this site.

So I played around with this:

$content = [System.IO.File]::ReadAllText("test.txt").Replace("# rotation x: ","")
[System.IO.File]::WriteAllText("test.txt", $content)
$content = [System.IO.File]::ReadAllText("test.txt").Replace("# rotation y: ","")
[System.IO.File]::WriteAllText("test.txt", $content)
$content = [System.IO.File]::ReadAllText("test.txt").Replace("# rotation z: ","")
[System.IO.File]::WriteAllText("test.txt", $content)
$content = [System.IO.File]::ReadAllText("test.txt").Replace("# translation x: ","")
[System.IO.File]::WriteAllText("test.txt", $content)
$content = [System.IO.File]::ReadAllText("test.txt").Replace("# translation y: ","")
[System.IO.File]::WriteAllText("test.txt", $content)
$content = [System.IO.File]::ReadAllText("test.txt").Replace("# translation z: ","")
[System.IO.File]::WriteAllText("test.txt", $content)

That is giving the desired result, now all you need to do is add a rename command into the script.

Rename-Item c:\scripts\test.txt new_name.txt

So the full script would look like this: (Cleaned up so you can just change the filepath variable)

$filepath = "c:\test\test.txt"
$rename = $filepath.Substring(0,$filepath.Length-4) +".trm"
$content = [System.IO.File]::ReadAllText("$filepath").Replace("# rotation x: ","")
[System.IO.File]::WriteAllText("$filepath", $content)
$content = [System.IO.File]::ReadAllText("$filepath").Replace("# rotation y: ","")
[System.IO.File]::WriteAllText("$filepath", $content)
$content = [System.IO.File]::ReadAllText("$filepath").Replace("# rotation z: ","")
[System.IO.File]::WriteAllText("$filepath", $content)
$content = [System.IO.File]::ReadAllText("$filepath").Replace("# translation x: ","")
[System.IO.File]::WriteAllText("$filepath", $content)
$content = [System.IO.File]::ReadAllText("$filepath").Replace("# translation y: ","")
[System.IO.File]::WriteAllText("$filepath", $content)
$content = [System.IO.File]::ReadAllText("$filepath").Replace("# translation z: ","")
[System.IO.File]::WriteAllText("$filepath", $content)
Rename-Item $filepath $rename

Comments

0

thanks for everyone`s help. At the end I did use something similar to FART but it was called TextCrawler. Worked very well...

THanks once again. Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.