Page 1 of 1

Listing files with exclusion

Posted: 06 Jul 2009 04:39
by gusarta
Hi,

I want to list files and folders (like using dir /s) on a certain folder but I also want to exclude some file extention, for instance *.mp3.
Is there anything from Dos scripting function that can make me list certain folder with some files excluded?

Thank you for your attention and help.

Posted: 06 Jul 2009 06:28
by ghostmachine4
you can use vbscript

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
Set objFolder = objFS.GetFolder(strFolder)
Go( objFolder)

Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders
         Go eFolder
    Next
   For Each strFiles In objDIR.Files
      strFileName = strFiles.Name
      strFilePath = strFiles.Path
      extension = Right(strFileName,4)
      If Not extension = ".mp3" And  Not extension = ".mp4" Then
         WScript.Echo strFilePath
      End If
   Next   
  End If 
End Sub


save as myscript.vbs and on command line

Code: Select all

c:\test> cscript /nologo myscript.vbs

Posted: 06 Jul 2009 07:00
by RElliott63
Since this isn't a VB Script forum.... try this: Save this as DirX.bat

Code: Select all

@Echo Off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

Set "Ignore=%1"

If /I [%Ignore%] equ [] Goto Syntax

Dir /b > %Temp%\Dir.Lst
For /F %%F in (%Temp%\Dir.Lst) Do (

    Set "Ext=%%~xF"
    If /I [!Ext!] neq [%Ignore%] (
       Echo %%~ftzaF
    )
)

Erase %Temp%\Dir.Lst >nul

Goto Exit

:Syntax
Cls
Echo;
Echo DirX .EXT      ; where .EXT = Extension to ignore
Echo;

:Exit

Posted: 06 Jul 2009 17:48
by ghostmachine4
RElliott63 wrote:Since this isn't a VB Script forum....

i beg to differ. this is not a vbscript forum, yes that is true, but putting cscript /nologo myscript.vbs into a text file, saving it as .bat makes it a BATCH file. A batch file just contains a series of commands put together to perform tasks, no matter those commands are downloaded command lines or interpreters like python.exe/perl.exe or cscript

that aside, that batch code you posted, can you do it without having to create a temp file?

Posted: 06 Jul 2009 21:11
by RElliott63
Yes...

Code: Select all

@Echo Off 

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

Set "Ignore=%1"

If /I [%Ignore%] equ [] Goto Syntax

For /F %%F in ('Dir /b') Do (
    Set "Ext=%%~xF"
    If /I [!Ext!] neq [%Ignore%] (
       Echo %%~ftzaF
    )
)

Goto Exit

:Syntax
Cls
Echo;
Echo DirX .EXT      ; where .EXT = Extension to ignore
Echo;

:Exit


What's your point?

Posted: 07 Jul 2009 02:48
by ghostmachine4
RElliott63 wrote:Yes...
What's your point?

1) don't do the unnecessary
2) makes your code neater, less code
3) less i/o processing

Posted: 08 Jul 2009 09:33
by avery_larry
ghostmachine4 wrote:
RElliott63 wrote:Since this isn't a VB Script forum....

i beg to differ. this is not a vbscript forum, yes that is true, but putting cscript /nologo myscript.vbs into a text file, saving it as .bat makes it a BATCH file. A batch file just contains a series of commands put together to perform tasks, no matter those commands are downloaded command lines or interpreters like python.exe/perl.exe or cscript
But most people want pure dos batch code when they come to a dos batch forum -- not 3rd party software requirements. One file that can go to any typical windows setup with no extra requirements. In addition, some people actually want to learn dos and come here to do so -- if they wanted a visual basic script that would be a completely different google search to find a forum that fits their need.
that aside, that batch code you posted, can you do it without having to create a temp file?
Can you do your code with 1 single file and no temp files?

Posted: 08 Jul 2009 09:40
by avery_larry
ghostmachine4 wrote:
RElliott63 wrote:Yes...
What's your point?

1) don't do the unnecessary
That's your opinion of "unnecessary".
2) makes your code neater, less code
It's also signifcantly more difficult for novices to understand. "Neater" is an opinion. Less code is not, by itself, the ultimate measuring stick of "better". Otherwise why would we ever declare variables?
3) less i/o processing
Quite true.

To be fair, I dislike the "cost" of writing to temp files, and I try to avoid that when I can.

RElliott -- if you do dump the dir output to a tmp file, it might be faster to use findstr on the tmp file (not sure of the exact syntax) instead of looping through the for loop. Actually, that might be faster on the dir output directly . . . Interesting.

Posted: 08 Jul 2009 20:10
by ghostmachine4
avery_larry wrote:
ghostmachine4 wrote:
RElliott63 wrote:Since this isn't a VB Script forum....
But most people want pure dos batch code when they come to a dos batch forum -- not 3rd party software requirements. One file that can go to any typical windows setup with no extra requirements.

pure dos batch? ok , for that i assume you are saying one cannot use ping.exe, ftp.exe and those from window\system32. is that correct? If its not correct, then what makes you decide that one cannot use vbscript? This forum is not called Pure DOS batch forum, and there is no definition of what's that either. Like i said in my previous post, a DOS batch file consists of a series of commands bundled together to perform a task. that includes using external command line tools. If you are going to emphasize on "Pure DOS batch", then make a rule about what can or cannot be posted here, and i will abide by that rule. Otherwise, I am free to post what ever solution seem best.

