How to pass command from one bat file to another bat file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

How to pass command from one bat file to another bat file

#1 Post by somu_june » 13 Oct 2014 17:42

Hi all,
I want to pass the parameter from file1.bat to file2.bat
echo set filename=%~n0 >> file2.bat
But when I do the above echo statement and writes to a file2.bat, the command is executed and giving the file name, but I need set filename=%~n0 in file2.bat and not the command output. Is there a way I can achieve this


Thanks,
Somaraju

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

Re: How to pass command from one bat file to another bat fil

#2 Post by foxidrive » 13 Oct 2014 19:03

Show us what is in file2.bat please.

There is not enough description to properly understand the issue.

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to pass command from one bat file to another bat fil

#3 Post by somu_june » 13 Oct 2014 19:41

file2.bat Contains

set filename=file1

But needs below command in file2.bat

set filename=%~n0


Thanks,
Somaraju

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

Re: How to pass command from one bat file to another bat fil

#4 Post by foxidrive » 13 Oct 2014 20:42

Here you go:


Code: Select all

>file2.bat echo set filename=%%~n0



Also consider this to protect the variable from some poison characters.

Code: Select all

>file2.bat echo set "filename=%%~n0"

somu_june
Posts: 30
Joined: 19 Jun 2013 20:26

Re: How to pass command from one bat file to another bat fil

#5 Post by somu_june » 13 Oct 2014 20:49

Thank you it worked

Post Reply