Running a code in the terminal environment on all folders inside a specific folder - cmd

Please guide me how can I run a code in cmd environment on all folders (not files) in a specific folder
FOR %A IN (%Specified folder%) DO %A vina.exe --config conf.txt --log log.txt
?= For example, run this code on all the folders located in the specified folder: vina.exe --config conf.txt --log log.txt ( The code is related to vina software, which is in its file (vina.exe) in all folders )
FOR %A IN (%Specified folder%) DO %A vina.exe --config conf.txt --log log.txt

Related

Dir command unexpected behaviour

So if I run the command line from a folder (c:/users/USER/desktop/FOLDER1) and type:
dir /b /a-d *.txt *.bin
it will display the files of the folder with the extension *.txt or *.bin. Perfect.
Then, if I run the terminal from any other location (either its default or say, from another folder on another drive, e.g. D:\FOLDER2) and type the command:
dir /b /a-d "c:/users/USER/desktop/FOLDER1"
then it will display, correctly, a list of all the files in the FOLDER1.
BUT, lastly, if on the above command I add the parameters for the extensions, all hell brakes loose (running the terminal from the D:\FOLDER2):
dir /b /a-d *.txt *.bin "c:/users/USER/desktop/FOLDER1"
Then it displays the files from both the c:/users/USER/desktop/FOLDER1 AND D:\FOLDER2 (of the extensions asked and also some more random files with extensions not of the ones asked).
The only work around I've found is to cd to the drive I want:
c:
then cd to the folder:
cd c:/users/USER/desktop/FOLDER1
then run the actual command:
dir /b /a-d *.txt *.bin
And then it will display the correct files. But this is so troublesome and "ugly". I would expect a single command to do it.
What is happening? Am I doing something wrong? I've tried all possible combinations in the ordering of the parameters.

Bash's "for" for windows

How to can I to write this linux-like loop in the Windows 7 command line?
for docker_path in `ls | grep "docker$"`
do
cd $docker_path
mvn -B -f pom.xml clean deploy -Pdocker
cd ..
done
I need found all *docker/ directory, exec there mvn-command and return into patern directory, but for Windows7 system.
On Windows command line use:
for /D %I in (*docker) do #pushd "%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
Use the following command line if mvn is not an executable, but a *.bat or *.cmd file:
for /D %I in (*docker) do #pushd "%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
Well, on command prompt it is not really necessary to use command CALL to run a batch file in a loop together with two other commands as done here.
The first command line for usage in a batch file:
#for /D %%I in (*docker) do #pushd "%%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
In a batch file the loop variable I must be referenced with two percent signs instead of just one as on command prompt. The loop variable can be only a single character.
The second command line for usage in a batch file:
for /D %%I in (*docker) do #pushd "%%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
In a batch file it is necessary to use command CALL to call another batch file and continue with next command respectively command line in current batch file after execution of the other batch file finished.
Windows command processor cmd.exe continues execution of current batch file on the other batch file on not using command CALL and exits batch file processing once the execution of other batch file finished without further processing command lines in current batch file.
See answer on How to call a batch file that is one level up from the current directory? for details on the existing methods to run a batch file from within a batch file.
The single command line can be also coded with multiple lines:
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
mvn.exe -B -f pom.xml clean deploy -Pdocker
popd
)
And the same multi-line batch file solution in case of mvn is a batch file.
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
call mvn.bat -B -f pom.xml clean deploy -Pdocker
popd
)
Command FOR with option /D searches with wildcard pattern *docker for non hidden directories in current directory of which directory name ends with the string docker.
It is advisable on Windows to reference a file to execute with complete name of file, i.e. file name + file extension. This makes it clear for Windows command processor as well as every reader of the code if the executed file is an executable or a script file which makes a difference as it can be seen here.
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.
call /?
echo /?
for /?
popd /?
pushd /?
See also:
Single line with multiple commands using Windows batch file
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line

What is the equivalent of mkdir -p on Windows 10?

I've been unable to find help files on Windows for things like MD, mkdir or new-item. Doing update-help just throws errors.
How do I do mkdir -p on Windows 10 in Command Prompt?
I want to create a directory even if that directory already exists.
mkdir of Windows 10 does not support -p or /p or an equivalent flag.
If you want realize the -p functionality according to Unix operating systems you can do that in one line as follows:
set MKDIR_DIR=D:\XYZ\123\ABC && if not exist %MKDIR_DIR% mkdir %MKDIR_DIR%
Replace D:\XYZ\123\ABC with the desired directory.
Use mkdir /? to get information on the command. -p is not a flag in Windows, parent/intermediate directories will be created if command extensions are turned on.
See this Microsoft documentation for more information.
Since mkdir in Windows 10 (even with command extensions enabled) does not the job, here is the content of my mkdir.bat that DOES it for a %path% at a %drive% :)
#ECHO OFF
SETLOCAL enableextensions
SETLOCAL enabledelayedexpansion
REM ## Create directory structure levelwise since there is nothing like "mkdir -p"
REM ## substitute "\" with ";"
SET "pathParts=%path:\=;%"
REM ## iterate over a list of the tokens between " ", ",", ";" or "="
SET pathToMake=%drive%
FOR %%d IN (%pathParts::= %) DO (
SET pathToMake=!pathToMake!\%%d
ECHO !pathToMake!
IF NOT EXIST !pathToMake! MKDIR !pathToMake!
)
Just call it like that:
D:\>SET drive=D:
D:\>SET path=XYZ\123\ABC
D:\>mkdir.bat
D:\XYZ
D:\XYZ\123
D:\XYZ\123\ABC
And et voilĂ : D:\XYZ\123\ABC exists :)
There is very easy solution in windows. -p is not required at all.
If you want to create nested folders then write like below:
mkdir \Taxes\Property\Current
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mkdir
On Windows 11 Pro using bash this command did the job for me:
mkdir.exe -p folder/subFolder
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/mkdir

Is there a way, from the cmd prompt, to do a dir of files/folders that are only in the path environment variable?

I want to search for a file on my C: drive from the cmd prompt. I can go to the root folder cd \ and then use dir/s myfile.txt.
But I want to just be able to search only those folders that are in my path environment variable.
Can that be done?
where myfile.txt
The search is done in the current active directory and in all the folders enumerated in the path variable
Yes, with the following command:
for %a in ("%path:;=";"%") do cd %~a && dir /b /s myfile.txt

two input variables in a simple bat file

I am trying to write my first bat file. Sorry......
I need to input a source directory (dir) for another bat file to find test files and a destination directory (log) for the bat file to output its results.
The bat file that does the processing is called fits.bat.
-i Indicates that a file or directory to process will follow
-o Directs the FITS output to a file
I have this:
#echo off
cd c:\program files\fits\fits-0.8.0
SET /P dir=enter directory of source files
SET /P log=enter directory for log files
fits.bat -i %dir -o %log
pause
But I get no output at all.
If I type individual commands in command prompt window , it works and files in dir folder are correctly read and output is sent to the log folder
try this....
#echo off
cd c:\program files\fits\fits-0.8.0
set /p dir=enter directory of source files
set /p log=echo enter directory for log files
fits.bat -i %dir% -o %log%
pause
when you call variables you need a % in front and back
%VAR%
You need to surround your two variables in percent signs:
#echo off
cd c:\program files\fits\fits-0.8.0
SET /P dir=enter directory of source files:
SET /P log=enter directory for log files:
fits.bat -i %dir% -o %log%
pause

Resources