Solved

Run command in each subfolder using batch file

Posted on 2016-11-22
7
59 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

773 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