# Need a Wscript, or Batch script made



## pure_evil020 (Jul 31, 2008)

Hi, I'm wanting to know if someone could do me up a script preferably with "wscript", to search a directory and its sub-directories for music files, and list each file found in a text file with their directories.

the text file should end up something like this:


```
"C:\musicfile1.mp3"
"C:\documents and settings\musicfile2.mp4"
"C:\documents and settings\my documents\musicfiles3.m4a"
```
etc etc

the script should search for files with the following extensions: mp3, mp4, m4a, WAV, AACc, MOD, OGG, S3M, it, XM, AIFF

Would someone be able to write me up a script for this please?


----------



## Squashman (Apr 4, 2003)

simple one liner at the command prompt


```
H:\>dir /b /s *.mp3 *.mp4 *.m4a >>myfilelist.log
```


----------



## Squashman (Apr 4, 2003)

You can do this with Karen's Directory printer as well. But it would probably be easier to just do the one liner at the command prompt.
http://www.karenware.com/powertools/ptdirprn.asp


----------



## Ent (Apr 11, 2009)

Shouldn't it be a single >, so that the same file isn't written a number of times if the command is used more than once?

```
dir /b /s [COLOR=Lime]*.mp3 *.mp4 *.m4a[/COLOR] > [COLOR=Red]myfilelist.log[/COLOR]
```
And just to clarify, 
The *.mp3 and so on are the file extensions you'll use, so you'll need to add *.wav and the rest from your list.
myfilelist.log is the name of the file. 
This will find all files in the current folder, (in the whole H drive in Squashman's case). If you put it into a batch file it would run from the folder that batch file is in.


----------



## pure_evil020 (Jul 31, 2008)

Thanks for that Squashman and Ent.
I tried out your batch codes, and it seems to be creating a blank text file instead.

Here's the code I used for my specific situation:

```
C:\Documents and Settings\Tom\My Documents\My Music\>dir /b /s *.mp3 *.mp4 *.m4a >>myfilelist.log
```
I thought maybe the directory wasn't being defined properly, so I tried adding quote marks around the directory, but that just makes the batch generate an error, and quit before it does anything.

The directory "My Music" has dozens and dozens of music files, including mp3, and mp4 files, and also has sub-directories that have music files in them, but the batch isn't writing any of them to the text file.

Any ideas why?

EDIT: Ent's code worked fine if I placed the batch in the directory I wish to search through and list, so it has to have something to do with the start of the code where it specifies the directory to execute the dir command in.

P.S. It would be allot better if I could do this with a wscript, as I can launch a wscript silently without having a black window pop up.


----------



## Ent (Apr 11, 2009)

That "start of the code" up to the \> shouldn't be typed. It's what the command line uses to tell you what directory you're in already, *not *what you use to tell it where to run. If using a batch file put a Pushd or CD line above it to specify the directory.


```
[COLOR=Red]@echo off[/COLOR]
[COLOR=Lime]pushd "C:\Documents and Settings\Tom\My Documents\My Music"[/COLOR]
[COLOR=Blue]dir /b /s *.mp3 *.mp4 *.m4a > myfilelist.log[/COLOR]
```

Tells the command line to stop annoying you by repeating each instruction back to you (use this only with a batch file)
Tells the command line that all following commands should take place in that directory
The code you actually need to run

Alternatively you can specify the folder in the dir command.

```
[COLOR=Red]dir /b /s [/COLOR][COLOR=Lime]"C:\Documents and Settings\Tom\My Documents\My Music"[/COLOR] [COLOR=Blue]*.mp3 *.mp4 *.m4a[/COLOR][COLOR=Purple] > myfilelist.log[/COLOR]
```
Command to list files in the specified directory and subdirectories
Tells which directory to use
Tells which extensions to consider
Tells which file to put the list into

vbs or another WScript language would be harder as it isn't designed with file systems in mind to the same extent as batch is. I'll have a quick look at it, but I can't spend too long on this problem.


----------



## Ent (Apr 11, 2009)

Here is a vbs script that does what you want. It doesn't display anything, and creates a file once it's finished. As you can see it's much easier to get a batch file to handle files and folders. 
I note that there is not WMA in your list of desired extensions; It isn't in the script, but I mention it in case it was accidentally omitted.

As is this will run against the directory the script is in. If you want to force it to run against your music folder remove the apostrophe from the front of the line 
' CurrentDirectory = "C:\Documents and Settings\Tom\My Documents\My Music"


```
' Name: ListMusic.vbs
' Author: Josiah Kane - Ent/Ehsanit
' Version: 1.00
' Date: 22/04/2011

Set objFSO = CreateObject("Scripting.FileSystemObject")
dim CurrentDirectory
dim FileList : FileList = ""

' List extensions to use
dim extensions(10) 
extensions(0) = ".mp3"
extensions(1) = ".mp4"
extensions(2) = ".m4a"
extensions(3) = ".wav"
extensions(4) = ".aacc"
extensions(5) = ".mod"
extensions(6) = ".ogg"
extensions(7) = ".s3m"
extensions(8) = ".it"
extensions(9) = ".xm"
extensions(10) = ".aiff"


ScriptPath = Wscript.ScriptFullName
Set objScriptPath = objFSO.GetFile(ScriptPath)
CurrentDirectory = objFSO.GetParentFolderName(objscriptPath)
' CurrentDirectory = "C:\Documents and Settings\Tom\My Documents\My Music"

CheckFolder(CurrentDirectory)
  Set objFile = objFSO.OpenTextFile(CurrentDirectory & "\MusicList.txt", 2, True)
  objFile.writeline(FileList)
  objFile.close
Wscript.quit

Sub CheckFolder(FolderName)
Set Folder = objFSO.GetFolder(FolderName)
for Each File in Folder.files
    FileName = File.name
    for ext = 0 to 10
        if Lcase(right(FileName, len(extensions(ext)))) = extensions(ext) then
            FileList = FileList & FolderName & "\" & FileName & VbCrLf
        end if 
    next
Next

for Each SubFolder in Folder.SubFolders
CheckFolder(SubFolder)
Next
End Sub
```


```

```


----------



## Squashman (Apr 4, 2003)

The one line cmd should work from any directory. There isn't anything special about it. It is a basic DOS command that has been around for years.


----------

