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
command start interprets first string in double quotes as optional title - run in a command prompt window start /?
for details;
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 /?
:
"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.
/D"K:\PROJÉČŤ\A B1"
... defines the working directory for the new command process.
"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.