running several CMD files within one batchfile - batch-file

I would like to make a generic script that runs every CMD-file on several subfolders. E.g.
Each subfolder contains 1 CMD file (name is not the same). It's not the same subfolders each time
c:\folder\Folder1
c:\folder\folder2
c:\folder\folder3
etc
So I would like to search all subfolders and run all CMD-files on each subfolder.

This is designed to:
1) look for every .bat file in a tree
2) change to each directory with a batch file, in turn
3) run the .bat file in the folder it is in
#echo off
for /r %%a in (*.bat) do (
pushd "%%~dpa"
call "%%a"
popd
)
If you want to run every .bat file in the tree on every folder:
#echo off
for /r %%a in (*.bat) do (
for /d /r %%b in (*) do (
pushd "%%b"
call "%%a"
popd
)
)

for /f %%a in ('dir /s /b *.cmd') do call %%a
dir /s /b *.bat builds a list of all cmd files
To run all those files in parallel, replace call with start

Related

copy specific folders and files using a batch file

I'm trying to create a batch file that will do the following:
I have multiple directories and in each one of those directories there is a folder called '06-2015'. I want to create a batch script that will go thru all of those directories and copy the folder '06-2015' and its files and nothing else.
Example:
C:\Files\Accounts\06-2015
C:\Files\Sales\06-2015
C:\Files\IT\06-2015
Is there a way I can create a script that will go something like:
xcopy C:\Files\*\06-2015 C:\Backup\*\06-2015 /s
Or is there a different/better way to do this?
A wildcard characters allowed only in the last path item.
#ECHO OFF
SET "target=06-2015"
IF NOT EXIST c:\backup\%target% MKDIR c:\backup\%target%
FOR /F "delims=" %%G IN ('DIR /B /AD "c:\files"') DO (
if exist "c:\files\%%G\%target%\" (
:: create backup directory if necessary
MKDIR "c:\backup\%%G\%target%\" 2>NUL
XCOPY /S /E /Y "c:\files\%%G\%target%\" "c:\backup\%%G\%target%\"
)
)
Test this: remove the /s and /e if you don't want any folders inside 06-2015 copied.
#echo off
for /d /r "c:\files" %%a in (06-2015?) do (
if /i "%%~nxa"=="06-2015" xcopy "%%a" "C:\Backup\%%~pnxa\" /s/h/e/k/f/c
)

How to zip all sub folders inside folder with a specific name

I am looking to find all folders with the name "Logfile" inside slightly different folder structures. For example how would I find the Logfile folder inside C:\ECU\ECU1\Logfile, C:\ECU\ECU2\Logfile, C:\ECU\ECU3\Logfile and C:\ECU\ECU4\Logfile? I then want to zip the .txt contents of this folder in each case. I currently have a batch file running which allows me to zip the contents of a folder which has the same folder structure each time but need to combine the above all together. Any help would be great...
Thanks.
#echo off
pushd "C:\ECU\ECU2" || goto :eof
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT,*.cpi) DO DEL "C:\ECU\ECU2.cpi*" "%%A"
popd
Try this as a test first to see if it prints the correct folders:
#echo off
for /d /r "c:\ecu" %%a in (target*) do (
if /i "%%~nxa"=="target" (
echo "%%a"
)
)
pause
and if it's ok then this should work - test it with dummy files, but your DEL command is odd in the first term. I replaced it with what might work.
#echo off
for /d /r "c:\ecu" %%a in (target*) do (
if /i "%%~nxa"=="target" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.TXT* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT *.cpi) DO DEL "%%A"
popd
)
)

batch file to create a txt file showing a list of folders inside each subdirectory of a parent folder

I need the correct way to get a simple process to repeat till it reaches end of folder. Inside my Main Folder are multiple subfolders. Inside each of these are more subfolders along with a few files.
I need to run the batch from inside the Main folder and have it enter each subfolder in turn and simply run "dir *. > %date%.txt", then do the same to the next subfolder till all are done.
The only part I cannot get to work right is the change directory to each in turn till all are done.
Thanks
#echo off
for /f %%D in ('dir /b /s /ad') do (
pushd %%D
dir *. >"%date%.txt"
popd
)
to append to a existing file you should use >>, try this:
cd /d X:\main &rem put the path to your main folder here
for /r /d %%i in (*) do dir "%%~fi\*.">>"%date%.txt"
This reports all subfolder from X:\main and there subfolder recursively.
Try these options, if you weren't aware of them:
dir "c:\main folder">"%date%.txt"
dir /a-d "c:\main folder">"%date%.txt"
dir /b /s /a-d "c:\main folder">"%date%.txt"
dir /b /s /ad "c:\main folder">"%date%.txt"

windows batch to recursively scan files in 1 level subfolders

I have many projects (let's say D:\Projects\), each of which has it's own directory. In some of directories (not all) there is bat file (sonar-runner.bat). I want to write windows batch script to run this .bat files sequentialy for each subfolders:
for /r %%i in (sonar-runner.bat) do (
#call "%%i"
pause
)
pause
However using this approach I faced problem where the same files in inner directories are executed (for example D:\Projects\Main\Inner\sonar-runner.bat). I don't want this to happen. I only want to look for file through projects folders (let's say subfolders of 1st level). Could anybody tell me how can I achieve that?
Edit
I have tried another approach:
for /D %%i in (*) do (
for %%j in (%%i\sonar-runner.bat) do (
#call "%%j"
pause
)
)
pause
But i'm not sure about performance in that case.
try this:
for /d /r "D:\Projects" %%i in (*) do (
pushd "%%~i"
echo(%%i|findstr /r "^.:\\[^\\]*\\[^\\]*$" >nul&&call sonar-runner.bat
popd
)
This is much more efficient.
#echo off
pushd "d:\folder"
for /f "delims=" %%a in ('dir /ad /b ') do (
pushd "%%a"
if exist "sonar-runner.bat" call "sonar-runner.bat"
popd
)
popd
pause

Running a .bat only within sub-directory from a .bat in root directory?

I have a series of commands that moves files from folders A-E to one folder X, e.g.:
for /f "delims=" %%a in (
'dir /s /b ^| find /i "\Folder A\"'
) do move "%%a" "G:\Folder X"
for /f "delims=" %%a in (
'dir /s /b ^| find /i "\Folder B\"'
) do move "%%a" "G:\Folder X"
etc
and once they're in X, running a .bat within X to sort those files based on their names into folders by name:
for /f "tokens=2 delims= " %%b in ('dir /b *.pdf') do (
md %%b > nul 2>&1
move *%%b*.pdf %%b
)
but it seems to want to run the .bat within X in the root folder instead of only \X\. Is there any way to tell it to run X\*.bat only within X? Aside from changing *.pdf to X\*.pdf? I'm trying to write so that each .bat is as portable as possible.
If I understand you right, the only problem is that the current directory doesn't change, and you'd like to change it to G:\Folder X when you run the batch file in that folder.
You could do this from the "parent" batch file:
pushd "G:\Folder X"
call "G:\Folder X\process.bat"
popd
Or from within the "child" batch file you could force it to always set its current directory to the directory containing the batch file:
pushd "%~dp0"
:: do your stuff
popd

Resources