In addition, some people actually want to learn dos and come here to do so -- if they wanted a visual basic script that would be a completely different google search to find a forum that fits their need.

that is also not for you to decide right? Also i think you should define the meaning of DOS here. Pure cmd.exe ?? or?


Can you do your code with 1 single file and no temp files?

why are you asking me?

Posted: 10 Jul 2009 02:13
by gusarta
ghostmachine4, RElliott63, avery_larry : Thank you guys for your reply and suggestion, maybe I'm not giving enough information on what I really want to do, it is my mistake.
here goes, what I actually needs is when I run dir /s, I got the summary (xx files, xx folders, xx Bytes, xx Bytes Free). Now I want to compare those summary with the other folder which has almost similar file (as I do this for backup purpose) but the source folder has additional files such as .mp3, .tmp, etc. which I do not backup. That is why I want to know is there any way that I can get the summary of a Dir /S command with some files exculded (like .mp3, .tmp) so that the source folder and the target folder has a match summary in terms of xx files, xx folders and xx Bytes.

Once again, I'm trully sorry for my unclear question and my beginner skills.

Posted: 10 Jul 2009 03:24
by jeb
Hi gusarta,

I tried it with the Xcopy command, because it has the option to exclude files and the option to do nothing

Code: Select all

@echo off

rem *** Folder (with subfolder) to count
set folder="C:\temp"

rem * Build the file exclude list
echo .mp3 > exc.tmp
echo .tmp >> exc.tmp

set /a totalSize=0
set /a totalFiles=0

rem *** /s /e copy all with (empty) subfolders
rem *** /l show the files, but dont copy anything
rem *** /EXCLUDE: exclude all files found in the file exc.tmp
for /F "tokens=*" %%F in ('xcopy %folder% %temp%\dummyFolder\ /s /e /l  /EXCLUDE:exc.tmp') DO (
   rem **Debug**  echo #%%~zF# ^"%%F^"
   rem *** Detect the last line of XCOPY and dont count it
   if "%%~zF" NEQ "" (
      set /a totalSize += %%~zF
      set /a totalFiles += 1
   )
)
echo totalFiles = %totalFiles% totalSize = %totalSize% Bytes


hope it helps
jeb

Posted: 10 Jul 2009 11:47
by avery_larry
gusarta wrote:ghostmachine4, RElliott63, avery_larry : Thank you guys for your reply and suggestion, maybe I'm not giving enough information on what I really want to do, it is my mistake.
here goes, what I actually needs is when I run dir /s, I got the summary (xx files, xx folders, xx Bytes, xx Bytes Free). Now I want to compare those summary with the other folder which has almost similar file (as I do this for backup purpose) but the source folder has additional files such as .mp3, .tmp, etc. which I do not backup. That is why I want to know is there any way that I can get the summary of a Dir /S command with some files exculded (like .mp3, .tmp) so that the source folder and the target folder has a match summary in terms of xx files, xx folders and xx Bytes.

Once again, I'm trully sorry for my unclear question and my beginner skills.
If you are looking for a backup solution, and are trying to do custom code that essentially compares a source to a backup location, while excluding certain files from that comparison, then you should look at ROBOCOPY. It's in the Windows 2003 Server Resource Kit (so not available on "standard" windows installations, but a free download and it works on pretty much any windows). It is specifically designed to do pretty much exactly what you're asking, if I understand correctly.

http://www.microsoft.com/downloads/deta ... 8c4790cffd

If you try robocopy and like it, I can help with the syntax to get you going.

Posted: 12 Jul 2009 21:57
by gusarta
@jeb : Thank you for your script and suggestion, but I still trying to figure out to run your script as all I get is totalFiles = 0, totalSize = 0 all the time no matter which folder I put in.

@avery_larry : I already have the backup tools, I just need to check to make sure all the necessary files are being backed up already. I like robocopy tools a lot, I've been using that tools quite often as a matter of fact.

Posted: 14 Jul 2009 13:27
by avery_larry
gusarta wrote:@jeb : Thank you for your script and suggestion, but I still trying to figure out to run your script as all I get is totalFiles = 0, totalSize = 0 all the time no matter which folder I put in.
Hmmm . . . jeb's code works on my machine.

3 notes:

1) The totalsize count will only work up to around 2Gb. After that, the set /a command will choke. If you need to could larger than 2Gb you'll probably have to do some math to count in megabytes instead of bytes.

2) It would probably be a good idea to add a line in the code to delete the temporary exclude file:

Code: Select all

. . .
del exc.tmp
echo totalFiles = %totalFiles% totalSize = %totalSize% Bytes


3) The script expects %temp% to already be defined, and it cannot contain quotes or spaces. Directly from a command prompt type echo %temp% and verify those parameters. Otherwise you could modify that line of the code something like this:

Code: Select all

. . .
for /F "tokens=*" %%F in ('xcopy %folder% "c:\dummyFolder" /s /e /l  /EXCLUDE:exc.tmp') DO (
. . .

If you still have problems, get rid of the rem **Debug** portion and allow the echo command so you can see more of what's going on:

Code: Select all

. . .
for /F "tokens=*" %%F in ('xcopy %folder% "%temp%\dummyFolder" /s /e /l  /EXCLUDE:exc.tmp') DO (
   echo #%%~zF# ^"%%F^"
   rem *** Detect the last line of XCOPY and dont count it
. . .