Set Colum for every file to txt [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AlphaInc.
Posts: 21
Joined: 15 Apr 2021 08:15

Set Colum for every file to txt [SOLVED]

#1 Post by AlphaInc. » 15 Apr 2021 09:03

Hello everybody,

I'm working on a batch-script which first counts all files in a directory and then creates a colum like the one blow for every file. For example if there are 11 files there are 11 Columns with the exception that X is replaced by an ascending number.

Code: Select all

---------------------------------------------------------------------------
File X
---------------------------------------------------------------------------

File-Name...............:
File-Size...............:

---------------------------------------------------------------------------
After that it should set File-Name and file-size in Bytes but in the format XXX.XXX.XXX Bytes

The Code I have so far is this:

Code: Select all

echo off

for /f "delims=|" %%f in ('dir /b %CD%') do (
	setlocal
	set file="%%f"
	set maxbytesize=1000
	FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
	echo --------------------------------------------------------------------------- 
	echo File X
	echo ---------------------------------------------------------------------------
	echo\
	echo File-Name...............: %%f
	echo File-Size...............: %size%
	echo\
	echo ---------------------------------------------------------------------------
	)> text.txt
exit
Currently the Script produces only column and the script doesn't record the file size into the text-file.
Last edited by AlphaInc. on 08 Aug 2021 13:17, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Set Colum for every file to txt

#2 Post by Aacini » 16 Apr 2021 04:40

Do you mean: "one COLUMN for every file? Or it should be "one GROUP OF LINES for every file"...

You said: "a batch-script which first counts all files in a directory and then creates a colum like the one blow for every file". Where is the "first counts all files in a directory" section? It does not appear in your code...

Antonio

PS - Please, before post any answer, carefully read this topic that is located at the very beginning of this forum...

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

Re: Set Colum for every file to txt

#3 Post by Compo » 16 Apr 2021 07:22

Ignoring the 'maxbytesize', which seems to be unused, perhaps something like this is what you want:

Code: Select all

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"
(
    For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D 2^>NUL') Do (
        Set /A i += 1, # = %%~zG
        SetLocal EnableDelayedExpansion
        For /F "Tokens=* Delims=." %%H In (
            "!#:~-15,-12!.!#:~-12,-9!.!#:~-9,-6!.!#:~-6,-3!.!#:~-3!") Do (
            Echo --------------------------------------------------------------------------- 
            Echo File !i!
            Echo ---------------------------------------------------------------------------
            Echo(
            Echo File-Name...............: %%G
            Echo File-Size...............: %%H
            Echo(
            Echo ---------------------------------------------------------------------------
        )
        EndLocal
    )
) 1>"text.txt"
Please take account that if you really are outputting to a file named 'text.txt' in the same directory as you're enumerating, then it will also be included within its own listing. It would also, as it is still being written to, show the incorrect filesize. My advice would be to use an alternative path, e.g. 1>"S:\omewhereElse\text.txt", but if you want it to remain in the current directory, but not listed, then the simplest way would probably be to use findstr:

Code: Select all

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "output=text.txt"
Set "i=0"
(
    For /F "EOL=? Delims=" %%G In (
        'Dir /B /A:-D 2^>NUL ^| FindStr /I /V "%output%"') Do (
        Set /A i += 1, # = %%~zG
        SetLocal EnableDelayedExpansion
        For /F "Tokens=* Delims=." %%H In (
            "!#:~-15,-12!.!#:~-12,-9!.!#:~-9,-6!.!#:~-6,-3!.!#:~-3!") Do (
            Echo --------------------------------------------------------------------------- 
            Echo File !i!
            Echo ---------------------------------------------------------------------------
            Echo(
            Echo File-Name...............: %%G
            Echo File-Size...............: %%H
            Echo(
            Echo ---------------------------------------------------------------------------
        )
        EndLocal
    )
) 1>"%output%"

AlphaInc.
Posts: 21
Joined: 15 Apr 2021 08:15

Re: Set Colum for every file to txt

#4 Post by AlphaInc. » 16 Apr 2021 09:14

Compo wrote:
16 Apr 2021 07:22
Ignoring the 'maxbytesize', which seems to be unused, perhaps something like this is what you want:

Code: Select all

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"
(
    For /F "EOL=? Delims=" %%G In ('Dir /B /A:-D 2^>NUL') Do (
        Set /A i += 1, # = %%~zG
        SetLocal EnableDelayedExpansion
        For /F "Tokens=* Delims=." %%H In (
            "!#:~-15,-12!.!#:~-12,-9!.!#:~-9,-6!.!#:~-6,-3!.!#:~-3!") Do (
            Echo --------------------------------------------------------------------------- 
            Echo File !i!
            Echo ---------------------------------------------------------------------------
            Echo(
            Echo File-Name...............: %%G
            Echo File-Size...............: %%H
            Echo(
            Echo ---------------------------------------------------------------------------
        )
        EndLocal
    )
) 1>"text.txt"
Please take account that if you really are outputting to a file named 'text.txt' in the same directory as you're enumerating, then it will also be included within its own listing. It would also, as it is still being written to, show the incorrect filesize. My advice would be to use an alternative path, e.g. 1>"S:\omewhereElse\text.txt", but if you want it to remain in the current directory, but not listed, then the simplest way would probably be to use findstr:

Code: Select all

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "output=text.txt"
Set "i=0"
(
    For /F "EOL=? Delims=" %%G In (
        'Dir /B /A:-D 2^>NUL ^| FindStr /I /V "%output%"') Do (
        Set /A i += 1, # = %%~zG
        SetLocal EnableDelayedExpansion
        For /F "Tokens=* Delims=." %%H In (
            "!#:~-15,-12!.!#:~-12,-9!.!#:~-9,-6!.!#:~-6,-3!.!#:~-3!") Do (
            Echo --------------------------------------------------------------------------- 
            Echo File !i!
            Echo ---------------------------------------------------------------------------
            Echo(
            Echo File-Name...............: %%G
            Echo File-Size...............: %%H
            Echo(
            Echo ---------------------------------------------------------------------------
        )
        EndLocal
    )
) 1>"%output%"
Thank you very much, that's what I was looking for :)

Post Reply