MSFN Forum: Help with a batch file. - MSFN Forum

Jump to content



  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Help with a batch file. Rate Topic: -----

#1 User is offline   madbull 

  • Junior
  • Pip
  • Group: Members
  • Posts: 83
  • Joined: 21-November 07

  Posted 05 February 2008 - 05:14 PM

Wow haven't written/requested anything from the forum lately. Just a quick one. I used to be fammiliar with this but have forgotten it most likely. I need to use a batch file to create a text file containing the contentents of a folder. More basically put: Its a batch file/command that will tell me the contents of a folder and put it in a text file in a folder of my choosing.

Thans in advance,


#2 User is offline   cluberti 

  • Gustatus similis pullus
  • Group: Supervisor
  • Posts: 10,936
  • Joined: 09-September 01
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 05 February 2008 - 05:21 PM

If you create a batch file that does a dir /s, and output it to a text file, it should be sufficient.

dir /a /r /s x:\path\to\directory\you\want\listed\ > x:\path\to\text\file.txt


This will be all you need.

#3 User is offline   madbull 

  • Junior
  • Pip
  • Group: Members
  • Posts: 83
  • Joined: 21-November 07

Posted 05 February 2008 - 05:57 PM

View Postcluberti, on Feb 5 2008, 11:21 PM, said:

If you create a batch file that does a dir /s, and output it to a text file, it should be sufficient.

dir /a /r /s x:\path\to\directory\you\want\listed\ > x:\path\to\text\file.txt


This will be all you need.


Thanks that's exactly what i needed. Thanks again,

My other question is: Is there any way to create it or run another command etc so that the details wouldn't show, only the files.

So instead of 30/01/2008 04:16 735,145,984 28 Weeks Later.avi
it would be 28 Weeks Later

This post has been edited by madbull: 05 February 2008 - 06:08 PM


#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 05 February 2008 - 11:38 PM

It's just the /b switch you need
dir/b/a/s "x:\path\to\directory\you\want\listed">"x:\path\to\text\file.txt"
Or depending on what your output file is to be used for try this as a test:
tree "%programfiles%" /f /a>.\desktop\output.txt


#5 User is offline   madbull 

  • Junior
  • Pip
  • Group: Members
  • Posts: 83
  • Joined: 21-November 07

Posted 23 March 2008 - 04:52 AM

View PostYzöwl, on Feb 6 2008, 05:38 AM, said:

It's just the /b switch you need
dir/b/a/s "x:\path\to\directory\you\want\listed">"x:\path\to\text\file.txt"
Or depending on what your output file is to be used for try this as a test:
tree "%programfiles%" /f /a>.\desktop\output.txt


Thanks! Just what i needed,

#6 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 04 April 2008 - 05:35 PM

View PostYzöwl, on Feb 5 2008, 09:38 PM, said:

dir/b/a/s "x:\path\to\directory\you\want\listed">"x:\path\to\text\file.txt"


I saw this thread and wanted to ask a question on this. I'm new here, sorry if it's against the rules to ask a question from someone else's thread. I know some forums are like that.

BASICALLY, I like what the above code does, however I only want to see the list of files I have, without the path and also without the extension. Example:

Instead of seeing C:\Program Files\myfile.txt, I'd rather just see myfile.

Is this possible w/ a batch file or do I need to use a different language (VB)?

#7 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,239
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 04 April 2008 - 07:11 PM

for /f %f in ('dir /s /a /b c:\winpe') do echo %~nf


will do it, keep in mind it will also give you the directories, replace c:\winpe with the directory you want the list from. If you want the results in a file add >>filename.txt to the end of the line.

#8 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 04 April 2008 - 07:33 PM

View PostIcemanND, on Apr 4 2008, 05:11 PM, said:

for /f %f in ('dir /s /a /b c:\winpe') do echo %~nf


