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.
Related
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
I was trying to put together a .bat file program that presents readable information. Basically, we have a application called vSphere that reads custom scripts directly from a xml file. So there are xml elements that are passed into the the client. Now, I have a similar situation where we are doing the same thing with vCloud. The difference is that vCloud only reads data from a .bat file instead of a xml file.. If I wanted to do such a thing what would that look like.. Below is my psuedo code as I'm not completely familiar with .bat file syntax. Is this how I can present the information to be read by the vCloud client? The client already knows how to read the .bat file, I just need help with a control structure example of a .bat file to tackle this problem.
xml version:
<_type>viss.CustomizationExample</_type>
<changeExample>1341932956</changeExample>
.bat file version:
#echo off
SET ChangeExample=1341932956
if changeExample==""(
echo changeExample==ChangeExample
)
Thank you!
this code may work for you
#echo off
set changeExample=1341932956
echo %changeExample%
This will display the value of changeExample 1341932956 on the cmd screen if you run the batch file is this what you wanted...
Your almost there, just a couple of things.
To access a variable after setting it, without any special circumstances, you need to put %'s at each side of it to tell cmd that you want the value contained in it.
By default if is case sensitive, so you must first make sure that your variables are spelt
correctly, also you need a space between the end of the if condition and the bracket.
Provided that this is the correct format for the batch (I don't have a clue what it actually wants to read) then this is the correct syntax
#echo off
SET ChangeExample=1341932956
if %ChangeExample%=="" (
echo changeExample==%ChangeExample%
)
I presumed you wanted to echo the variable contents rather than the string ChangeExample, if you just want the string remove the %'s.
I have a Windows batch file which has an instruction to execute an EXE file in a location whose path contains accented characters. Following are the contents of the batch file.
#echo off
C:\español\jre\bin\java.exe -version
C:\español\jre\bin\java.exe - This path exists and is proper. I can run this command directly on cmd.exe. But when I run the command from a bat/cmd file it fails saying "The system cannot find the path specified"
One way to fix this is by setting code page to 1252 (that works for me). But I'm afraid we'd have to set code pages for any non-English locale and figuring out which code page to use is pretty difficult.
Is there an alternative approach to fix this problem? Maybe a command-line option or something else?
Another way of doing this, in Windows, is by using wordpad.exe:
Run wordpad.exe
Write your script as you usually do, with accents
Choose Save as > Other formats
Choose to save it as Text document MS-DOS (*.txt)
Change the file extension from .txt to .bat
I had the same problem, and this answer solved it. Basically you have to wrap your script with a bunch of commands to change your terminal codepage, and then to restore it.
#echo off
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul
:: your stuff here ::
chcp %cp%>nul
Worked like a charm!
I'm using Notepad++ and it has an option to change "character sets", OEM-US did the trick. ;)
Since you have #echo off you can't see what your batch is sending to the command prompt. Reproducing your problem with that off it seems like the ñ character gets misinterpreted since the output I see is:
C:\espa±ol\jre\bin\java -version
The system cannot find the path specified.
I was able to get it to work by echoing the command into the batch file from the command prompt, i.e.
echo C:\español\jre\bin\java.exe -version>>test.bat
This seems to translate the character into whatever the command prompt is looking for, though I've only tested it with English locale set so I don't know if it'll work in all situations for you. Also, if you open the batch in a text editor like notepad it looks wrong (C:\espa¤ol\jre\bin\java.exe)
Use Alt + 0164 for ¤ instead of Alt + 164 ñ in a batch file... It will look odd, but your script should run.
You can use Visual Studio Code and it will let you select the encoding you want to use. Lower right corner, you select the encoding and will display option "save with encoding". Select DOS and will save the accented chars.
I also had the same problem. I was trying to create a simple XCOPY batch file to copy a spreadsheet from one folder to another. Its name had the "é" character in it, and it refused to copy.
Even trying to use Katalin's and Metalcoder's suggestions didn't work on my neolithic Windows XP machine. Then I suddenly thought: Why not keep things as simple as possible (as I am myself extremely simple-minded when it comes to computers) and just substitute, in the batch file code, "é" with the wildcard character "?".
And guess what? It worked!
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.
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.