Why is atprogram not working from within a batch file? - batch-file

I am working with an embedded system developed specifically for the company I work at. I want to use Atmel's command prompt to program the target processor which is pretty easy.
But, in the future, the technician building our products will do it themselves so I thought of a batch file they could run with the scrips for programming and checking the code for them automatically.
Here is the problem, I don't manage to get it running trough the *.bat file.
When I do it without I just start the Atmel console and type
atprogram -i ...
I have tried writing the batch file like:
"C:\...\StudioCommandPrompt.exe" atprogram -i ...
Which gives me the result atprogram is neither a file nor a program
I also tried with:
START "C:\...\StudioCommandPrompt.exe" atprogram -i ...
Then the program starts in a separate prompt, and produces the same error as above.
The empty set as suggested in the comments:
START "" "C:\...\StudioCommandPrompt.exe" atprogram -i ...
Gives the same result as without the empty set.
Does anyone know how to resolve this?

SOLUTION:
START "programTool" /B /WAIT "C:\...\atprogram.exe" -i ...
I.E. refering to the correct software (cred: http://www.avrfreaks.net/forum/just-run-programmer hobbss)

Related

Executing windows commands in C

I have a batch file which changes the direction to a specific toolchain and executes one command like this:
cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin
avr-objcopy -O binary C:\Users\cinar\Desktop\hextobin\GccApplication.elf C:\Users\cinar\Desktop\hextobin\GccApplication.bin
I want to do this with my C application. I found this topic, tried the system(); command and it works partially. I can call this:
system("cd");
and get the direction back. But I can not change it with this command:
system("cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin");
This caused a compile warning about unknown escapes, so i added \ to escapes and tried this:
system("cd C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
I was able to compile and run this but that didn't change the direction.
Is there any possibility to execute my commands with system()? As I just want to change the direction and execute one command, I wanted to keep it simple.
Update: I found this topic afterwards:
system("cd <path>") in a C program
Then solved my query with this:
chdir("C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
system("avr-objcopy -O binary C:\\Users\\cinar\\Desktop\\ff.elf C:\\Users\\cinar\\Desktop\\ff.bin");
Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.
Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().
For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.
Also
system("dir Users\\whatEverNextFolder > test.txt");

Is there a way to check for return code using PSFTP?

I am creating a script file using windows BATCH and then calling it using PSFTP (using Putty from command line to call TransferScript.txt).
The requirement is to get a file from Unix box to windows and then delete it from Unix but after getting the return 0. My code looks like:
TransferScript.txt
lcd E:\Temp
cd /sap/xx/yy
get abc.dat
IF %ERRORLEVEL% EQ 0
rm abc.dat
quit
This doesn't work, however file is getting copied but not getting deleted. Can anyone help?
The command processor for the file is psftp, not cmd.exe. As such, IF is not a recognized command. You can, however, use the ! PSFTP command to pass commands to Windows. This is documented here. Here is a tested working version:
Command: psftp -b script.txt username#ftpserver.com -pw password
#script.txt
!echo Connected. Getting file...
lcd c:\temp
cd sap/xx/yy
get abc.dat
!IF EXIST c:\temp\abc.dat echo Success
!IF NOT EXIST c:\temp\abc.dat echo Failure
quit
Simply replace echo Success with rm abc.dat or whatever you want to happen if the file exists on the local file system.
Another option would be to do all the logic in an external batch file. Write 2 PSFTP scripts like script_get.txt and script_rm.txt and call the second one only if the file exists (like above).
I know neither of these solutions technically answer your question in that they don't capture an error state from PSFTP but hopefully it's an acceptable workaround.

Running a shell script through Cygwin on Windows

I have a bunch of shell scripts that used to run on a Linux machine. Now, we've switched over to Windows, and I need to run these scripts there. I have Cygwin installed, but is there a way to make the script run using Cygwin, but the call is made from Windows batch?
Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh) from a Windows batch file using a command like:
C:\cygwin\bin\bash testit.sh
... which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.
One more thing - if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin's bash wouldn't accept those \r. Just run dos2unix testit.sh before executing the script:
C:\cygwin\bin\dos2unix testit.sh
C:\cygwin\bin\bash testit.sh
If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:
Open the file that's giving the error in Notepad++.
Go under the "Edit" Menu and choose "EOL Conversion"
There is an option there for "UNIX/OSX Format." Choose that option.
Re-save the file.
I did this and it solved my problems.
Hope this helps!
Read more at http://danieladeniji.wordpress.com/2013/03/07/microsoft-windows-cygwin-error-r-command-not-found/
Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to 'fix' a bunch of our scripts.
find . -type f -exec dos2unix.exe {} \;
I'd do it as a comment to Roman's answer, but I don't have access to commenting yet.
The existing answers all seem to run this script in a DOS console window.
This may be acceptable, but for example means that colour codes (changing text colour) don't work but instead get printed out as they are:
there is no item "[032mGroovy[0m"
I found this solution some time ago, so I'm not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:
D:\apps\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico bash.exe .\myShellScript.sh
... this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.
If you don't mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).
To illustrate:
1. Always include .sh to your script file name, e.g., test1.sh
2. test1.sh looks like the following as an example:
#!/bin/bash
echo '$0 = ' $0
echo '$1 = ' $1
filepath=$1
3. On Windows with Cygwin, you type "test1.sh" to run
4. On a Unix, you also type "test1.sh" to run
Note: On Windows, you need to use the file explorer to do following once:
1. Open the file explorer
2. Right-click on a file with .sh extension, like test1.sh
3. Open with... -> Select sh.exe
After this, your Windows 10 remembers to execute all .sh files with sh.exe.
Note: Using this method, you do not need to prepend your script file name with bash to run

Why multiple arguments with spaces are not interpreted correctly in a batch script?

I'm running an exe with multiple arguments that may or may not contain spaces. I encloses them with quotes but they are somehow not passed to the exe correctly.
Here's the command i'm using:
makeblastdb -in "D:\d b\sequence.fasta" -input_type fasta -dbtype prot -title xd -out "D:\d b\xd"
which I think cmd should pass 10 arguments to the exe but somehow it isn't passing correctly.
this is the result i get
BLAST options error: File "D:\d" does not exist.
which is basically saying that the second argument is being chopped for some reason?
Any help will be appreciated, thanks!
Based on your comments to your question, the BLAST utility does not properly handle quoted paths with spaces, and your volume does not support short file names.
Obviously you can move your working directory to a path that does not contain spaces.
An alternative is to use SUBST to temporarily create a virtual drive that points to your problematic path.
subst K: "d:\d b"
makeblastdb -in "K:\sequence.fasta" -input_type fasta -dbtype prot -title xd -out "K:\xd"
subst /d K:
Type subst /? for help with the command.
Update based on fact that you are running the command from within python
In your comment to this answer, you state you will attempt to get the command to work from within python. That could be the entire source of your problem.
You should try to run the command in your question directly from a Windows command prompt (cmd.exe console).
If the command does not work from the command prompt, then the problem is indeed with the BLAST utility, and SUBST is a good solution.
If the command does work from the command prompt, then the problem is with how you are shelling out the command from python, and the SUBST command should not be required.
I'm not a python user, but I see that many people have similar problems when using python on Windows. Perhaps this will help: How do I execute a program from python? os.system fails due to spaces in path
makeblastdb has an odd escaping convention. Try this:
-in \""D:\d b\sequence.fasta"\"
Unfortunately this doesn't work for -out, so dbenham's answer is probably best.
Alternative is you can try using directory shortname for "D:\d b" which you can find by running dir /X command on your D drive. For instance if I run dir /X on my C drive here is what I get:
01/21/2013 09:47 AM <DIR> PROGRA~1 Program Files
So you want to use C:\Program Files you can alternatively use C:\PROGRA~1.

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.

Resources