1

I'm working with multiple .bat files on multiple locations and I would like to connect whole work in one job. So here's the issue:

I have folder called A A1 and inside there is .bat file which is working with file which is also in the same folder. Then there is folder A B1 with same setup. Example:

  • W:
    • PROJÉČŤ
      • A A1
        • @ START BAT.bat
        • FILE.avi (which is 60 MB)

and

  • K:
    • PROJÉČŤ
      • A B1
        • @ START BAT.bat
        • FILE.avi (which is 80 MB)

Please note the different drives!

So at this point I would like to run W:\PROJÉČŤ\A A1\@ START BAT.bat and when this batch file has executed all written commands, I want to automatically start second batch file K:\PROJÉČŤ\A B1\@ START BAT.bat from the first one W:\PROJÉČŤ\A A1\@ START BAT.bat

My batch scripts:

W:\PROJÉČŤ\A A1\@ START BAT.bat

IF EXIST "*.avi" (
    IF NOT EXIST "FILE.avi" (
        REN "*.avi" FILE.avi
    )
    ATTRIB -R "FILE.avi"
    IF EXIST "FILE.avi" (
        ATTRIB -R "FILE.avi"
    )
)
REM ???
PAUSE
EXIT 

K:\PROJÉČŤ\A B1\@ START BAT.bat

IF EXIST "*.avi" (
    IF NOT EXIST "FILE.avi" (
        REN "*.avi" FILE.avi
    )
    ATTRIB -R "FILE.avi"
    IF EXIST "FILE.avi" (
        ATTRIB -R "FILE.avi"
    )
)
PAUSE
EXIT

I think I just need to replace the line with REM ??? with a command like

START "K:\PROJÉČŤ\A B1\@ START BAT.bat"

But the listed commands below have unfortunately not worked:

START "K:\PROJÉČŤ\A B1\@ START BAT.bat"
CALL "K:\PROJÉČŤ\A B1\@ START BAT.bat"
START CALL "K:\PROJÉČŤ\A B1\@ START BAT.bat"
START cmd /K CALL"K:\PROJÉČŤ\A B1\@ START BAT.bat"

The batch file in directory K:\PROJÉČŤ\A B1 was not executed from within batch file in directory W:\PROJÉČŤ\A A1.

Any ideas, please?

player0
  • 124,011
  • 12
  • 67
  • 124

2 Answers2

0

It is fine to have spaces in your file name, and @ is not a character that needs escaping. That said, it is much easier sometimes to name your batch files without special characters or spaces and use dashes between words like my-cool-script.bat.

Here is an example that should mimic what you are doing that should help you debug your scripts.

Directory of L:\test

12/27/2015  11:12 AM    <DIR>          .
12/27/2015  11:12 AM    <DIR>          ..
12/27/2015  11:12 AM               118 @ start one.bat
12/27/2015  11:08 AM                56 @ start two.bat

L:\test>"@ start one.bat"
ONE
Running "@ start one.bat"
Calling @ start two.bat
TWO
Running "L:\test\@ start two.bat"
Bye from "L:\test\@ start two.bat"
Bye from "@ start one.bat"

And here are contents of the batch files:

L:\test>type *.bat

@ start one.bat


@echo off
echo ONE
echo Running %0
echo Calling @ start two.bat
call "L:\test\@ start two.bat"
echo Bye from %0

@ start two.bat


@echo off
echo TWO
echo Running %0
echo Bye from %0

This also works if the files are in different directories:

Directory of L:\test

12/27/2015  11:35 AM    <DIR>          one
12/27/2015  11:35 AM    <DIR>          two

Directory of L:\test\one

12/27/2015  11:35 AM               116 @ start one.bat

Directory of L:\test\two

12/27/2015  11:35 AM                54 @ start two.bat

L:\test>"one\@ start one.bat"
ONE
Running "one\@ start one.bat"
Calling @ start two.bat
TWO
Running "L:\test\two\@ start two.bat"
Bye from "L:\test\two\@ start two.bat"
Bye from "one\@ start one.bat"

