Solved

Run command in each subfolder using batch file

Posted on 2016-11-22
7
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 54

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
batch file or script 4 64
powershell switch statement 7 46
Help to debug powershell script 5 57
Need a script that will create its own directory to dump logs into 7 44
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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