windows batch to recursively scan files in 1 level subfolders - batch-file

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

Related

Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)

Compress separately files within subfolders

Hi all and thanks for the answers,
Firstly, I tried to find the answer to my problem but I did not find anything.
I have a tree of folders and sub-folders and I want to use 7zip to compress the files within those folders separately.
I have got this piece of code from this very website, it does what I want to get but it places the compressed files on the main folder:
set extension=.*
for /R %%a in (*%extension%) do "%sevenzip%" a -mx "%%~na.zip" "%%a"
I wonder if I can get a zip file of every file and have it in the sub-folder containing the source file. Or doing the process above and place every zip file inside the appropriate sub-folder.
I tried with a double 'For /d' but I was unable to get it:
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set
sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set
sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
for /D %%O in (*) do (
for /R %%I in ("%%O\*") do (
"%sevenzip%" a -mx "%%~na.zip" "%%a"
:: rd /s /q "%%I" **Because I do not want to delete anything by now.
)
)
Again, thank you.
Alex.
If you have somewhat complex folder structure then you probably better use plain list from dir:
dir /a:-d /s /b /o
Just use its output in for:
for /f %%f in ('dir /a:-d /s /b /o') do (
echo %%f <-- %%f is a full path to a file, do something with it
)
Btw, 7zip has useful option -sdel to remove the source file when archive has been created successfully.
This is the final code:
#echo off
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
#echo searching...
for /R %%I in (*) do (
"%sevenzip%" a -mx -mmt4 "%%I.7z" -r -x!*.bat "%%I"
)
del "Compressing_files_7zip.bat.7z"*
del *.7z.7z
del *.zip.7z
::::::::::::::::::::::::::For setting up shutdown 60' after the end of the process.Remove colons in the line below.
::shutdown.exe /s /t 3600
pause
Thanks all for the support, especially to Frost.
For a simpler version without using 7zip:
for /f %%f in ('dir /a:-d /s /b /o *.mdb') do (
zip -r -p "%%f.zip" "%%f"
)

Batch file that creates folder with wildcard in path

I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?
Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"
Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"

batch to rename multiple files

I have some files that I need to rename and as the volume is quite big, I was thinking of using a batch file since all I need to do is:
u_ex140429.log >> u_ex140429_01.log
But all the codes I found either place the _01 at the beginning or at the end of the file:
_01u_ex140429.log or u_ex140429.log_01
If there is a thread somewhere here about this, I do apologize for I have been looking for quite a while.
Thanks for your help guys.
This adds _01 before the extension of each .txt filename in the current folder without the limitation of a plain for-in-do command.
#echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do ren "%%a" "%%~na_01%%~xa"
Try using this.
#echo off
for %%f in (*.*) do (move %%f %%~nf_01%%~xf )
technet
a for loop works just fine ...
#echo off
dir /b ¦ find /v /c "*">filescount.txt
for /f %%f in (filescount.txt) do (
for /L %%n in (1 1 %%f) do (
for %%a in (*.*) do (
rename "%%a" "%%~na_%%n%%~xa"
)
)
)
place the batch file in the directory containing your files to be renamed

Moving Files up one Directory

I'm looking to make a batch script that will move all images in sub subfolders down to the layer above the batch script. For Example:
comics\move.bat
comics\series 000\series 000 blah\01.jpg,02.jpg, etc.
comics\series 001\series 001 blah\series blah\01.jpg,02.jpg, etc.
into
comics\series 000\01.jpg,02.jpg, etc.
comics\series 001\01.jpg,02.jpg, etc.
I've tried several variations of
for /r %%i in (*.jpg) do move "%%~fi" "%%~pi*.jpg"
But it won't do anything. One way I had it would move all images into the same folder as I was running the script from but that's the closest I've gotten. A clean up of the now empty folders would be nice too!
This should move the images in each branch into the first level folder, but doesn't deal with filename collisions.
Test it on a folder of copies of your files.
#echo off
for /d %%a in (*) do (
pushd "%%a"
for /r %%b in (*.jpg) do move "%%b" .
popd
)
pause
for /L %%a in (1,1,5) do for /d /r %%b in (*) do rd "%%b" 2>nul
The last line should remove only empty folders up to 5 levels deep, if they exist.
Create a .bat file with following content on the root folder and double click it.
#echo off
for /f "usebackq tokens=*" %%d in (`dir /s /b /o:n /ad`) do (
cd "%%d"
for /f "usebackq tokens=*" %%f in (`dir /b /o:n /a-d`) do (
move /y "%%f" ..
)
)
pause
To explain the code, the outer loop iterates directories recursively and cd into it. The inner loop iterates all files inside the directory and move it one up above. /y flag on move is to suppress prompting. Use dir /? for explanation of all the dir flags. As for the for part I took it from https://stackoverflow.com/a/2768660/179630
Try this:
move comics\series 000\series 000 blah\*.jpg comics\series 000

Resources