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"
)
Related
I need to delete specific files from 28 folders on the same server.
e.g
C:/folder/DMP/app_x0
C:/folder/DMP/app_x1
C:/folder/DMP/app_x2
DeleteList.txt has a list of files names (with path).
C:/folder/DMP/app_x0/ABC1.txt
C:/folder/DMP/app_x0/ABC1.doc
The batch file needs to have a loop to go through each folder one by one and delete all files mentioned in a text file. Following worked ok for one folder only if I specify the full path before each file's names in DeleteList.txt file.
for /f "delims=" %%f in (DeleteList.txt) do del "%%f"
How to use above so that same code could run 28 times in batch file but each time replaces folder location path. DeleteList.txt will not change.
Any sample code/suggestion would help.
Thx.
One of these questions where we need to read between the lines.
Given we have on file of full filenames with the incorrect path separator, and a requirement to delete the files from 28 directories (but there's no list) then I conclude that since the required subdirectories shown are all subdirectories of C:\folder\DMP\ then the requirement actually is to delete all of the files that match the filenames in the file in all subdirectories of the parent directory of the full filename in the list.
So my suggestion would be
for /f "delims=" %%b in (DeleteList.txt) do del /s "%%~dpb..\%%~nxb"
Naturally, you should test this on a dummy tree before implementing it.
del /s deletes in the target directory and all subdirectories. The target subdirectory is the drive and path of %%b; its parent (..); the name and extension part of %%b.
I prefer to use metavariables that are not available as metavariable-modifiers.
To delete in subdirectories but not in the parent:
for /f "delims=" %%b in (DeleteList.txt) do FOR /d %%c IN ("%%~dpb..\*") DO del /s "%%c\%%~nxb">nul
This time, %%c is set to all subdirectorynames in turn, and the del/s applied to each subdirectory. The /s (...and subdirectories) probably isn't required. The >nul suppresses the deletion report if desired.
Not sure if the OP has need for this anymore, but you can use the following batch script to delete the specific files from multiple folders, starting from a parent folder.
#echo off
for /r "Drive:\path\to\parent_folder" %%d in (.) do (
del "%%~d\ABC1.txt"
del "%%~d\ABC1.doc"
)
The script will loop through the parent directory and its sub-folders, and delete only the specific files from the folders that they appear.
In the case of the post, the parent folder can be "C:\folder".
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"
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 parent folder with lots of folders (movies) underneath. Each folder has 1 file (the actual movie).
I would like some advice on a batch script that I can run to rename the folders to the file (movie) within excluding the extension (.avi)
e.g.
BEFORE
Parent (folder)
Folder 1
Movie 1.avi
Folder 2
Movie 2.avi
AFTER
Parent (folder)
Movie 1
Movie 1.avi
Movie 2
Movie 2.avi
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*" '
) DO (
FOR %%b IN ("%sourcedir%\%%a\*") DO IF /i "%%~na" NEQ "%%~nb" ECHO(REN "%sourcedir%\%%a" "%%~nb"
)
GOTO :EOF
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
You would need to set your directory as sourcedir. I set my testing directory.
The following batch file does the job:
#echo off
if exist "%TEMP%\RenameFolders.bat" del "%TEMP%\RenameFolders.bat" >nul
cd /D "Complete\path\to\parent\folder"
for /R "Complete\path\to\parent\folder" %%I in (*.avi) do echo ren "%%~dpI" "%%~nI">>"%TEMP%\RenameFolders.bat"
if exist "%TEMP%\RenameFolders.bat" (
call "%TEMP%\RenameFolders.bat"
del "%TEMP%\RenameFolders.bat" >nul
)
"Complete\path\to\parent\folder" must be modified twice in the batch file by real parent folder path.
The main job is done by command FOR. Run in a command prompt window for /? to get information about option
/R ... recursive folder/file scan, and
%%~dpI ... drive and path of file, and
%%~nI ... name of file without path and file extension.
For each *.avi file the command echo is executed to create the rename command used later to rename the folder containing the *.avi file to the name of the *.avi file.
It is not possible to do the folder rename directly within the FOR loop as it is not possible to rename a folder while it is the current working directory for a running process. Each folder is the current working directory for the batch process while FOR is running.
The command CD in third line just makes sure that the working directory is set to parent folder of all the subfolders to rename to avoid that renaming a folder fails because the batch file is started from one of the subfolders.
The second line just makes sure there is no file RenameFolders.bat in the folder for termporary files for example when terminating this batch file once by click on button X while command FOR is currently running.
After command FOR processed recursively all *.avi files and a line with command ren was appended to file RenameFolders.bat for each file, this created batch file is called which renames now the folders.
Last the batch file temporarily created is deleted as not needed anymore.
I'm trying to create a batch file that will execute all of the batch files located in a folder. So I have a folder that contains 6 (will increase) batch files, and I want to use a for loop to iterate through the list. An easier way of understanding it would be like so:
for batchFile in folder:
CALL batchFile
cd Desktop
Each batch file should start after the other finishes. Also, batch files will be the only type of files in the folder, so there shouldn't need to be any kind of separation. It may sound odd, but both the call and the directory change need to be inside of the loop (due to directory changes in each of the batch files).
This is the closest thing I've found for getting a start on this:
for /r %%i in "DIR" do CALL
Where "DIR" is the folder directory. However, it 1) doesn't work and 2) would only do a CALL and not also a directory change. Does anyone know how to do this?
Also, I'm using Windows 7.
Thanks.
EXTENSION:
Doing this in a bat file will work:
CALL "WelcomeScreenLists.bat"
cd directory
CALL "WelcomeScreenLinks.bat"
cd directory
CALL "RightClick.bat"
cd directory
CALL "RibbonButtons.bat"
cd directory
pause
"directory" is the full path of the folder containing the bat files. I would like to turn this into a for loop that loops through all files in the folder.
I would think you'd want something like this:
for /f "delims=" %%i in ('dir /b ".\*.bat"') do (
CALL %%i
)
This works from the current directory. You can hard-code a directory by replacing the first "." in the dir statement with the full directory name, like so:
for /f "delims=" %%i in ('dir /b "c:\folder\subfolder\*.bat"') do (
CALL %%i
)
The "for /f" syntax will iterate over file names, the "dir" command is any valid dir command... you can use any options to control sort order, for example.