Loop through files in subdirectories batch file - batch-file

I am trying to loop through a bunch of files in sub-directories to run a command on them, but I can't seem to figure out how to reach them.
The directory structure is like this:
Main directory (Holds .bat file and top-level directories)
Sub-directory
Search directory
A bunch of files (I need the locations of all of these separately)
I am able to get a list of all of the files/folders in the "Main directory" using:
for /f %%f in ('dir /b /r *') do echo %%f
but I cannot seem to figure out how to go any further into the directories to get the files in the "Search" directory.
An example of the file structure is:
C:\Users\swiftsly\Search160\0002\search\AP584.txt
Any help is greatly appreciated!

The final solution to this question, with the help of the recommendation by Squashman to use /R instead of /f is:
#echo off
for /R %%f in (*.txt) do echo %%f
This will print out a list of all of the .txt files in the sub-directories.

Related

organise files based on words in the file and create a directory and put them in there

I have about a few thousand files in a folder. Each filename has a few different version's of the file so for example. tree.blue, tree.green, tree.orange, cat.black, cat.green,cat.white.
what i would like to do is have a .bat file to put all tree or any word before the "." to auto create a directory and add any file related to tree for example in it as well as for all the other filenames without specifying the word.Currently I have found code on here which almost does it but have to specify the keyword in the code for it to work.
Assuming each file is in the same format, something like this would work:
pushd "path_to_folder"
for /f "tokens=1* delims=." %%g in ('dir /b') do (
if not exist "%CD%\%%g" #echo md "%%g"
#echo copy "%CD%\%%g.%%h" "%CD%\%%g\%%g.%%h"
)
popd
You can remove the #echo once you confirm the system will be creating the right directory name and copying the files to the right location.

Recursively append folder name to the files in Windows batch file

I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.
C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt
C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt
C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt
The above files should be renamed like below:
C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt
C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt
C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt
Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.
So far below is what I wrote:
for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do ECHO rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
The rename command is merely displayed using ECHO for debugging purposes. To make it operational, remove word ECHO (no sooner than debugged).
Moreover, I'd consider checking whether a particular file is already renamed…

Run all programs file whose same name by batch file

I have a folder which contains many sub-folders, each this sub-folder contains a program file named bk.bat in its root.
I want to write a bat file so that I can execute all these bk.bat files.
Please help.
Mine is folder structure
Create a batch file in your WWW folder and put this code in it.
FOR /F "delims=" %%G IN ('dir /a-d /b /s bk.bat') DO CALL "%%~G"

How to merge .sql files listed in a .txt into a single .sql script using batch?

I've searched high and low for a solution to this problem but have so far drawn a blank. I'm fairly new to batch scripts, so apologies if this turns out to be a stupid question.
I have a list of sql filenames in a .txt file which I would like to merge into a single sql script. The .txt file and the .bat file are in one subdirectory of a root location, and the sql scripts are in a separate subdirectory like so:
root\
batch files\
.bat
.txt
views\
.sql
I can read the file names out the .txt file quite happily and pass them to a subroutine using this:
for /f %%d in (IncludeList.txt) do call :Include_List "%%d"
goto :eof
The part that is causing me problems is the :Include_List subroutine. I need to be able to search the views\ subdirectory for each specified filename and then copy the content of it into a new script.
I tried the following, which I think is the nearest I've got it to working, without it actually working:
:Include_List
for /r %%f in ('DIR /B /S ..\Views\vw*.sql ^| find /i %~1') do type %%f >> _All_views.sql
It seems to be treating each part of this ('DIR /B /S ..\Views\vw*.sql ^| find /i %~1') as a filename and then failing to find any of them. It's also checking inside the batch files\ directory and not the views\ directory.
One additional requirement, is that this is part of a larger batch file, which would need to continue processing after the files had been looped through and written to the newly created _all_views.sql file.
Is what I am trying to achieve even possible with a batch file? Or am I just going to have to manually add each required file into the batch file like this
type ..\Views\[filename].sql >> _All_views.sql
Based on wOxxOm's answer my final solution is as follows:
setlocal EnableDelayedExpansion
for /f %%d in (IncludeList.txt) do (
for /f "delims=" %%f in ('DIR /B /S ..\Views\vw*.sql ^| find /i "%%d"') do type %%f >> _All_views.sql
)
I did away with the subroutine as it was causing issues when placed inside a larger batch file, but a nested for loop works perfectly for what I need.

How to write a Batch Script that searches for files based on list

Can anyone assist me in writing a batch script that searches for file names and outputs the location of the files to a text file. Example I have a file called list.txt located in a folder, C:\LocateFiles\list.txt. Located in the list.txt file are about 25 file names that I wish to determine if they are anywhere on the C:\ drive. If it locates any of the file names identified in the file list.txt it will output the path of all files found to a single file in C:\LocatedFiles\results.txt.
A million thanks,
Johnny Mac
#ECHO OFF
FOR /F %%F IN (C:\LocateFiles\List.txt) DO DIR /s/p/b %%F.* >> C:\LocateFiles\finds.txt
Save that as LocateFiles.cmd and place it in whichever directory you wish to search, note that C:\ is very large and will take quite a while! as in, forever, seriously, i really wouldnt, your call...
the file finds.txt will have the entire path for any file that matches up to the file names listed in List.txt
Also note, this finds files of any extension, but the filename itself must match exactly to whats in List.txt
The solution below search the files in the current directory just once, so it run faster.
#echo off
dir /S /B /A-D | findstr /I /G:C:\LocateFiles\list.txt > C:\LocatedFiles\results.txt
EDIT: New method added
The method below may run even faster. It is necessary to complete a timing test.
#echo off
setlocal EnableDelayedExpansion
rem Read file names from file list and assemble a long string with this format:
rem "filename1.ext*" "filename2.ext*" ...
set "fileList="
for /F "delims=" %%a in (C:\LocateFiles\list.txt) do set fileList=!fileList! "%%a*"
rem Search the files from current directory downwards
(for /R %%a in (%fileList%) do echo %%a) > C:\LocatedFiles\results.txt

Resources