Windows BAT file SET command works in weird way - Python virtual environment activate script - batch-file

I'm trying to add some variables into Python virtual environment activate.bat.
I supposed it's a regular BAT file with pretty expected behaviour. But what I see is weird.
set env=abc
set db_url=dfg
If I do echo %env% after in the command prompt I see:
abcset db_url=dfg
Who can explain why is that and how to fix it?
Also, if I try clear it up like
set env=
it just reads all the strings after.
UPD: Okay, I don't know what I've done but what worked:
I recreated the virtual environment
I opened all files in VS Code (not in Notepad++ as before)
I put all the variables sets into double quotes like set "env=dev"

Related

How to use yarpgen random C program generator?

I am new here.
Could anyone help me, on how to use yarpgen to generate a random c program.
I tried running the run_gen.py script that I saw in the yarpgen readme.
But, I got a warning and an error like this:
Warning: please set YARPGEN_HOME envirnoment variable to point to test generator path, using C:\Users\..\Python\Python36-32\yarpgen-master for now
and
File C:\Users\..\Python\Python36-32\yarpgen-master\yarpgen wasn't found
Any help would be much appreciated.
Thanks in advance !
The warning very likely points to the source of the problem, run_gen does not know where the other parts of yarpgen are installed.
First, note down the directory you installed/copied yarpgen to.
Then open a command shell. Type this:
cd <where run_gen.py is>
set YARPGEN_HOME=<the path you just noted down>
run_gen.py
If this works, you can write a batch script, that contains the set YARPGEN_HOME=... line and then calls run_gen.py. If the directory where run_gen.py is located is not on your PATH environment variable, call run_gen.py with the full absolute path in the batch script:
set YARPGEN_HOME=<the path to yarpgen>
python3 <absolute_path_to>\run_gen.py
Then you can call your batch script.
You may have to adjust the python3 command depending on the executable Python 3 installed on your machine (it may be just python on Windows).
When I tried this after building with cygwin, I noticed that I had to rename yarpgen.exe to yarpgen to make it work.

Windows - making cmd see PATH changes

I have a bunch of batch files which I want to run sequentially. One of them runs an MSI which adds a folder to the PATH. How can I make sure that subsequent batch files will notice this change, without restarting CMD? I'm using Windows Server 2008 R2 x64.
I've tried call, cmd /c and start "", in the hope that starting a new process will work, but it doesn't.
in run-both-scripts.bat
call script1.bat <-- This runs an MSI which modifies the PATH
call script2.bat <-- This relies on the PATH changes which were made by the MSI in script1.bat
To clarify: this is fairly straightforward to reproduce.
Start CMD
Create an environment variable manually, not using setx, to mimic what the MSI does.
Right click on Computer -> Properties -> Advanced System Settings -> Environment variables -> New
Create an environment variable called, say, hello with the value hi there.
In your CMD window, type echo %hello%. You'll get %hello%.
Try cmd /c "echo %hello%. You'll get %hello%.
Try start "" to open a new CMD process; type echo %hello%. You'll get %hello%.
Try start "" echo %hello% to run the command in a new CMD process. You'll get %hello%.
Finally, try manually opening a new CMD window from the Start menu and type echo %hello% from there. You'll see hi there.
So you can see that the only way I've been able to make CMD see the change to the environment variable is by restarting CMD.
Ok, did some research, and found out why the soloutions we've been throwing at you don't work. When you start cmd.exe as an application, it looks at the current environment variables and copies it to memory. When you start cmd in a batch file, it will not look at the environment variables, instead it will look at the variables set in the current batch file, and utilise those. Thats the problem when your storing data on memory. The only way this is possible is if you copy the current environment variables into a text file as memory on a hard disk. Now, the question is how to go about doing this.
After heavy research, the only thing I could find that related to the topic was the use of start /i, however, when I tested this it didn't work. (start /? for more info).
In other words, other the setx, I don't think this is possible with batch.
Mona

Using %~dp0 for MSIExec Logfile Output

I'm trying to output my logfiles to a subfolder relative to my install script, and MSIExec doesn't seem to like it when used with the "/l*v" command. I've tried variations of %~dp0Logs (with and without quotes, etc.). If I manually enter the full path like: /l*v c:\scripts\logs\%computername%.txt" it works fine, but the script is always going to be in different locations (USB, network, etc.).
I see references online to using the %temp% system variable which I guess works, but not the parent lookup folder variable of %dp0. And I'm using the same variable elsewhere in my script for other things, like running the MSI and file copy commands (copy "%~dp0Files\Images\%LogonBackgroundWinXP%"...). I've even tried setting a variable like: Set LogFolder=%dp0\logs, but that doesn't seem to work either.
Am I missing something or am I just going to have to find another folder lookup method just for my log files?
Thanks,
Brian
I guess all I needed was a few hours away from this and lunch! Works perfectly fine, not sure what I was doing wrong before. I was going to do a copy command afterwards, but now I don't have to.

.bat to .sh converting for SQL*Loader Start on UNIX PC

I have a simple .bat file
#echo; set nls_lang=russian_cis.ru8pc866
#echo off
SET NLS_NUMERIC_CHARACTERS=. '
sqlldr.exe userid=PRB/0611#TSESTDB control=control_file.ctl LOG=fdb_log.log errors=100
cmd
and i need to convert to .sh file for running on the UNIX based pc.
I began to read "BASH Programming - Introduction HOW-TO" (is it suitable for beginners?), but it is a episodical task and dead line comes.
Could anybody help me to convert file? Thanks a lot!!!
rewriting your script.
#!/bin/bash
# #echo;
# set nls_lang=russian_cis.ru8pc866
export NLS_LANG=russian_cis.ru8pc866
# not needed #echo off
# SET NLS_NUMERIC_CHARACTERS=. '
export NLS_NUMERIC_CHARACTERS='.'
PATH="/path/to/sqlDir/install:${PATH}"
# sqlldr.exe userid=PRB/0611#TSESTDB control=control_file.ctl LOG=fdb_log.log errors=100
sqlldr userid=PRB/0611#TSESTDB control=control_file.ctl LOG=fdb_log.log errors=100
# ? cmd
I've left your code in, but commented out (using the shell comment char '#'). The uncommented lines are the 'translation' of .bat syntax into Linux/Unix bash/shell syntax.
There some things above that you may need to fix:
You'll have to include the correct value in the resetting of PATH,
note that the value there is strictly to illustrate the issue.
export is used so that variables set in the current shell (the
shell script) are visible to child processes that run from the shell
script, in this case the important one being sqlldr
I'm not sure what values you really need assigned to
NLS_NUMERIC_CHARACTERS. Note that by quoting with the single-quote
char ' available to the shell, you should get exactly the value
used that you intended. If '*' or other reg-exp chars are used, this
may cause problems.
You may find that sqlldr.exe has a different name altogether. The
linux/unix convention for executable commands does not require the
.exe extension, so I have used sqlldr. Just use the full name of
the program you find in the installed directory.
The line with #!/bin/bash needs to be the first line in the file, with no leading spaces.
You'll also need to inform your OS that the script is intended to be executable. From a bash cmd line, IN the directory that contains this script, do
chmod 755 mySQLLDR_runningScript
Finally, not sure why you have cmd at the end of your .bat file, to open a new window? You'll need to experiment on your system to find the correct cmd to do that. Maybe xterm.
I hope this helps.

Running BAT/CMD file with accented characters in it

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!

Resources