To Get Conda in cmd System-Wide

You have installed Miniconda3 on my Windows (10/7) laptop. You can run python through the Anaconda Prompt but python is not recognised in the Windows Command Prompt.

From Windows Command Prompt if you type in ‘conda info’ you get this because it doesn’t even recognise conda:

‘conda’ is not recognized as an internal ….

How to solve this?

Sometimes having conda in cmd line is a useful thing to have. Opening Anaconda prompts just for accessing conda utilities is a hassle, so I always have conda available in cmd systemwide.

Here’s are two steps to follow.

To get conda in cmd system-wide

Step 1

If Anaconda is installed for the current user only, add %USERPROFILE%\Anaconda3\condabin (I mean condabin, not Scripts) into the environment variable PATH (the user one). If Anaconda is installed for all users on your machine, add C:\ProgramData\Anaconda3\condabin into PATH.

How do I set system environment variables on Windows?

set path=%path%%USERPROFILE%\Anaconda3\condabin

Step 2
Open a new Powershell or CMD, run the following command once to initialize conda.

conda init


These steps make sure the conda command is exposed to your cmd.exe and Powershell.

Hope this helps someone.

Pair this post with these useful posts to dig deeper.

Advertisement

Use FORFILES to Automate and Simplify Windows workflow

If you are using windows command line cmd, you need to know about the for command. This command is useful and can help you automate many common tasks with the windows batch command. I have talked about the for command a lot.

But recently I was surprised to find another gem of a command that makes using files a lot more convenient. The command is forfiles.

Don’t know when this was introduced but it’s a command worth learning about if you manipulate files from the command line of windows.

Forfiles helps your select a file or set of files and execute a command on that file. Really really useful if you work on batch jobs involving a number of files.

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname   - returns the file name without
                                   extension.
                        @ext     - returns only the extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "MM/dd/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.

Examples:
    FORFILES /?
    FORFILES
    FORFILES /P C:\WINDOWS /S /M DNS*.*
    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C:\ /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 01/01/2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +6/9/2021 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

Pair this post with these posts.

A Common Problem When Using a Virtual Machine

Here’s a common problem when using a virtual machine. You go out for lunch or a small errand to get the food order from the security gate of your apartment and the virtual machine locks out.

If you are especially unlucky the VDI machine completely disconnects due to aggressive inactivity monitoring. And if the VDI machines are in short supply or you are in peak working hours, good luck in getting the system back.

Hours wasted. Usually, this ruins the work mood and spoils the experience of working.

To mitigate this frequent issue, wanted to have my VDI machine always running, so needed a system to make this work.

Python was my first choice, but due to security, the system was stripped to a bare minimum and even python was not installed in the virtual system and was unreliable.

So turned to the trusty windows command line cmd.

My first attempt was this

@echo off

:loop
RunDll32.exe user32.dll,SetCursorPos
timeout /t 45
goto loop

This simple windows batch moves the mouse by a tiny amount every 45 seconds. Yes, it’s annoying but I was hoping this will work, but alas I was wrong. Bloody windows.

Searched online and someone said that moving the mouse is not enough. Windows is looking for user input, not just mouse movement. Damm it.

So the next solution was two parts.

Created a VBScript neversleep.vbs

set shell = CreateObject("WScript.Shell")
WScript.Sleep 1500
shell.SendKeys"{SCROLLLOCK}"
WScript.Sleep 1500
shell.SendKeys"{SCROLLLOCK}"

This script mimics the pressing of a key on the keyboard.

And the second file is the command line batch script to fire the VBScript after every 45 seconds.

@echo off

:loop
cscript "C:\Users\sukhbinder\Desktop\neverSleep2.vbs" //nologo
timeout /t 45
goto loop

I used the most useless key on the keyboard Scroll Lock and it is nice to see it briefly blink now and then.

If the VDI servers are working properly this solution works like a charm. Happy!! But not for long as…….

Well, let’s park that for another post.

If you found this post useful, you might like these too.

Backing up Files using OneDrive

My employer provides Microsoft OneDrive to keep the works files safe. Anytime I call the IT department, for any fix in the laptop, they always ask “Is your files backedup in OneDrive in case we might need to re-image the system” Reimage, whatever that is. I always mumble yes.

This always reminds me to backup my files on the desktop, so I do it on that day. But having done this a couple of times, I have finally created a batch file using robocopy which does the job every week.

Here’s the batch file that I use.

