Solved

Run command in each subfolder using batch file

Posted on 2016-11-22
7
23 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 51

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

Expert Comment

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now