Please help me create a txt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Please help me create a txt

#1 Post by goymerac » 12 Sep 2012 19:22

Hi. I will start by admitting I love what batch files do but have no idea how they do it. I am trying to modify an existing .bat to do what I wish, but it is more difficult than I anticipated. I am attempting to create a .bat that will write a .txt to the active directory that lists the contents. I would like it to put a label at the top which is selected from an existing .txt then list the contents of the directory and delete anything from the list that is a .txt or .bat and then put the amount of free space at the bottom.
I would like a .txt that looks like this
"Title" I have a Title.txt that I'd like to be able to select from, it reads as listed below.

"directory contents"
"directory contents"
"directory contents"
Sometimes there are other .txt or .bat files here that I do not wish to be listed

"space free" Free

Title.txt contents
[1] "Title1"
[2] "Title 2"
[3] "Title 3"
as so on
I'd like the dos window to prompt me to select the appropriate title by selecting the associated number or give me the option to add a new title if desired.
I really appreciate any help anyone can give.
Sincerely,
Mike "The Batch Idiot" G

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#2 Post by abc0502 » 13 Sep 2012 02:58

So what you need is a list of all files exist in the directory where the batch is and this list contain all files except the txt & bat files.

But do you want this list to be exported to a txt file after display on screen?
and do you want to list folder too?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#3 Post by abc0502 » 13 Sep 2012 04:16

NOTE
I changed the Code, it now can take as many as titles you want, without the need to change any codes.
BUT The titles must be in that form "[number] title", the number is the title order and must be between [ ]


Title.txt:

Code: Select all

[1] First Title
[2] First Title
[3] First Title
[4] First Title
[5] First Title
[6] First Title
[7] First Title
[8] First Title


The batch:

Code: Select all

@echo off & cls & Title goymerac

:main

cls
setlocal enabledelayedexpansion

:: Display Title Names from Title.txt
echo.
echo    Select Title:
echo    -------------

For /F "tokens=*" %%A in ('type "Title.txt"') Do echo      %%A

:: Set Each Title to a variable (unlimited titles).
echo.
set "num="
set /p "num=Which Title: "
if not defined num goto main

:: set the default printer
for /f "skip=2 tokens=*" %%a in ('find /I "!num!" "Title.txt"') do set Final_Title=%%a

:: Display Result
:display

cls
echo.
echo !Final_Title:~4!
echo.

:: Get a List of ALL files EXCEPT txt and bat files.
:: to prevent display of folders chanag below (line 49) for command to: For /F "tokens=*" %%A in ('Dir /B /A:-D "*.*"') Do (
For /F "tokens=*" %%A in ('Dir /B /A:D "*.*"') Do (
   set files=%%A
   IF not "!files:~-3!" == "txt" (
      IF not "!files:~-3!" == "bat" (
         echo  ^> !files!
      )
   )
)

:: Get total Free space and set it to variable %free%
For /F "tokens=3,4,5 delims= " %%A in ('dir "*.*"') Do set free=%%A %%B

echo.
echo    Total Free Space: !free!


:: Time to wait before making a log and then exit is about [5]sec
ping localhost -n 5 >nul

:: Make Log file of the output
>>Directory_log.log call :display

Exit /B


This code will list folders as well, to prevent that see line (31).
The Title.txt must be in the same folder as the batch, to put it in differant folder, must write the full location in the for commands in line (13 and 21)
Last edited by abc0502 on 13 Sep 2012 09:12, edited 2 times in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#4 Post by abc0502 » 13 Sep 2012 06:38

I edited the code above

goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Re: Please help me create a txt

#5 Post by goymerac » 13 Sep 2012 08:54

Thank you for your reply. I am trying to list all the information in a txt that is created with the batch onto the current directory. I am trying to list only the folders in the current directory.

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

Re: Please help me create a txt

#6 Post by foxidrive » 13 Sep 2012 09:05

This will give you a list of the folders.


Code: Select all

@echo off
dir /b /ad >dirlist.txt

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#7 Post by abc0502 » 13 Sep 2012 09:11

ok , i changed the code up

goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Re: Please help me create a txt

#8 Post by goymerac » 13 Sep 2012 09:23

I'm almost there. It won't let me select a title from my Title.txt file that's on my c drive in the same place as my .bat file. It is putting
~4 at the top, and is there a way to show the free space in GB?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#9 Post by abc0502 » 13 Sep 2012 09:54

how is the titles in the title.txt file are written?

goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Re: Please help me create a txt

#10 Post by goymerac » 13 Sep 2012 10:02

[1] Title 1
[2] Title 2

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#11 Post by abc0502 » 13 Sep 2012 10:15

it should be working i tested again and it give me the correct title, but about the free size it a bit difficult to convert it to GB, cause i get the total from the cmd window when you write in it DIR command

write this and tell me the result instead of "!Final_Title:~4!" write "!Final_Title!" without the quotes

goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Re: Please help me create a txt

#12 Post by goymerac » 13 Sep 2012 10:26

It places an empty line at the top and on the second line
ECHO is off.

the free space issue could be solved if there is a way to do simple math on !free! by dividing it by 1073741824?

goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Re: Please help me create a txt

#13 Post by goymerac » 13 Sep 2012 10:28

It works if I put the title.txt in the cd but it is on my c: and that is where i am running my .bat from too.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Please help me create a txt

#14 Post by abc0502 » 13 Sep 2012 10:30

goymerac wrote:It works if I put the title.txt in the cd but it is on my c: and that is where i am running my .bat from too.

what do you mean with put title.txt in the cd

goymerac
Posts: 15
Joined: 12 Sep 2012 19:03

Re: Please help me create a txt

#15 Post by goymerac » 13 Sep 2012 10:33

current directory
sorry still bad with commands

Post Reply