@echo off
cd C:\Users\sukhbinder.singh
robocopy Desktop "D:\sukhbinder.singh\OneDrive\backup" /e /mir /np /tee /mt /log:d:\backup_log.txt

And all this is scheduled using the schtasks and runs multiple times a week.

Calendar in Cmd

Calendar in CMD using python

In my quest to make my windows system mostly behave like Mac, i keep adding things that i miss in windows like the ability to see the calendar in terminal.

In macbook pro, typing cal anywhere in the terminal gives you the calendar.

Python comes to my rescue in all these quests to turn windows more like the mac os.

The first step to do that is use alias and create a cal keyword. This post talks about setting alias in windows using doskey.

And the meat of the application is this python one-liner that gives you the calendar output in text format.

python -m calendar 2021 10

If you want the calendar for entire year, just skip the year and month

python -m calendar

Hope this helps someone else.

Managing Windows Desktop Using Git

My desktop quickly becomes unwieldy, so to tame the clutter, I have been using git to take regular backup of the desktop, here’s the batch script that I have configured with schtasks on my windows system.

Posting it here with explanation so its useful for others.

@echo off
cd C:\Users\sukhbinder\Desktop

git diff --stat > test
git status >> test
git add -A
git commit -F test

Explanation

git diff –stat Gives a compact statistics on the git

git status shows the current state for the working git folder

git add -A adds all files in the folder. By all I mean all tracked and untracked files in the folder

git commit -F test commits the added files using the contents of test file as the commit message

Taking a Break with Schtasks

Taking a break from the computer has become essential in this WFH environment. In the office, it was natural and happened often but at home, this is not the case.

I have tried many things.

Setting up a timer that is displayed on my desktop to remind me to take a break. This failed as resetting the countdown timer every time added the friction to render the whole exercise fruitless

Then I tried to use the outlook calendar to remind me to take a break.

This failed as slowly the calendar reminders were ignored.

Both of these approaches failed.

Needed something more visual. So this is my current setup. All done using standard available tools.

No software or installation required.

schtasks /create /tn take_a_break /sc HOURLY /mo 2 /st 08:30 /et 18:00 /tr "https://large-type.com/#Take%20a%20Break"

Every 2 hours it launches the default browser with the big fonts telling me to take a break. Much more intuitive and useful than anything I have tried earlier.

Another beauty of the system is that if this doesn’t get me started, I can configure this to lock my screen at that moment. A slight inconvenience but something that I will try if this big pop up doesn’t work.

How do you deal with this?

Add an app to run automatically at startup in Windows 10

Last summer, changed my office laptop after a long time and have been using windows 7 on it but then with the new system got upgraded to windows 10.

Windows 10 is nice but there are few quirks that are intimidating for any new user moving to it. One such simple thing is if you want to run an app automatically at startup in windows 10.

In all previous windows, it was simple to navigate to the startup folder and place a shortcut of your app to start it on startup

But in Windows 10, this has changed. Now to get to the startup folder you need to type shell:startup in the run command line.

Here’s a gif of the same in action.

Steps

  • Press the Windows logo key  + R,
  • Type shell:startup, then select OK. This opens the Startup folder.
  • Copy and paste the shortcut to the app from the file location to the Startup folder.

Have you faced this or something similar in your move to windows 10?

For Magic in CMD

A windows batch script command that has come in handy for me in number of occasions. This has been a time saver for many occasions, most recently this was used for running a machine learning model on number of bank statements stored in various excel workbooks stored in a folder.

Another was when I had to convert and process large number of FEM models stored across in the a drive.

Here’s the command

for /f %e in ('dir *.pm /b/s') do echo %e

echo %e is the command that needs to be fired.

dir *.pm /b/s list all *.pm files names in the current directory and sub folders

Some specific examples:

for /f %e in ('dir *.xls /b/s') do python make_inference.py %e
for /f %e in ('dir d:\monte_carlo_inputs\*.pm /b/s') do call monte_carlo.bat %e -e exec.script
for /f %e in ('dir d:\*.fem /b/s') do auto_nlc -fem %e -j input.json

Hope this helps. I know it helps me a lot every time I have do something repeatedly.

Bloody windows: Create conda environments

Bloody windows

One of my collegue at office really really hates windows, every time he is forced to use windiwos, I have heard him mutter “bloody windows”

Sometime back I had the same desire.

This is the batch script, which works perfectly fine if I use the command in the cmd window but it failes in batch mode.

Old batch file

rem Create conda environment
echo Creating conda environment
conda deactivate
conda env create --file environment.yml --force --quiet