will do it, keep in mind it will also give you the directories, replace c:\winpe with the directory you want the list from. If you want the results in a file add >>filename.txt to the end of the line.


Doesn't seem to work. It's not producing an output file. Here's what I'm using.

for /f %f in ('dir /s /a /b "C:\Users\User1\Documents\Backup Files\Halo\Halo CE Maps\"') do echo %~nf >> C:\maplist.txt


#9 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,239
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 04 April 2008 - 07:45 PM

If you have it in a batch file double up the % signs %%f

And with the space in the path you need this:
for /f "delims=*" %f in ('dir /s /a /b "c:\users\user1\documents\backup files\halo\halo ce maps\"') do echo %~nf >> c:\maplist.txt


#10 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 04 April 2008 - 08:24 PM

View PostIcemanND, on Apr 4 2008, 05:45 PM, said:

If you have it in a batch file double up the % signs %%f

And with the space in the path you need this:
for /f "delims=*" %f in ('dir /s /a /b "c:\users\user1\documents\backup files\halo\halo ce maps\"') do echo %~nf >> c:\maplist.txt


Yes it's in a batch file of course, hence the name of the thread. Anyway, the above code isn't the right answer, but modifying it based on your comment (adding the second '%'), was. This is what ended up working:

for /f "delims=*" %%f in ('dir /s /a /b "C:\Users\user1\Documents\Backup Files\Halo\Halo CE Maps\"') do echo %%~nf >> c:\maplist.txt


Here's a couple things I don't like.
  • Is there a way to get it to run so you don't see the batch file outputting the data to the file on the screen?
  • I noticed that if I run the file again, it appends. Instead, I'd prefer it to overwrite the existing file.


#11 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,239
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 04 April 2008 - 08:50 PM

@echo off
echo.>maplist.txt
for /f "delims=*" %%f in ('dir /s /a /b "c:\users\user1\documents\backup files\halo\halo ce maps\"') do echo %%~nf >> c:\maplist.txt
exit


#12 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 04 April 2008 - 09:38 PM

View PostIcemanND, on Apr 4 2008, 06:50 PM, said:

@echo off
echo.>maplist.txt
for /f "delims=*" %%f in ('dir /s /a /b "c:\users\user1\documents\backup files\halo\halo ce maps\"') do echo %%~nf >> c:\maplist.txt
exit

Got the first problem solved. However, the file is still being 'appended'. Let me make myself clear. When I run the batch file, it exports the list to the file, and if I run it a second and third time, what I end up with is two and three sets of the list, so if I have 300 files, running the batch file 2 and 3 times would make me end up with 600 and 900 items in the file. To get what I want, I have to manually delete maplist.txt and THEN run my batch file. So basically I need the file to be opened for input, not for append, if that sounds right.

#13 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,239
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 04 April 2008 - 10:26 PM

Sorry, forgot to put the path to maplist.txt in. change echo.>maplist.txt to echo.>c:\maplist.txt

#14 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 04 April 2008 - 10:52 PM

View PostIcemanND, on Apr 4 2008, 08:26 PM, said:

Sorry, forgot to put the path to maplist.txt in. change echo.>maplist.txt to echo.>c:\maplist.txt


OK that worked. Here's something I noticed. Why am I getting a blank line at the top of my file? Just wondering. Not a big issue, just a small annoyance. Here's what I'm using now based on your most recent reply:

@echo off
echo.>C:\maplist.txt
for /f "delims=*" %%f in ('dir /s /a /b "C:\Users\user1\Documents\Backup Files\Halo\Halo CE Maps\"') do echo %%~nf >> c:\maplist.txt
exit


#15 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,239
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 04 April 2008 - 11:12 PM

the echo. puts the blank line, your other option would be to put

if exist c:\maplist.txt del c:\maplist.txt


which would delete the file so there would be nothing to append to.

#16 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 04 April 2008 - 11:22 PM

View PostIcemanND, on Apr 4 2008, 09:12 PM, said:

