writing the a local file using parameters on command line - batch-file

I am trying to write a little project thing to get my javascript skills up. The goal is for, when using curln, the .cmd file will write %~dp0\#temp with the curl %1 (the curled html result of the first parameter)
expected with curln "https://www.google.com": (long html)
actual: "<< was unexpected at this time."
I have tried looking things up, this was a mesh of what I found online. I have tried using < and << as, a beginner, I don't understand the difference.
Code:
%~dp0\#temp << cmd curl %1
I would like the output of curl to be written to the current directory's (%~dp0) file named #temp. %1 is a preset *.cmd parameter variable that is shown in this example:
example "this is %1"

> and >> are output. The first creates a new file, the second appends to an existing file. < is input. It accepts input (reads) into a program from another source, which is typically a file or the output of another program. It doesn't work with writing to files.
You're most likely looking for curl %1 >>"%~dp0#temp" instead. You don't need the cmd in front of it, as you're already in a command window when the batch file executes.

For a simple transfer I use repeatedly using curl the minimal call is
curl -o "%~dpnx0" remote.url
That ensures the fileName and eXtension are saved in the current DrivePath
There is no error checking or security communication so is only useful for a single .html or .js or .pdf etc.
Thus a complex call can run to dozens of lines.

Related

Batch File Calls VB Script & Passes Filename as String Only

I have a batch file that calls a VB Script file. This VB Script comes from MS Office Suite and is located in the Installed Folder. My file calls the VB Script and pipes it to a file using the > symbol. This works if your redirect is a string. I was wondering how to redirect to a file using a variable.
I'm only including a small fraction of the code showing how I want it to work.
This, as written, works. However I want my batch file to use a variable instead of the text in quotes. In fact, my batch file creates the path depending on where the batch file is located.
::Create file and display on screen
Echo Writing Information to File: "OfficeStatus.txt"
Call cscript ospp.vbs /dstatus > OfficeStatus.txt
Echo.
When written as shown below, it does not work:
::Create file and display on screen
Echo Writing Information to File: "OfficeStatus.txt"
Call cscript ospp.vbs /dstatus > %_sLogFile%
Echo.
I don't know if the issue is because of the redirect not able to handle a variable or not. An example of what might be in the variable:
_sLogFile=E:\UserName\Documents\Status\OfficeStatus.txt.
Any thoughts would be great.
The variable, by the way, correctly contains the path and filename... so that's not the issue.
it does not work means did not do what I expected and unfortunately does not tell us what it did do.
Did it crash the system? Create an error message (If so, what message)? Create an output file in an unexpected place??
Personally, I'd suspect what you have concealed as username. Without knowing precisely what that text is, we start guessing, which isn't a logical approach. It doesn't have to be the real username, substitute Fred Bloggs for the real name to mask it if necessary.
I believe that username may either be %username% to retrieve the name from the system, or a real literal. The problem with this is that such names often contain spaces, you'd need to "Enclose the full filename in quotes".
See - had you told us _sLogFile=E:\Fred Bloggs\Documents\Status\OfficeStatus.txt it would all have been a lot clearer - presuming that my guess as to the cause of the problem is correct.
Perhaps you should look for files named like E:\Fred which is where the expected output may have ended up.

Batch File Command throwing error in loop

Here is the situation. I need to "type" a .sj files content, then save it as an .js file. This is not the same as a rename, as the encoding is different. I am very new to Batch File syntax, but proficient with other programming languages. Here is what I have tried:
call for %%i in (.\*.js) do type %%i > %%i.js
But this is giving me the "%%i is unexpected at this time" Error.
If you need me to provide more insight, I will be happy to.
BACKGROUND: Trying to use JSDoc3 on .sj files but the encoding is not compatible. Using an encoder did not work either. What did work is copying and pasting the contents into a new file with encoding UTF8. But like I stated, a program like UTFCast did not work.
Two percent signs without anything in between (in a batch file) are treated like a single percent sign in a command (not a batch file).
Moreover CALL command enables a user to execute a batch file from within another batch file.
So from cmd you need to run only below..
for %i in (.\*.js) do type %i > %i.js
if you want to learn more...
for command
call command

Bat file prints multiple statements

I am invoking bat command in C++.
The command below writes to console "connecting to bootrom: connected . writing 0x001A13".
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit")
but when i execute same command to dump the above string into file like this:
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit">>D:\abc\13oct\tempFile.txt");
It appears that the temp file is having multiple instances of connected . writing 0x001A13 in tempFile.txt
Does any body point me an appropriate fix for this.
Thanks in advance!
OK, I hope I understood this well: The same output you get once if the first command appears twice in the temp file.
That's probably because you're using >> for redirection, which doesn't replace the ouput file, but appends to it.
That means, if you execute echo Test >> tempfile.txt twice, it will have two lines reading Test.
If you want to save only the ouput of the last command to the file, use > instead of >>.

batch language tutorials - running simple programs that rely on .bat file

I am just starting to get a handle on batch programming.
To date, I've been copy/pasting into the MS-DOS command prompt from a text editor. Some of these copy pastes are getting large. Im sure there is a better way to go about this, ie. writing a line in command prompt that calls other text files (effectively doing the work of copy pasting).
Are these external files going to be .bat (which are just text that could also be put directly into the command prompt?) or .txt or something else?
I am mainly looking into this so that I can get into reusing code and getting into looping.
Are there any tutorials someone would recommend to get my acquainted with these topics?
Thanks for any help.
You can name a text file .bat or .cmd (the latter if you know it's only usable as a Windows batch file) and put commands into it, line by line.
You can run such files by typing their name at the command prompt when you're in the directory where they reside (or if they are contained in one of the PATH directories).
By default the behavior will match pretty much exactly with what you would type by hand. You'll see what commands are executed as well as their output. For example the following batch file (saved as test.cmd here)
echo Hello World
dir /b *.cmd
yields the following output when run
> echo Hello World
Hello World
> dir /b *.cmd
date.cmd
foo.cmd
test.cmd
x.cmd
y.cmd
You can suppress the output of the command being run by including the line
echo off
in your batch file. Prefix it with an # to suppress command output for that line in particular, but ever subsequent command won't be echoed:
#echo off
If other concrete questions arise, feel free to ask.

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources