Solved

Run command in each subfolder using batch file

Posted on 2016-11-22
7
69 Views
Last Modified: 2016-11-22
Hi,

I could really so with some help please.

I'm trying to run a command where I wish to display the current foldername in a text file (this works fine using a command of echo %cd%)

In my parent folder I have over 500 sub folders, so I wish to run this command in each folder and display the answer in a text file in each folder and call it File2.txt, so far I have the following

for /d %%a in (C:\Test\*.*) do echo %cd% >File2.txt

Open in new window


However rather than creating the 500 File2 text files in the folders containing the foldernames I have 1 File2.txt file created in the parent directory (C:\Test\) which displays C:\Test\

Also, I'm not sure if its possible but would it be ok to only display the foldername in the textfile and not the full path so textfile would contain Example1 rather than C:\Test\Example1

Many thanks for your help.
0
Comment
Question by:Legolas786
7 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41897481
Here are a few options to try.  

If you just want the folder name of the top level subfolders then:

dir /ad /b "c:\temp\*.*">>c:\temp\all.txt


or

for /d  %%A in ("c:\temp\*.*") do echo %%~nA>>c:\temp\all.txt

If you want all subfolders at all levels then, with full path then:

for /r "c:\temp" %%A in (.) do echo %%~dpA

If you want all subfolders at all levels then, with just folder name then:

for /r "c:\temp" %%A in (.) do echo %%~nA


Hope this gives you some ideas.

~bp
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41897499
you don't need to use the For command to iterate, just use the /s switch on the Dir command.  It's wicked fast.
dir /ad /b /s "c:\temp\*.*" > c:\temp\all.txt

Open in new window

0
 

Author Comment

by:Legolas786
ID: 41897548
Thanks but those commands are not exactly what I was after.

The closest I have found is the following

for /d  %%A in ("c:\temp\*.*") do echo %%~nA>>c:\temp\all.txt

Open in new window


However this displays all the foldernames in the one text file, but what I am trying to achieve is create separate textfiles and place these in each subfolder.

So for example

The C:\Test\ Parent directory contains the following subfolders

C:\Test\Example1
C:\Test\Example2
C:\Test\Example3
C:\Test\Example4
C:\Test\Example5

When running the above command I get the following textfile created

C:\Test\all.txt

which contains the following

Example1
Example2
Example3
Example4
Example5

however I am trying to achieve the following:-

C:\Test\Example1\File2.txt which contains
Example1
C:\Test\Example2\File2.txt which contains
Example2
C:\Test\Example3\File2.txt which contains
Example3
C:\Test\Example4\File2.txt which contains
Example4
C:\Test\Example5\File2.txt which contains
Example5

Is this possible at all? many thanks
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 45

Expert Comment

by:aikimark
ID: 41897624
What are you trying to accomplish with this?
0
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 41897628
Here you go.
Change Target to your desired 'top' folder.
@echo off
setlocal enabledelayedexpansion
set Target=C:\local\test

for /f "tokens=1*" %%a in ('dir /b "%Target%"') do (
  cd "%Target%\%%a"
  echo %CD%
  echo %%a>file2.txt
)

Open in new window

0
 

Author Comment

by:Legolas786
ID: 41897630
To create a text file in each subfolder and that textfile contains the current folder name.
0
 

Author Closing Comment

by:Legolas786
ID: 41897640
Many thanks for your help
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question