Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Run command in each subfolder using batch file

Posted on 2016-11-22
7
Medium Priority
?
155 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 58

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 46

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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
LVL 46

Expert Comment

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

Accepted Solution

by:
NVIT earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

618 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