Page 1 of 1

Set Colum for every file to txt [SOLVED]

Posted: 15 Apr 2021 09:03
by AlphaInc.
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.

Re: Set Colum for every file to txt

Posted: 16 Apr 2021 04:40
by Aacini
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...

Re: Set Colum for every file to txt

Posted: 16 Apr 2021 07:22
by Compo
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%"

Re: Set Colum for every file to txt

Posted: 16 Apr 2021 09:14
by AlphaInc.
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 :)