the echo. puts the blank line, your other option would be to put

if exist c:\maplist.txt del c:\maplist.txt


which would delete the file so there would be nothing to append to.


OK out of my 3 echo occurrences I had, I just took a guess at which one you meant and I got it to work correctly. Plus, the coding makes a little more sense to me. I figured deleting the file would be the way to go, anyway. Thanks!
@echo off
if exist c:\maplist.txt del c:\maplist.txt
for /f "delims=*" %%f in ('dir /s /a /b "C:\Users\user1\Documents\Backup Files\Halo\Halo CE Maps\"') do echo %%~nf >> c:\maplist.txt
exit


#17 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 05 April 2008 - 06:49 AM

This is how I'd do it:
@Echo off
>C:\maplist.txt Type Nul
Pushd C:\Users\User1\Documents\Backup Files\Halo\Halo CE Maps
For /f "delims=" %%# In ('dir/b/s/a-d') Do >>C:\maplist.txt Echo:%%~n#
Write an empty output file first, then perform the commands.

You'll notice I've used -d to select only files. This is because removing extensions from files and directories you'd be unable to differentiate which was which in your output file.

#18 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 05 April 2008 - 09:52 AM

Here is a VBS script that will list the Halo folder and write the text file you want.
I added 3 lines of code for making the numbers appear 001, 002 Etc.
Save As ListHalo.vbs

Quote

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim F1, F2, F3,TS
 Set TS = Fso.CreateTextFile("C:\maplist.txt")
 Set F1 = Fso.GetFolder(" C:\Users\User1\Documents\Backup Files\Halo\Halo CE Maps")
   For Each F2 In F1.Files 
	F3  = F3 + 1
	If Len(F3) = 1 Then F3 = "00" & F3 
	If Len(F3) = 2 Then F3 = "0" & F3 
	If Len(F3) = 3 Then F3 = F3 
	 TS.WriteLine F3 & " " & F2.Name
   Next
  TS.Close()


#19 User is offline   JOSHSKORN 

  • Newbie
  • Group: Members
  • Posts: 16
  • Joined: 17-March 07

Posted 05 April 2008 - 01:10 PM

View Postgunsmokingman, on Apr 5 2008, 07:52 AM, said:

Here is a VBS script that will list the Halo folder and write the text file you want.
I added 3 lines of code for making the numbers appear 001, 002 Etc.
Save As ListHalo.vbs

Quote

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim F1, F2, F3,TS
 Set TS = Fso.CreateTextFile("C:\maplist.txt")
 Set F1 = Fso.GetFolder(" C:\Users\User1\Documents\Backup Files\Halo\Halo CE Maps")
   For Each F2 In F1.Files 
	F3  = F3 + 1
	If Len(F3) = 1 Then F3 = "00" & F3 
	If Len(F3) = 2 Then F3 = "0" & F3 
	If Len(F3) = 3 Then F3 = F3 
	 TS.WriteLine F3 & " " & F2.Name
   Next
  TS.Close()



Well that's cool. It's always nice to see a different way of doing things. However, this code produced this error message and I'll just type it out:

Quote

Script: C:\Users\user1\Desktop\ListHalo.vbs
Line: 4
Char: 1
Error: Path not found
Code: 800A004C
Source: Microsoft VBScript runtime error.


The text file is created but blank. I'm interested in seeing what this code is suppose to do.

#20 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 05 April 2008 - 04:31 PM

It was a sloppy cut and paste there is a extra space in the path

Set F1 = Fso.GetFolder(" C:\Users\User1\Documents\Backup Files\Halo\Halo CE Maps")


Set F1 = Fso.GetFolder("C:\Users\User1\Documents\Backup Files\Halo\Halo CE Maps")


You have to make sure you have that path, or change it to what ever path you want
Set F1 = Fso.GetFolder("C:\WINDOWS\system32")


Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy