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.
Related
I'm very new to .bat files and have excitedly created some to copy, move and rename documents.
Despite searching, I'm getting stuck with a more complex command, largely because the document I'm trying to modify is pipeline delimited rather than 'normal' csv...
My question: Can I, and if I can - how do I take an existing pipeline delimited csv that always has the same number of columns and add a column onto the end with todays date (DD/MM/YYYY) in it for every row?
$ awk -F, 'NF>1{$0 = "\"YYYY-MM-DD\"" FS $0}1' file
sed 'N;s/","/","YYYY-MM-DD HH:MM:SS","/5' file
I cant seem to get anything to even modify the document at the moment :-(
Your batch attempt isn't that bad:
#echo off
for /f "delims=" %%a in ('type "file.csv"') do (
>>"fileout.csv" echo.%%a|%time%
)
Just a few adjustments:
#echo off
(for /f "delims=" %%a in (file.csv) do (
echo(%%a^|%time%
))>"fileout.csv"
for /f is able to process the contents of a file directly (no need for type, although it works fine)
Redirecting (>>) inside the loop is slow, because the file will be opened, and closed every time you write to it (although it works). It's much faster to only once open/close the file (especially with large files).
echo. is not secure (although it works fine in most circumstances), best option is echo(.
The pipe, | is a special character and in this case needs escaping with the caret, ^.
Note: for /f skips empty lines, so they will not be in the new file. Same with lines, that start with ; (default EOL)
Edit for "adding |Date to the Header":
#echo off
<file.csv set /p header=
>fileout.csv echo %header%^|DATE
(for /f "skip=1 delims=" %%a in (file.csv) do (
echo(%%a^|%date%
))>>"fileout.csv"
<file.csv set /p header= is a way to read just one (the first) line of a file to a variable. Write it back with the new data appended (or leave it unchanged - your comment isn't quite clear about that). Use skip=1 to skip the first line with further processing.
Don't forget to change ))>"fileout.csv" to ))>>"fileout.csv".
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.
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..
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
Currently I have a large number (about 10000) of images in a directory (no subfolders), and I need to catalogue them in a csv file. The image filenames are laid out as below:
p000001_06.jpg
p000001_05.jpg
p000001_04.jpg
p000001_03.jpg
p000001_02.jpg
p000001_01.jpg
f000005_11.jpg
f000004_11.jpg
f000003_11.jpg
f000002_11.jpg
f000001_01.jpg
Taking the first image as an example, I would need to get it into the following 5-column format:
p000001_06.jpg,p000001_06,p,000001,06
I have put together the script for the first two columns (see below):
(for %%F in (*) do #echo "%%~nxF",%%~nF) >imageExport.csv
However, I have been floundering around trying to get the rest of it right. I'm pretty sure that it is to do with delimiters and tokens, and I have been tinkering and tinkering but am just not getting it. Would really appreciate the help, as it is not my area of expertise. I also need to work out how to add column headings, but if that confounds matters it's not pressing. Many thanks
delimiters and tokens don't help here. But as your files all have a common format (Xnnnnnn_nn.XXX) you can use simple string substitution. Also adding a header is easy:
#echo off
setlocal enabledelayedexpansion
REM create header:
echo filename,name,letter,number,whatever>imageExport.csv
(for %%F in (*.jpg) do (
set name=%%~nF
echo %%~nxF,%%~nF,!name:~0,1!,!name:~1,6!,!name:~8,2!
)) >>imageExport.csv