After a lot of head scratching, trying to figure out what is happening, found that I have to use call.

New batch file

rem Create conda environment
echo Creating conda environment
call conda deactivate
call conda env create --file environment.yml --force --quiet

Turns out call is needed.

call activate %name%

I’m assuming that activate is a batch file. If you call it, processing will return after that batch is finished. Without the call, execution is transferred to activate and ends when activate ends

Kill Tasks in Windows

Linux, Mac users have it easy. Top and kill are two commands that can help one take control of the system.

For windows user, until recently I was stuck with the task manager. Like all manager this one demands too much attention and is not batch able.

Summoned Google Gennie and discovered.

Tasklist and Taskkill commands in CMD.

Neat. Where were these commands hiding?

Here is two gifs for how they work

A Mac App for Your Windows

Dictionary App

I am using Mac OS for more than 10 months now and every day I am loving it more… It’s the little tweaks and apps that make using it such a pleasure.

One of them is the quick Dictionary available at finger tips across all apps. Select a word, press the shortcut key and the meaning pops up. (Shown in image above). Made reading of ebooks on Mac so much fun.

Missed this feature so much when working on windows, created a similar app in windows. Uses dictionary.com, Oxford online dictionary and google to get the meaning of the selected word.

Here’s the fruit of two days of intermittent work.
Download the App as Zip file

No installation. Unzip. Click on the executable.

How to use:
1. Select the word.
2. Click F10

Use F11 to use google and use F12 for online Oxford dictionary.

Unlike the Mac app, this app needs online access to work!!

Windows, Mac and Linux

mac-book-pro

Before I say anything read this…

The analogy between cars and operating systems is not half bad, and so let me run with it for a moment, as a way of giving an executive summary of our situation today.

Imagine a crossroads where four competing auto dealerships are situated. One of them (Microsoft) is much, much bigger than the others. It started out years ago selling three-speed bicycles (MS-DOS); these were not perfect, but they worked, and when they broke you could easily fix them.

There was a competing bicycle dealership next door (Apple) that one day began selling motorized vehicles–expensive but attractively styled cars with their innards hermetically sealed, so that how they worked was something of a mystery.

The big dealership responded by rushing a moped upgrade kit (the original Windows) onto the market. This was a Rube Goldberg contraption that, when bolted onto a three-speed bicycle, enabled it to keep up, just barely, with Apple-cars. The users had to wear goggles and were always picking bugs out of their teeth while Apple owners sped along in hermetically sealed comfort, sneering out the windows. But the Micro-mopeds were cheap, and easy to fix compared with the Apple-cars, and their market share waxed.

Eventually the big dealership came out with a full-fledged car: a colossal station wagon (Windows 95). It had all the aesthetic appeal of a Soviet worker housing block, it leaked oil and blew gaskets, and it was an enormous success. A little later, they also came out with a hulking off-road vehicle intended for industrial users (Windows NT) which was no more beautiful than the station wagon, and only a little more reliable.

Since then there has been a lot of noise and shouting, but little has changed. The smaller dealership continues to sell sleek Euro-styled sedans and to spend a lot of money on advertising campaigns. They have had GOING OUT OF BUSINESS! signs taped up in their windows for so long that they have gotten all yellow and curly. The big one keeps making bigger and bigger station wagons and ORVs.

On the other side of the road are two competitors that have come along more recently.

One of them (Be, Inc.) is selling fully operational Batmobiles (the BeOS). They are more beautiful and stylish even than the Euro-sedans, better designed, more technologically advanced, and at least as reliable as anything else on the market–and yet cheaper than the others.

With one exception, that is: Linux, which is right next door, and which is not a business at all. It’s a bunch of RVs, yurts, tepees, and geodesic domes set up in a field and organized by consensus. The people who live there are making tanks. These are not old-fashioned, cast-iron Soviet tanks; these are more like the M1 tanks of the U.S. Army, made of space-age materials and jammed with sophisticated technology from one end to the other. But they are better than Army tanks. They’ve been modified in such a way that they never, ever break down, are light and maneuverable enough to use on ordinary streets, and use no more fuel than a subcompact car. These tanks are being cranked out, on the spot, at a terrific pace, and a vast number of them are lined up along the edge of the road with keys in the ignition. Anyone who wants can simply climb into one and drive it away for free.

Customers come to this crossroads in throngs, day and night. Ninety percent of them go straight to the biggest dealership and buy station wagons or off-road vehicles. They do not even look at the other dealerships.

