Listing files with exclusion
Moderator: DosItHelp
Listing files with exclusion
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.
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
you can use vbscript
save as myscript.vbs and on command line
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
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
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?
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
Yes...
What's your point?
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?
-
- Posts: 319
- Joined: 12 May 2006 01:13
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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.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
Can you do your code with 1 single file and no temp files?that aside, that batch code you posted, can you do it without having to create a temp file?
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
That's your opinion of "unnecessary".ghostmachine4 wrote:RElliott63 wrote:Yes...
What's your point?
1) don't do the unnecessary
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?2) makes your code neater, less code
Quite true.3) less i/o processing
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.
-
- Posts: 319
- Joined: 12 May 2006 01:13
avery_larry wrote:ghostmachine4 wrote: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.RElliott63 wrote:Since this isn't a VB Script forum....
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?
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.
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.
Hi gusarta,
I tried it with the Xcopy command, because it has the option to exclude files and the option to do nothing
hope it helps
jeb
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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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.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.
http://www.microsoft.com/downloads/deta ... 8c4790cffd
If you try robocopy and like it, I can help with the syntax to get you going.
@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.
@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.
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Hmmm . . . jeb's code works on my machine.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.
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
. . .