Running DOS batch script from another program

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cats_five
Posts: 6
Joined: 03 Aug 2015 07:59

Running DOS batch script from another program

#1 Post by cats_five » 03 Aug 2015 09:38

I want to get a front-end program run a DOS script, but it only has one variable to pass the script in and I am at a lose as to how to form my working batch script into the variable. This is not quite how it needs to be in the final version - instead of the echo it will it needs to exit 0 or 1 as appropriate which will get returned to the calling program as true or false.

Unfortunately I know very little about DOS batch scripts hence asking for some help...

Code: Select all

@echo off
set path=%1
for %%A in (%path%) do set X=%%~aA
set D=%X:~0,1%
if %D% EQU d (echo directory) else (echo file)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Running DOS batch script from another program

#2 Post by Squashman » 03 Aug 2015 11:40

Use the exit command to set the errorlevel.
http://ss64.com/nt/exit.html

cats_five
Posts: 6
Joined: 03 Aug 2015 07:59

Re: Running DOS batch script from another program

#3 Post by cats_five » 04 Aug 2015 08:45

I know I need to use exit, what I've obviously not explained very clearly is that I need to be able to write it as a one-liner.

cats_five
Posts: 6
Joined: 03 Aug 2015 07:59

Re: Running DOS batch script from another program

#4 Post by cats_five » 05 Aug 2015 04:12

I've had a go at reducing the script to a single line. This is my original script, which works - I added setlocal & endlocal as I was getting problems resulting from variables persisting in my DOS window. I also changed path to P as I realised path is a system variable and (naturally!) resetting it was giving me other problems:

Code: Select all

@echo off
setlocal
set P=%1
for %%A in (%P%) do set X=%%~aA
set R=%X:~1,1%
if %R% EQU r (echo readonly) else (echo writeable)
endlocal


This is my one-liner which I called sw3.bat:

Code: Select all

SETLOCAL & SET P=%1 & FOR %%A IN (%P%) DO SET X=%%~aA & SET R=%X:~1,1% & IF %R% EQU r (echo readonly) ELSE (echo writeable) & ENDLOCAL


When I run it, this is the result:

Code: Select all

E:\Users\sallyw\Downloads>sw3.bat test
E:\Users\sallyw\Downloads>SETLOCAL   & SET P=test   & FOR %A IN ((null)) DO SET
X=%~aA   & SET R=~1,1R EQU r (echo readonly) ELSE (echo writeable)   & ENDLOCAL


As you can whilst it appears to set P to the variable, in the FOR loop it's empty. Getting very frustrated with this, could have done it ages ago in Unix... Think I need delayed variable expansion but am struggling to use it correctly.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running DOS batch script from another program

#5 Post by foxidrive » 05 Aug 2015 04:27

Code: Select all

@for %%A in ("%~1") do @echo %%~aA|find /i "r">nul&&(@echo readonly)||(@echo writeable)

cats_five
Posts: 6
Joined: 03 Aug 2015 07:59

Re: Running DOS batch script from another program

#6 Post by cats_five » 05 Aug 2015 08:18

This I is as far as I've managed to contract it:

Code: Select all

setlocal EnableDelayedExpansion & set G=%1 & for %%H in ( !G! ) do ( set I=%%~aH )
set J=%I:~1,1%
if %J% EQU r (echo readonly) else (echo writeable) & endlocal


Which produces the following output:

Code: Select all

E:\Users\sallyw\Downloads>sw3.bat TEST
E:\Users\sallyw\Downloads>setlocal EnableDelayedExpansion   & set G=TEST   & for  %H in (!G!) do (set I=%~aH  )
E:\Users\sallyw\Downloads>(set I=dr-------  )
E:\Users\sallyw\Downloads>set J=r
E:\Users\sallyw\Downloads>if r EQU r (echo readonly )  else (echo writeable )  &  endlocal
readonly


(I've taken out the blank lines and the word wrap on the output)

As soon as I try joining the 1st & 2nd, or 2nd & 3rd lines, I get errors.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Running DOS batch script from another program

#7 Post by Squashman » 05 Aug 2015 13:59

Did you try Foxidrive's code?

Why do you want it all on one line if you are going to use it in a batch file?

cats_five
Posts: 6
Joined: 03 Aug 2015 07:59

Re: Running DOS batch script from another program

#8 Post by cats_five » 06 Aug 2015 03:05

His code works in a batch file (of course!), I didn't notice it arriving. :oops:

It's complicated to explain clearly why it needs to be on one line, but it's to do with another program only being able to run a single line of DOS and only being able to pick up true or false as the outcome.

I can see how I would adapt it to pick up if the path is a directory or not a directory so that's fine.

Now to try it in the correct context.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Running DOS batch script from another program

#9 Post by Squashman » 06 Aug 2015 07:33

cats_five wrote:It's complicated to explain clearly why it needs to be on one line, but it's to do with another program only being able to run a single line of DOS and only being able to pick up true or false as the outcome.

Well the only way I can see that being true is if you are physically putting the code into the "other" program to execute and not having the "other" program actually execute a .BAT file.

Other than that there is a much easier one liner to check if something is a folder.

Code: Select all

IF EXIST "C:\path to some folder\subfolder\" echo is a folder


Code: Select all

H:\>cmd /C "IF EXIST "H:\temp\" (exit /b 0) else (exit /b 1)"

H:\>echo %errorlevel%
0

H:\>cmd /C "IF EXIST "H:\tempno\" (exit /b 0) else (exit /b 1)"

H:\>echo %errorlevel%
1

cats_five
Posts: 6
Joined: 03 Aug 2015 07:59

Re: Running DOS batch script from another program

#10 Post by cats_five » 10 Aug 2015 03:29

The other program is running on a server and passes the script to a frontend running on a workstation, always a Windows PC in our case.

However thanks for the suggestion, it will deal with determining if something is a folder though not if it's readonly. But maybe I can do without that.

I will take up the other one-liner with the people who support the product I'm using.

Thanks all.

Post Reply