Of the remaining ten percent, most go and buy a sleek Euro-sedan, pausing only to turn up their noses at the philistines going to buy the station wagons and ORVs. If they even notice the people on the opposite side of the road, selling the cheaper, technically superior vehicles, these customers deride them cranks and half-wits.

The Batmobile outlet sells a few vehicles to the occasional car nut who wants a second vehicle to go with his station wagon, but seems to accept, at least for now, that it’s a fringe player.

The group giving away the free tanks only stays alive because it is staffed by volunteers, who are lined up at the edge of the street with bullhorns, trying to draw customers’ attention to this incredible situation.

This extract is from In the Beginning was the Command Line by Neal Stephenson. Loved the analogy. And if the car analogy isn’t enough read the updated commented version here to know about the monkeys!!

A neat trick to use notepad as logger

notepad

Here’s a simple trick in notepad that I have been using ever since I remember. I am surprised how few know about this.

Use notepad to log anything in 3 steps

  1. Open notepad.
  2. In the very first line input this .LOG Yes ‘dot’ followed by the word ‘log’ without a space.
  3. Save and close the file.

That’s it. You can choose whatever name you want.

Now whenever this file is opened, it gives you a convenient time and date. Just write your log entry after the date and time stamp and close.

Convenient isn’t it!!

Know any other trick similar to this, Would love to know about it. Share it here.

Plants Become Weeds when they…..

FortranDLL in visual studio

How to Create Fortran DLL in Visual Studio? – The simple way


Plants become weeds when they obstruct our plans or our tidy maps of the world. Fortran is also perceived in the same way. Although still a formidable programming language, many now treat it as weeds.

They encounter it and they go back to fortran 66 mindset. Fortran’s new capabilities are discounted and people look past the new features of the language to talk about the horrible “go to” mess!! They might want to look at this Myths Of Fortran post!

There’s so much legacy code in fortran, but no one wants to touch it. The easiest solution commonly used is to convert it into a dll or some library and use with new languages. This explains the popularity of the post how to create fortran dll in visual studio on this blog.

This brings me to today’s post. Here’s an even more convenient animated gif to explain the process of converting a fortran program to a dll in visual studio with intel fortran compiler!

Shortcuts

image

We were taught “Never take shorts cuts” but in excel and other software program, shortcuts make you productive.

Here are my two most used shortcuts in Microsoft excel. They save a ton of clicking and touching the mouse.

1. Use F4. It repeats the previous action. Automate any single action instantly.

2. Control+1 quick formatting

What are your most used shortcuts?

Meshing Fortran and Matlab together with Mex

matlab fortran A few days back colleague of mine needed a help in calling Fortran from matlab. At that time I knew about the call system command.

But as we tried solving his problem, I learnt about Mex. I knew Mex was matlab executable but had never explored them or used or created them.

My colleague’s problem was solved once we got matlab and the c compiler. That day and few days after, I continued exploring the dark alleys of Mex.

Learnt a lot.  Here’s the list of links that were great at explaining the meshing of fortran and c with matlab with Mex.

Perhaps someday I will use them in some project, but till then let them be kept here.

A Stochastic Encounter

I use gfortran fortran compiler at home and intel fortran compiler in office.

Its by random chance I found a vital difference in gfortran and intel compiler’s interpretation of using the intrinsic subroutine Random_number.

In gg95 fortran forum, someone posted this query.

Hi,
I have the following code to generate a random number in between 0 and 1.

program random
implicit none
real :: R
call random_seed()

call random_number(R)
end program

But it always returns a same number. Why this happens, can any body suggest me?

I checked this in gfortran and I got the same result. Everytime it returned the same random number.

With Intel compiler the result was as expected. In sun solaris it was as expected. The program gave different random number on each call.

But gfortran was different. I thought it was a bug in gfortran.

But then Tobius Burnus explained this

No, it’s actually a bug in the Fortran standard: The standard does not
specify whether calling “random_number()” should produce by default the same sequence or every time a different sequence.

There are proponents for both.

One group argues that having a random_number() function should produce random results. Others argue that when running, e.g., an Monte Carlo algorithm, by default the results should be reproducible. As the standard does not mandate either choice, one has to live with having compiles in both groups. (I heard that the person responsible for adding the intrinsic believed that he had
written down the choice – but obviously he hasn’t.)

I think its worth keeping in mind. This can help solve lot of pain if you are using gfortran and intel compilers for your stochastic programs.