How set name of batch as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

How set name of batch as variable

#1 Post by darioit » 10 Apr 2012 07:52

Hello,

in many script I like to insert this instruction

Code: Select all

IF NOT %errorlevel%==0 CSCRIPT error_script.vbs %name_of_batch% email etc.etc.


How can I set variable %name_of_batch% as launching script name, so I know where this error come from.

Regards

Dario

tonysathre
Posts: 14
Joined: 20 Mar 2012 10:07

Re: How set name of batch as variable

#2 Post by tonysathre » 10 Apr 2012 08:22

IF NOT %errorlevel%==0 CSCRIPT error_script.vbs %~0 email etc.etc.

Tony

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

Re: How set name of batch as variable

#3 Post by foxidrive » 10 Apr 2012 08:23

I think this is what you mean. As long as the SHIFT command isn't used in the batch script.

Code: Select all

IF NOT %errorlevel%==0 CSCRIPT error_script.vbs "%~nx0" email etc.etc.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How set name of batch as variable

#4 Post by darioit » 10 Apr 2012 08:25

oohohooho wonderful, but how do you take this information?

tonysathre
Posts: 14
Joined: 20 Mar 2012 10:07

Re: How set name of batch as variable

#5 Post by tonysathre » 10 Apr 2012 08:29

foxidrive wrote:I think this is what you mean. As long as the SHIFT command isn't used in the batch script.

Code: Select all

IF NOT %errorlevel%==0 CSCRIPT error_script.vbs "%~nx0" email etc.etc.


Doesn't %~0 expand to the same thing as %~nx0? I know what the n and x do, but I just tested it and it produced the same output. The quotes to catch filenames with spaces is better.

Tony

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

Re: How set name of batch as variable

#6 Post by foxidrive » 10 Apr 2012 08:38

%0 is the full command as entered at the command line (when shift is not used).

If you enter "c:\program files\widget\run.bat" then that entire string will be in %0

So "%~nx0" will only return "run.bat"

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: How set name of batch as variable

#7 Post by Fawers » 10 Apr 2012 10:39

From the CALL command help:

Code: Select all

%~1         - expands %1 and removes any quotes (") on it
%~f1        - expands %1 to a full, qualified path
%~d1       - expands %1 to a drive letter
%~p1       - expands %1 to a path
%~n1       - expands %1 to a file name
%~x1       - expands %1 to a file extension
%~s1       - expands %1 to a short path and file name
%~a1       - expands %1 to file attributes
%~t1         - expands %1 to file date and time
%~z1        - expands %1 to file size

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: How set name of batch as variable

#8 Post by Fawers » 10 Apr 2012 10:43

foxidrive wrote:%0 is the full command as entered at the command line (when shift is not used).

If you enter "c:\program files\widget\run.bat" then that entire string will be in %0

So "%~nx0" will only return "run.bat"


As foxidrive stated, you can combine these letters in order to have only part of the path.
For instance, "%~dpnx1" would return return you the full path of a file. And so would "%~f1".

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How set name of batch as variable

#9 Post by darioit » 12 Apr 2012 03:14

yes, but how can I know the content of variable %errorlevel%

I mean, during a failure copy of a file, my %errorlevel% is equal to 1, but on shell the error is "Access denied"

How can I grab this information?

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

Re: How set name of batch as variable

#10 Post by foxidrive » 12 Apr 2012 08:18

It depends on your batch file.


You can use something like this, as a simple example.

Code: Select all

@echo off
copy "c:\WINDOWS\system32\cmd.exe" "c:\pagefile.sys" 2>errors.txt
if errorlevel 1 echo Errorlevel is %errorlevel% >>errors.txt
pause

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How set name of batch as variable

#11 Post by darioit » 12 Apr 2012 08:52

can I wrote 2> also into a variable?

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: How set name of batch as variable

#12 Post by Fawers » 12 Apr 2012 09:00

darioit wrote:can I wrote 2> also into a variable?


I think so, but you'd have to escape the > sign.

Code: Select all

2^>

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

Re: How set name of batch as variable

#13 Post by foxidrive » 12 Apr 2012 09:34

darioit wrote:can I wrote 2> also into a variable?



You can wrap the command in a for /F and then get the output into a variable.

Code: Select all

@echo off
for /f "delims=" %%a in ('copy "c:\WINDOWS\system32\cmd.exe" "c:\pagefile.sys" 2^>^&1 ') do set "var=%%a"& goto :goterror
:goterror
echo %var%
pause


You have to be specific about what you want to do because the code will work above for the copy command, but not for every circumstance.

Post Reply