And the updated contents of the first bat file:

L:\test>type "one\@ start one.bat"
@echo off
echo ONE
echo Running %0
echo Calling @ start two.bat
call "L:\test\two\@ start two.bat"
echo Bye from %0

Another update to show how to reference the current directory of the script and not just "current directory" of where the script started.

%~dp0 is the key here...

L:\test>dir /s
Directory of L:\test
12/27/2015  03:12 PM    <DIR>          one
12/27/2015  03:12 PM    <DIR>          two

Directory of L:\test\one
12/27/2015  03:17 PM               170 @ start one.bat
12/27/2015  03:12 PM                 8 file.txt

Directory of L:\test\two
12/27/2015  03:17 PM               104 @ start two.bat
12/27/2015  03:12 PM                13 file.txt

ONE

@echo off
echo ONE
echo Running %0
echo From Directory: %~dp0
echo Calling @ start two.bat
dir "%~dp0\file.txt"
call "L:\test\two\@ start two.bat"
echo Bye from %0

TWO

@echo off
echo TWO
echo Running %0
echo From Directory: %~dp0
dir "%~dp0\file.txt"
echo Bye from %0

OUTPUT

L:\test>"one\@ start one.bat"
ONE
Running "one\@ start one.bat"
From Directory: L:\test\one\
Calling @ start two.bat

Directory of L:\test\one

12/27/2015  03:12 PM                 8 file.txt
           1 File(s)              8 bytes

TWO
Running "L:\test\two\@ start two.bat"
From Directory: L:\test\two\

Directory of L:\test\two

12/27/2015  03:12 PM                13 file.txt
           1 File(s)             13 bytes
Bye from "L:\test\two\@ start two.bat"
Bye from "one\@ start one.bat"

L:\test>

You should have all you need to fix your scripts.

Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • well what you described is "running two bats from same folder/location" so... not really my case tho – player0 Dec 27 '15 at 19:33
  • I updated my example to 2 different directories, and `call "L:\test\two\@ start two.bat"` or `call "W:\test\two\@ start two.bat"` will work the same. Have you tried to run my examples on your machine to solve the original question you asked about how to call a 2nd script from the 1st one? – Kory Gill Dec 27 '15 at 20:09
  • yeah I tried out your example but it's not what I seek for because that 2nd bat is executed trought first bat instead new separate execution. in your example I would work with FILE.avi (which is 60mb) and then it would autostart 2nd batch which would work also with FILE.avi (which is 60mb) instead of FILE.avi (which is 80mb) – player0 Dec 27 '15 at 21:41
  • OK, that was not your original question, but I see that was the problem you might have been trying to solve. I will update to my answer to show you how to use variable modifiers to ensure you are using a path to your files instead of just "file.txt". `for /?` has good examples of how to use these. – Kory Gill Dec 27 '15 at 23:20
0

Do not name a batch file like an internal command of Windows command processor or a standard Windows console application stored in system32 directory of Windows. @ START BAT.bat is definitely not a good name for a batch file because it contains command start, has spaces in file name and @ is also very unusual in file names which is the reason why this character was selected many years ago as separator in email addresses.


The task renaming (hopefully) one AVI file not named FILE.avi to FILE.avi in each subfolder of "W:\PROJÉČŤ and "K:\PROJÉČŤ and remove read-only attribute can be done with the following batch code saved into AviFileRename.bat:

@echo off
call :AviFileRename "W:\PROJÉČŤ"
call :AviFileRename "K:\PROJÉČŤ"
goto :EOF

:AviFileRename
for /R %1 %%# in (*.avi) do (
    if /I "%%~n#" == "FILE" (
        %SystemRoot%\System32\attrib.exe -r "%%#"
    ) else (
        ren "%%#" "FILE.avi"
        %SystemRoot%\System32\attrib.exe -r "%%~dp#FILE.avi"
    )
)

It does not matter in which directory AviFileRename.bat is saved with those code lines.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • attrib /?
  • call /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • ren /?

