0

I have bulk html files within several folders. And the problem is i have to remove tags from those html files. I can't figure out how to do that.. I searched the internet and found nothing. Is there any cmd script that will open every html file and remove the tags or replace with any other tag of my choice? Thanks for the help.

user3551620
  • 169
  • 1
  • 5
  • 17
  • 3
    Any decent text/code editor will do that. For example, Notepad++. The feature you're looking for is "Find & Replace". On Linux, you could probably do it on the command line, too. – domsson Mar 07 '17 at 12:56
  • Yeah i tried notepad++ but the problem is that i have the html files in sub folders.. and there are like 100+ html files. – user3551620 Mar 07 '17 at 12:58
  • Do not use `cmd`/batch files for such tasks! If you want to write a script, use a language that natively supports HTML, like JavaScript, for example... – aschipfl Mar 08 '17 at 18:04

1 Answers1

2

Well.. after 8 hours of my personal experiments, it should work now as @user3551620 wanted. I made updates in my answer due to change of specification of question, where user told me he wants to run this script in system files where I treat with problem of work with path that contain spaces as: "Program Files (x86)" ... Remember that If you run this script in system files, you should do it as an administrator, due to creating new temp file and other writings in script that need permission to do it.

Now correct code should work as follows:

setlocal enabledelayedexpansion

::get path
SET mypath=%~dp0*.html

set /p old=old string ?
set /p new=new string ?

::cycle for every file of specific folder where you have this script and all html files
for /f "delims=" %%f in ('dir /b /s "%mypath:~0,-1%"') do (

    ::copy to temp file line by line text with replacing of specific tags
    for /f "delims=" %%a in ('type "%%f"') do (
        set str=%%a
        set str=!str:%old%=%new%!
        >> tempfileXXX.txt echo !str!
    )

    ::empty the folder from where you copied
    break>%%f


    ::cycle over every line of temp file to copy back to old file
    for /f "delims=" %%a in (tempfileXXX.txt) do (
        set str=%%a
        >> "%%f" echo !str!
    )

    ::clear tempfile
    break>tempfileXXX.txt

)

::delete temp file
del tempfileXXX.txt
pause

You have to run this script.bat in the folder where you need do your actions. After running script will ask you for adding string which tags should be replaced and second with what tag it should be replaced. Remember that when using "<" and ">" signs for creating tag, you should enter before every "<" sign special "^" character. This will replace all html tags with php tags in arr .html files in your folder.

Example of usage: enter image description here

Additional problems but not big:

  • you must rerun script for closing tags , but you can now programm it on your own
  • after script copy line by line you can notice, that script will remove newline characters, but that's not so huge problem to find out how
  • I believe that script that I made above can be made smarter in way of no need of creating temp file.. more experienced programmers could comment below this post for this issue..
Marek Bernád
  • 481
  • 2
  • 8
  • 28
  • 1
    It is amazing what cmd shell programmers must go through to get the same result as a file rename and one line of `sed`. – lit Mar 07 '17 at 16:27
  • Hey @marek Bernad can you tell me why did you used dir /b /a-d C:\Path\To\Your\Folder \*.html'? if i have to place the bat file in every folder.. do i still have to set the path too? – user3551620 Mar 07 '17 at 17:55
  • well, I am glad that it helped you, but about that options /b, /a-d ... while I was trying to find answer, I tried some options here but forgot to remove them ... /b option is OK, it stands for bare format... if you intrested to know more about options, this is perfect site for it: http://www.computerhope.com/dirhlp.htm ... yes I programmed that only for specific folder, not subfolders, but just see that link and if you will try option /S it stands for all subdirectories too ... well give there that option and it is what you needed – Marek Bernád Mar 07 '17 at 18:30
  • If my answer is final answer and most correct for you, you can check this answer as correct answer for question below voting... – Marek Bernád Mar 07 '17 at 18:35
  • it must work bro, I tried it and all tags were replaced that I tried... I tried 10 different cases... I dont understand what concretely dont work for you... – Marek Bernád Mar 08 '17 at 06:42
  • I made a folder and put an .html file into it. then i put some tags into it and tried to replace them. nothing happend. – user3551620 Mar 08 '17 at 11:02
  • maybe you´ve just bad specified the path where script should work with... here you have an evidence that it works and how: https://youtu.be/ntj1J5mSZxI – Marek Bernád Mar 08 '17 at 12:29
  • I have to specify the path too? I thought if i just put the script in the folder it will automatically get the folder path.. Can you tell me how to do this? and can you tell me how to fix a tag in it so it wont ask me again and again? – user3551620 Mar 08 '17 at 14:19
  • Ok, I updated the script, now it should work wherever you put your script, that folder should be processed where script lays... but I don´t understand that fix about asking again and again for tag... naturally, you run script for changing all "
    " to "

    " for example and this will change in all the .html files in that folder, but if you want to change another type of tags, you have to rerun script... what did you mean by your last subquestion about to not ask you again and again? Script asks you just for old tag and new tag, but not more times...

    – Marek Bernád Mar 08 '17 at 15:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137586/discussion-between-user3551620-and-marek-bernad). – user3551620 Mar 08 '17 at 19:32
  • well, I´ve updated answer, now it should be correct answer for you to call script in system folders – Marek Bernád Mar 09 '17 at 15:07
  • I have already written a script with a similar replace-technique. The problem i am facing is that comments (like "") lose their ! (so they look like "<-- example -->"). I can remove all "<--" and "-->", but the comment itself is still in the document. Any idea how to solve this? (Would also start a new question about it) – timlg07 Oct 21 '18 at 18:09
  • Sorry, have no much time now, but interesting topic to solve it could be [here](https://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters). And an interesting idea is something like this `set "print=for /f %^" in ("""") do echo(%~""` and `%print%%%a` – Marek Bernád Oct 22 '18 at 12:56