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"
Related
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.
I am new to programming, thanks to the new task my boss has provided.
I am trying to run a batch file to zip multiple files in a folder separately.
So, I want file1.txt to zip to file1.zip and so on for other files.
I have only the following code:
for /f "tokens=*" %f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%f.7z" "%f"
My issue: When I run it on cmd after navigating to the target folder, it works, but when I store it in a batch file and run it from the target folder, it wont work.
Please help me identify what and where I need to make changes in my code.
Regards
AK
WIthin a batch file, the metavariable (loop-control variable) requires the % to be doubled, so replace each %f with %%f when you mean the loop-control variable"f"`
Your code seems fine. The issue is, that when you run it inside a batch file, you need to put an extra % mark.
In the batch try:
for /f "tokens=*" %%f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%%f.7z" "%%f"
I have a problem on copying a file because of name of one directory in directory tree varies.
The directory tree is: D:\folder\Unknown Folder\myfile.rar
I want to copy the RAR file inside D:\folder\ containing only one folder.
The name of this folder varies and is therefore unknown for me.
I want that the batch script opens D:\folder\, then find and open first subfolder and finally copies the RAR file myfile.rar.
Something like this:
copy "D:\folder\*\myfile.rar" "D:\a.rar"
For each folder under d:\folder, if the searched file exists, copy to target folder
for /d %%a in ("d:\folder\*") do if exist "%%a\myfile.rar" copy "%%a\myfile.rar" d:\a.rar
To use it from command line, replace all %% with %
copy does not support wildcards in the path.
MC ND's answer is good enough , but you can try also this:
for /f "delims=" %%a in ('dir /b /s /a:-d "D:/folder/" ^|findstr /i /e /c:"/myfile.rar"') do (
copy "%%a" d:\a.rar
)
I have a case where I have N number of sub folders under a parent folder X. The folder names are not in any order.
I have a requirement where I have to go into each of these subfolders and perform a specific task or run a command (for example create a text file called new.txt).
How can I do this?
Put the script bellow in a bat file and place it in the root directory tree. Run it. It will create file new.txt in all folders. You can replace this command or add others. %%a is the folder path.
#echo off
for /F "tokens=*" %%a in ('dir /B /S /AD') do (
rem command to execute for each folder
type nul >"%%a\new.txt"
)
I'm trying to write a batch file that will find all files with a csproj extension in a large hierarchy of directories.
Wherever I find one, I want to then create a new SameName.csproj.user file, and put some hard coded text in it. i.e 'test text'
How can this be done in a batch script?
Not tested:
#echo off
:: put your dir here
pushd c:\cproj_dir
for /f "delims=" %%f in ('dir /b /s /a-d *.csproj') do (
echo test text >"%%~dpfnxf.user"
)