Command call can be used usually to continue processing of a main batch on a sub-batch, and when processing this sub-batch finished, continue processing of main batch. For details see answer on How to call a batch file in the parent folder of current batch file?

But command exit without option /B results in exiting entire command process, not just processing of current batch file. For details see answer on In a Windows batch file, can you chain-execute something that is not another batch file? And read the help output on running in a command prompt window exit /?. The solution is using command start to run the batch file in a new command process.

But just using

START "K:\PROJÉČŤ\A B1\@ START BAT.bat"

is not enough because

  1. command start interprets first string in double quotes as optional title - run in a command prompt window start /? for details;

  2. the current directory is not set to the directory of the started batch file as required for the lines in @ START BAT.bat in K:\PROJÉČŤ\A B1.

A working command line would be:

start "Processing A B1" /D"K:\PROJÉČŤ\A B1" "K:\PROJÉČŤ\A B1\@ START BAT.bat"

This command put into W:\PROJÉČŤ\A A1\@ START BAT.bat instead of line with command REM would work.

Explanation of the parameters, please refer to output of start /?:

  1. "Processing A B1" ... title for the new process window. Could be also an empty string specified with "". But something readable for a human as window title is always better than a console window with no title. A title string is required if any other parameter is specified on the command line with double quotes.

  2. /D"K:\PROJÉČŤ\A B1" ... defines the working directory for the new command process.

  3. "K:\PROJÉČŤ\A B1\@ START BAT.bat" ... the command / executable / script to start is in this case the batch file @ START BAT.bat in directory K:\PROJÉČŤ\A B1.

You may want to add additionally the start parameter /wait after /D"K:\PROJÉČŤ\A B1" to run the batch file in the new command process and halt processing of W:\PROJÉČŤ\A A1\@ START BAT.bat always until started batch file finished and command process was exited by command exit or when last line of batch file was processed by command processor.


But there is one more problem with folder name PROJÉČŤ. It contains the non ASCII characters ÉČŤ and therefore it is important that in text editor used for editing the batch file the correct code page is set as otherwise directory PROJÉČŤ would not be found. The code page in a console window depends on Windows region and language settings.

Running in a command prompt window chcp displays the code page defined for command processes on your machine. This (usually) OEM code page must be used also in text editor and not the ANSI (Windows) code page usually used by text editor for editing text files.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Well, I've read whole `start /?` and I still can't tell where's `"Processing "` comes from. Is that some kind of unmentioned subcommand? – player0 Dec 30 '15 at 00:46
  • The __title__ string is optional. But __start__ interprets first double quoted string always as title string and therefore it is often required to define explicitly a title string. Run from within a command prompt window `start "K:\PROJÉČŤ\A B1\@ START BAT.bat"` and you can watch that instead of running this batch file, just a new console window opens with title `K:\PROJÉČŤ\A B1\@ START BAT.bat`. That's the reason for the title string `"Processing A B1"` which can be also `""` or `"Judge Dredd"`. – Mofi Dec 30 '15 at 10:25
  • thank you! your suggestion of abandoning "@" character, using `CHCP 1250 > NUL 2>&1` and `START "" /D"<folder>" /WAIT "<path>" /ABOVENORMAL` solved my issue</path></folder> – player0 Dec 30 '15 at 12:28
  • just wondering, what would I need to do to execute batch that is in subfolder? tried: `START "subfolder name is AA" /D"AA" /WAIT "AA\start bat.bat"` but with no luck – player0 Jan 03 '16 at 00:11
  • The current directory is with `/D"AA"` the subfolder `AA` for new command process. Starting from this folder now `"AA\start bat.bat"` in new command process will not work as there is most likely no more subfolder `AA` within current folder `AA`. You need: `START "subfolder name is AA" /D"AA" /WAIT "start bat.bat"`. The batch file is either referenced with full path or with path relative to directory set as working directory for new command process. – Mofi Jan 03 '16 at 16:29