Editing Command Outputs - html

Basically, I am working on a large project, with some new concepts to challenge myself. I was planning on mapping out my computers directories and folders from what folder my document was in through HTML. Well, as it turns out (not surprisingly), I need some other files to do the mapping so that the HTML file can visualize it. I am stuck. Basically, I decided I would create a batch script that looks like this:
echo off
cd %cd%
for /d %%a in (*) do dir /ad /on /s /b "%%a" >> get_dirs.txt
This is a simple but cool script that I discovered at https://grumpybear.wordpress.com/2011/05/22/batch-files-to-list-all-files-and-directories-in-a-folder/ Which describes the components of the script very nicely. The output text file looks something like this:
C:\Users\Jason\Desktop\G3\DCIM\CardboardCamera
C:\Users\Jason\Desktop\G3\DCIM\Facebook
C:\Users\Jason\Desktop\G3\DCIM\Flickr
C:\Users\Jason\Desktop\G3\DCIM\.thumbnails\otg
C:\Users\Jason\Desktop\G3\Download\Adobe Acrobat
C:\Users\Jason\Desktop\G3\Download\Adobe Reader
C:\Users\Jason\Desktop\G3\Download\TrimbleMapBundles
C:\Users\Jason\Desktop\G3\Pictures\Facebook
C:\Users\Jason\Desktop\G3\Pictures\Instagram
C:\Users\Jason\Desktop\G3\Pictures\Messenger
C:\Users\Jason\Desktop\G3\Pictures\Old Phone
This is when the script is placed on the desktop. This is pretty much as far as I got when I came upon a serious problem. I needed to get these files to be displayed in an html document as
C:/Users/etc.
So I was going to edit the echo output, silly me didn't realize that it wasn't using echo for output but instead the dir command. So I tried to edit the output with echo by sending the output as input to a variable. Which I'm sure there is solutions to on Batch - Command output to variable and Save command output to variable, but the answers on those pages are a little vague and somewhat confusing to someone with a little less experience. Plus, I'm not sure it matters, because the directories all come out as one full output, and not line by line (editable line by line). I'm not sure if it is possible to make one line after another in the output file into separate variables, which would be great. It would be helpful if I could make one line of the text document a variable, then the next, and so on. which I could the format my html links using the variables in separate links, by making a different output with something caused by:
echo %dir1%
echo %dir2%
echo %dir3%
echo %dir4%
echo %dir5%
The script could calculate the amount of variables needed by reading how many lines are in the .txt file as seen here:How to count no of lines in text file and store the value into a variable using batch script?.
If anyone can share any insight (I know this is a lot to ask for and a lot of information) I would greatly appreciate it. Mainly just getting the variables line by line is the main part. I think I might be able to get some of the other stuff. Thank You.
--Edit: So, I was kind of thinking, is there a way for the batch script to read the .txt file line by line? It seems there is but no one has really broken down the method. It all seems a little vague to me.
--Edit: I did some more thinking and looking around and came up with this code:
#echo off
set /a file=file
cd %cd%
for /d %%b in (*) do dir /ad /on /s /b "%%b" >> get_dirs.txt
for /F "tokens=*" %%A in (get_dirs.txt) do set /a text=%%A
echo %text%
pause
This script, has something wrong with it, or as batch script output says, "Missing Operator," a bunch of times, then closes the window. I am not sure what is wrong with it but I do think I am getting somewhere. I think it may have to do with the angle brackets when echoing my html code, thinking it's trying to output or redirect the echo. Anyone know how I can solve this problem (assuming that it is the actual problem)?

<and > are commands to redirect in- or output. To disable this behaviour you have to escape ("disarm") those characters with a caret ^:
echo ^<a href="%file%:///%text%"^>%text%^</a^>
Also your second for reassigns each line to the variable (one after the other), resulting in your varaible containing the last line only. Extend the for to contain your echo line:
for /F "tokens=*" %%A in (get_dirs.txt) do (
echo ^<a href="%file%:///%%A"^>%%A^</a^>
)
PS: are you sure set /a file=file does what you want? According to your desired output, you should just use set file=file

Related

How to create multiple html/text files at the same time using CMD/Powershell

Can I ask if anyone knows how to create multiple html files using CMD or Powershell (preferably CMD)?
I have here a long list of book titles and I'd like to create a static page for each.
Here's what I'm hoping to achieve:
To turn this list
thecharacterdesign
stealthisbook
seo2020
giveandtake
originals
accounting101
americangovernment101
anatomy101
architecture101
art101
astrology101
astronomy101
buddhism101
...
into these (inside a folder this time):
thecharacterdesign.html
stealthisbook.html
seo2020.html
giveandtake.html
originals.html
accounting101.html
americangovernment101.html
anatomy101.html
architecture101.html
art101.html
astrology101.html
astronomy101.html
buddhism101.html
...
I am not good with CMD or Powershell, but I believe this is doable?
I used to create batch folders before using CMD, but I'm not sure how to tweak the command below to create a text/html instead:
md thecharacterdesign stealthisbook seo2020 giveandtake originals accounting101
If anyone can help, I'd really appreciate it!
Thanks
EDIT: I need a to create lots of blank html files inside a single folder with the names given above and more.
To create an empty file for each entry of a text file, use a for /f loop. (See for /? for more information)
for /f "delims=" %a in (list.txt) do break>"%a.html"
If you want them to be in a certain folder (has to exist):
for /f "delims=" %a in (list.txt) do break>"C:\desired folder\%a.html"
Note: this is command-line syntax. To use the command within a batch file, use %%a instead of %a (both occurrences)
Simple way of doing what you want in Powershell:
# $titles = #('book','book2')
$titles = Get-Content .\BookTitles.txt
foreach ($title in $titles) {
$fileName = $title + '.html'
New-Item -ItemType file -Name $fileName -WhatIf
}
I've commented out the first part of the code snippet (the line starting with #) and added a new line using the Get-Content in stead.
What Get-Content does is grab each line of the text file BookTitles.txt and add it as a separate entry in the $titles array.
Right now there's a -WhatIf at the end of the line creating the actual files. You can run the code as it is presented and it will tell you what it is going to do. When you feel confident it does what you want, remove -WhatIf and running the code will create the files for you.
In a batch file you can try like this :
#echo off
Set "List=%~dp0List.txt"
Set "MyFolder=%~dp0MyFolder"
If Not Exist "%MyFolder%" MD "%MyFolder%"
#for /f "delims=" %%a in ('Type "%List%"') do (
echo. 2>"%MyFolder%\%%a.html"
)
pause

Read all the csv file in a folder and only showed a single header in the output file

I would like to read all the csv file in the folder and compile it using an awk file. Below is the code that i had wrote:
#echo off
del c_1.csv
setlocal ENABLEDELAYEDEXPANSION
set file2=*.csv
set outputfile=c_1.csv
REM get header:
set /p header=<%outputfile%
for %%i in (*.csv) do (
if not exist %header% (
nawk -f "c_1.awk" *.csv >> c_1.csv
)
if exist %header% (
nawk -f more +1 "c_1.awk" *.csv >> c_1.csv
)
)
echo done!
setlocal
pause
goto:eof
But the header still printed in my output file and it had also printed extra data that is incorrect also. Ur help will be appreciated.Thanks
Will this not do what you want?
nawk "FNR==1 && NR!=1{next;}{print}" *.csv>c_1.csv
Idea taken from here.
EditAs it seems I understood your request wrongly, (I didn't properly read the question and assumed you were concatenating files, but only retaining the header on the first). You appear to be running an awk script, c_1.awk on all csv's in the current directory, if the header of any csv doesn't match the input from outputfile then you're intending to 'compile' the entire file, if it does then you're wanting to bypass that header.
The main problem with your batch-file lies with the fact that if exist doesn't tell you if the content of %header% is empty, for that you'll need If Defined header. That said, as you have already deleted the input file, your set /p command would output an error The system cannot find the file specified. and header will still not be defined.
I think that what you should really do is adjust your awk script such that it takes the header to match as an input parameter. That would be much better than trying to check the content in a different language then run one of two awk commands depending upon that content.

Single HTML tag remove from mass files?

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.
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:
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..

Read and Write HTML file in Batch Script

I'll confess that I was never all that good at writing DOS Batch scripts beyond basic stuff.
I've had need to write a script to read an HTML template file and create a new file from it... Now I can read the file into a variable using:
for /f "delims=" %%x in (template.html) do set fileData=%%x
and I can echo the variable out to see that the html data is in there.
But when I try to write it out using:
echo %fileData%>test2.html
I get an error:
The system cannot find the file specified.
I'm not sure if it has anything to do with the variable containing html tags that are confusing the output to file?
Yep, the < and > tag characters are being evaluated literally by echo %fileData%. Use the delayed expansion style to prevent that from happening.. Put setlocal enabledelayedexpansion at the top of your script and echo !fileData!>test2.html.
As a side note, you might benefit from a batch script heredoc for composing your HTML. If you're comfortable with JavaScript and you're feeling adventurous, it's also possible to interact with the DOM by calling Windows Scripting Host methods in a hybrid script.
One other note: If you
for /f "delims=" %%x in (template.html) do set fileData=%%x
your fileData variable gets reset over and over for each line in template.html, until it ultimately ends up containing only the last line. Batch variables can't (easily) contain multiple lines. You need to do your processing within the loop, something like this:
#echo off
setlocal enabledelayedexpansion
(
for /f "delims=" %%x in (template.html) do (
set "fileData=%%x"
echo !fileData:search=replace!
)
)>test2.html
I've struggled with this for the past ~2 hours, just to discover that it can be done in one line:
type file.html>>otherFile.html
As far as I know, it should work with any file extension (as long as the type command works with it). I'm not sure if this completely suits your needs but it's a pretty neat way to append a file's content to another file.
P.S.: Don't judge me if I made a mistake, I'm also a batch script newbie.

Quick and dirty way to parse a mozilla firefox json file

I want to scrape all of the url's out of my .json bookmark backup that firefox creates and output a .txt file.
Here's a sample of one of the objects located in the file:
{"index":1,"title":"Bookmarks Toolbar","id":3,"parent":1,"dateAdded":1219177758531250,"lastModified":1288873459187000,"annos":[{"name":"bookmarkProperties/description","flags":0,"expires":4,"mimeType":null,"type":3,"value":"Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar"}],"type":"text/x-moz-place-container","root":"toolbarFolder","children":[{"title":"","id":25,"parent":3,"dateAdded":1224693644437500,"lastModified":1236888979406250,"annos":[{"name":"placesInternal/GUID","flags":0,"expires":4,"mimeType":null,"type":3,"value":"{f6066e21-10ff-46a2-af7a-2891f8dca345}0"}],"type":"text/x-moz-place","uri":"http://www.google.com/"}
These objects are comma-separated and should all contain at least one member that contains a string whose value is the url of the bookmark.
Here's a sample of what the .txt file would have in it:
http://www.google.com
http://www.yahoo.com
http://www.etc.com`
Ideally, I'm interested in seeing if this can be pulled off using any scripting tools available within a generic Windows XP "environment".
If Windows can't cut it, what would be the quickest & easiest solution to this?
Is there a website or program that can do pattern matching or regex to parse the file do search & replace before I go install something like Active Perl or Strawberry Perl and write a script for it.
Another way I found is the method at the following site:
http://forums.mozillazine.org/viewtopic.php?f=38&t=1057265&sid=66d981cc79d1ff63644e0cdd5b665a37
Basically you do the following:
(1) Create a firefox bookmark with the following as the location:
javascript:(function(){var E=document.getElementsByTagName('PRE')[0],T=E.innerHTML,i=0,r1,r2;t=new Array();while(/("uri":"([^"]*)")/g.exec(T)){r1=RegExp.$1;r2=RegExp.$2;if(/^https?:/.exec(r2)){t[i++]='['+(i)+']:<a href='+r2+'>'+r2+'<\/a>';}}with(window.open().document){for(i=0;t[i];i++)write(t[i]+'<br>');close();}})();
(2) Open a blank firefox tab.
(3) drag your firefox json file into the blank tab, this should open the json file.
(4) goto your bookmark you created in step 1.
(5) you should have a list of "clickable urls" for all your bookmarks.
If you have Excel, it's probably easy to do a text to columns split
http://office.microsoft.com/en-us/excel-help/split-names-by-using-convert-text-to-columns-HA001149851.aspx
on ". Given the format (order of fields) is always the same, you should have the URLs somewhere near the last column.
I haven't tested this.
NOTE: Verify/correct all below file paths to match your system.
#Echo Off
Rem FFExportBookmarks.bat
SetLocal EnableDelayedExpansion
Set JSONFile="%APPDATA%\Mozilla\Firefox\Profiles\xyz42pdq.default\bookmarkbackups\Bookmarks.json"
Set FavOut="%USERPROFILE%\My Documents\FFBookmarks.txt"
Set JSONTemp="%Temp%\JSONTemp.txt"
Echo.> %JSONTemp%
Set JSONTemp1="%Temp%\JSONTemp1.txt"
Echo.> %JSONTemp1%
For /f "UseBackQ Delims=" %%N In ('Type %JSONFile%') Do (
Set JSONInput=%%N
Rem Filter double " and other delimiters
Set JSONInput=!JSONInput:"=!
Set JSONInput=!JSONInput: =!
Set JSONInput=!JSONInput:^,= !
Set JSONInput=!JSONInput:[= !
Set JSONInput=!JSONInput:]= !
Set JSONInput=!JSONInput:{= !
Set JSONInput=!JSONInput:}= !
For %%K In (!JSONInput!) Do For /f "Tokens=1,2 Delims=:" %%X In ("%%K") Do (
If /i "%%X"=="uri" Echo %%Y >> %FavOut%
)
)
Start "" %FavOut%
It wasn't very quick, but it's plenty dirty!