Rod
Rod

Reputation: 1

Delete specific content from a text file using windows batch script

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.

Upvotes: 0

Views: 1872

Answers (4)

Rod
Rod

Reputation: 1

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

Upvotes: 0

Magoo
Magoo

Reputation: 79982

@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

Upvotes: 1

Nat Thulke
Nat Thulke

Reputation: 95

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

Upvotes: 0

Nat Thulke
Nat Thulke

Reputation: 95

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.

Upvotes: 0

Related Questions