[SOLVED] Output txt based on directories/subdirectories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
_mononoke_
Posts: 4
Joined: 26 Mar 2014 02:19

[SOLVED] Output txt based on directories/subdirectories

#1 Post by _mononoke_ » 26 Mar 2014 02:34

Hello everyone, here's my problem:

I'd like to generate multiple output texfiles in batch, based on directories+subdirectories (one level below) with timestamp in it.

Okay, I realize it's not so easy to understand, so here's a concrete example of a folder/subfolder setting and the output that I'd like:

I:\Directory A\Subdirectory 1\...
I:\Directory A\Subdirectory 2\...
I:\Directory B\Subdirectory 1\...
I:\Directory B\Subdirectory 2\...

For this previous directory setting, I'd like to craft a batchfile that will generate a text file for each subdirectory, named in that fashion:
[Directory A] Subdirectory 1.txt
[Directory A] Subdirectory 2.txt
[Directory B] Subdirectory 1.txt
[Directory B] Subdirectory 2.txt

I manage to craft this following batchfile that works perfectly, but it will only list the main directories, not the first subdirectories:

Code: Select all

for /d %%i in (*) do echo %date% > I:\listing\%%i.txt


Someone could help please?
Last edited by _mononoke_ on 26 Mar 2014 12:38, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Output txt based on directories/subdirectories

#2 Post by foxidrive » 26 Mar 2014 06:23

Give this a crack.

Code: Select all

@echo off
cd /d "i:\"
md "listing" 2>nul
for /d %%a in (*) do (
    for /d %%b in ("%%a\*") do (
        if /i not "%%a"=="listing" >"\listing\[%%a] %%~nxb.txt" echo %date%
    )
)
pause

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Output txt based on directories/subdirectories

#3 Post by Compo » 26 Mar 2014 08:55

A two character very simple fix!
for /r /d %%i in (*) do echo %date% > I:\listing\%%i.txt

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output txt based on directories/subdirectories

#4 Post by Squashman » 26 Mar 2014 09:20

Compo wrote:A two character very simple fix!
for /r /d %%i in (*) do echo %date% > I:\listing\%%i.txt

Wouldn't you need to strip the drive letter and backslashes from %%i ?

_mononoke_
Posts: 4
Joined: 26 Mar 2014 02:19

Re: Output txt based on directories/subdirectories

#5 Post by _mononoke_ » 26 Mar 2014 10:11

Ok, I've tried your bat foxidrive.
It does exactly what I want!
Except that it lists the content of the I:\ drive.

I'm sorry for being a bit confusing in my previous post, but I'd like the batch to list the folders it is in (not I:\ specifically)

I'm sure you'll be able to fix it accordingly, thanks very much for your help!

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Output txt based on directories/subdirectories

#6 Post by Compo » 26 Mar 2014 10:18

Squashman wrote:
Compo wrote:A two character very simple fix!
for /r /d %%i in (*) do echo %date% > I:\listing\%%i.txt

Wouldn't you need to strip the drive letter and backslashes from %%i ?
No but the OP may!

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output txt based on directories/subdirectories

#7 Post by Squashman » 26 Mar 2014 11:54

Well maybe I am not understanding something but I believe your code will not work then because your variables would expand to this.

echo Wed 03/26/2014 > I:\listing\I:\Directory A\SubDirectory 1.txt

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output txt based on directories/subdirectories

#8 Post by Squashman » 26 Mar 2014 11:55

_mononoke_ wrote:Ok, I've tried your bat foxidrive.
It does exactly what I want!
Except that it lists the content of the I:\ drive.

I'm sorry for being a bit confusing in my previous post, but I'd like the batch to list the folders it is in (not I:\ specifically)

I'm sure you'll be able to fix it accordingly, thanks very much for your help!


remove this line or change it to what you need it to be.

Code: Select all

cd /d "i:\"

_mononoke_
Posts: 4
Joined: 26 Mar 2014 02:19

Re: Output txt based on directories/subdirectories

#9 Post by _mononoke_ » 26 Mar 2014 12:05

Hello Squashman,

When I remove the line altogether, the batch file won't do anything, and when I replace i:\ with the actual path, it says path incorrect.

Thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Output txt based on directories/subdirectories

#10 Post by Squashman » 26 Mar 2014 12:25

_mononoke_ wrote:Hello Squashman,

When I remove the line altogether, the batch file won't do anything, and when I replace i:\ with the actual path, it says path incorrect.

Thanks

We can't see what you tried to change it to. If you make code changes then post the entire code you are using.

_mononoke_
Posts: 4
Joined: 26 Mar 2014 02:19

Re: Output txt based on directories/subdirectories

#11 Post by _mononoke_ » 26 Mar 2014 12:38

Actually I dunno what I did, I must have forgotten a symbol or something...
It works like a charm!

Thanks very much guys for your (very fast) help, I appreciate it very much.
Keep up the good work.


I changed the topic name to [SOLVED]

Post Reply