Fumbling a Nested Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
flywire
Posts: 17
Joined: 14 Feb 2018 03:10

Fumbling a Nested Batch File

#1 Post by flywire » 15 Feb 2018 04:46

My batch file generates a directory, files and a batch file which generates a batch file. Download SetupNestBat.Bat and run it as a demo.

I have a few issues:
  • SetupNestBat.Bat does not write the '%%', '%1' or '%2'
  • Using filename without the extension (ie I want Best1.dta not Best1.dat.dta)
  • Inserting a '>' in batch file comment lines without redirecting the line (eg for Usage line)
  • Appending a '> nul' in the output rather than a second redirection on the line (eg after redirected copy lines
  • Echoing lines to con while output directed to file (eg done.)

SetupNestBat.Bat

Code: Select all

:: Create NestBat.bat and demo files
::
:: Ver 15/02/2018 (CC BY-SA 4.0)
::
mkdir NestBat
cd NestBat 
echo @echo off > NestBat.bat
echo ::
echo :: Usage: NestBat [File (without extension) [File (without extension)]] > Batling.bat >> NestBat.bat
echo :: >> NestBat.bat
echo :: Ver 15/02/2018 (CC BY-SA 4.0) >> NestBat.bat
echo :: >> NestBat.bat
echo echo Generating ... >> NestBat.bat
echo :: >> NestBat.bat
echo For %%f in (%1*.dat) do ( >> NestBat.bat
echo   echo ::>> NestBat.bat
echo   echo ::*** %%f >> NestBat.bat
echo   echo copy %%f DAT >> NestBat.bat
echo   echo copy %%f.dta DTA >> NestBat.bat
echo   echo copy %%f.dtt DTT >> NestBat.bat
echo :: >> NestBat.bat
echo   For %%g in (%2*.per) do ( >> NestBat.bat
echo     echo ::** %%g >> NestBat.bat
echo     echo copy %%g.per PER >> NestBat.bat
echo     echo Program >> NestBat.bat
echo     echo copy OUT %%f%%g.out >> NestBat.bat
echo     echo :: >> NestBat.bat
echo   ) >> NestBat.bat
echo   echo :: >> NestBat.bat
echo ) >> NestBat.bat
echo echo done. >> NestBat.bat
echo :End >> NestBat.bat
:: Test Files
echo > Best1.dat
echo > Best1.dta
echo > Best1.dtt
echo > Test2.dat
echo > Test2.dta
echo > Test2.dtt
echo > Test3.dat
echo > Test3.dta
echo > Test3.dtt
echo > High.per
echo > Low.per
echo > OUT
cd ..
Proper NestBat.bat run as: Nestbat >Batling.bat

Code: Select all

@echo off 
@echo off 
:: Usage: NestBat [File (without extension) [File (without extension)]] > Batling.bat 
:: 
:: Ver 15/02/2018 (CC BY-SA 4.0) 
:: 
echo Generating ...
:: 
For %%f in (%1*.dat) do ( 
  echo ::
  echo ::*** %%f 
  echo copy %%f DAT 
  echo copy %%f.dta DTA 
  echo copy %%f.dtt DTT 
  echo :: 
::   
  For %%g in (%2*.per) do ( 
    echo ::** %%g 
    echo copy %%g.per PER 
    echo Program 
    echo copy OUT %%f%%g.out 
    echo :: 
  ) 
  echo :: 
) 
echo done. 
:End 
Run Batling.bat as: Batling >Batling.log
Last edited by flywire on 15 Feb 2018 21:33, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Fumbling a Nested Batch File

#2 Post by aGerman » 15 Feb 2018 11:57

Every single special characters that you want to redirect has to be escaped.
^ to ^^
< to ^<
> to ^>
| to ^|
& to ^&
% to %%

Steffen

flywire
Posts: 17
Joined: 14 Feb 2018 03:10

Re: Fumbling a Nested Batch File

#3 Post by flywire » 15 Feb 2018 13:38

Well on the way now. The code is set up for very easy downloading and testing. After downloading SetupNestBat.Bat
Proper NestBat.bat as best downloded as NestBat2.bat to allow comparison with the generated NestBat.bat.
aGerman wrote:
15 Feb 2018 11:57
Every single special characters that you want to redirect has to be escaped.
... % to %%
Thanks, escaped special character is clearer now but I've already tried the % one in SetupNestBat.Bat but it makes a real mess of things.

flywire wrote:
15 Feb 2018 04:46
Using filename without the extension (ie I want Best1.dta not Best1.dat.dta)
for %%f in (*.bat) do echo %%~df%%~pf%%~nf
Ref: Microsoft Command-line reference A-Z: For

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Fumbling a Nested Batch File

#4 Post by aGerman » 15 Feb 2018 15:56

aGerman wrote:
15 Feb 2018 11:57
Every single special characters that you want to redirect has to be escaped.
Pretty clear, isn't it?
%%f to %%%%f
%%~df to %%%%~df
etc.

And of course you can shorten %%~df%%~pf%%~nf to %%~dpnf as in the reference that you linked.

Steffen

flywire
Posts: 17
Joined: 14 Feb 2018 03:10

Re: Fumbling a Nested Batch File

#5 Post by flywire » 15 Feb 2018 21:29

Thanks Steffen - the previous failure of %%%% must have been another issue and I missed the %~dpnf - :D cool!

Any advice on redirecting the output to a file from the command line but forcing messages (eg done.) to the console?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Fumbling a Nested Batch File

#6 Post by aGerman » 16 Feb 2018 11:54

flywire wrote:
15 Feb 2018 21:29
Any advice on redirecting the output to a file from the command line but forcing messages (eg done.) to the console?
Not quite sure what you're talking about. You mean someting like that?

Code: Select all

call "test.bat" >"test.log"
In that case everything that test.bat writes to StdOut will be redirected to the log file. Any error messages written to the StdErr will not be redirected. You can redirect a certain message from the StdOut (stream id 1) to the StdErr (stream id 2) e.g. if test.bat contains the line

Code: Select all

1>&2 echo done
Steffen

Post Reply