Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - FORFILES Command

Reply
 
Old 01-05-2009   #1 (permalink)
Dave Cox


 
 

FORFILES Command

Trying to use the following to list files older than 5 days:

FORFILES -pC:\unzipped -mTECHSHR.TMSD203D.VLT2.VOLSER* -d-5 -c"CMD /C Echo
@FILE"

Am getting:

CHAR 14
Expected Statement
Code: 800A0400
Compilation error

This is under XP SP3.

Any ideas on format changes ?

Thanks.

My System SpecsSystem Spec
Old 01-05-2009   #2 (permalink)
Al Dunbar


 
 

Re: FORFILES Command


"Dave Cox" <DaveCox@xxxxxx> wrote in message
newsFF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxx
Quote:

> Trying to use the following to list files older than 5 days:
>
> FORFILES -pC:\unzipped -mTECHSHR.TMSD203D.VLT2.VOLSER* -d-5 -c"CMD /C Echo
> @FILE"
>
> Am getting:
>
> CHAR 14
> Expected Statement
> Code: 800A0400
> Compilation error
>
> This is under XP SP3.
>
> Any ideas on format changes ?
Not so much format changes as venue changes. FORFILES is an executable
intended for batch use:

http://technet.microsoft.com/en-us/l.../cc753551.aspx

If you put that command into a .cmd or .bat file, it ewll be more likely to
do what you intend it to.

/Al


My System SpecsSystem Spec
Old 01-05-2009   #3 (permalink)
Pegasus \(MVP\)


 
 

Re: FORFILES Command


"Dave Cox" <DaveCox@xxxxxx> wrote in message
newsFF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxx
Quote:

> Trying to use the following to list files older than 5 days:
>
> FORFILES -pC:\unzipped -mTECHSHR.TMSD203D.VLT2.VOLSER* -d-5 -c"CMD /C Echo
> @FILE"
>
> Am getting:
>
> CHAR 14
> Expected Statement
> Code: 800A0400
> Compilation error
>
> This is under XP SP3.
>
> Any ideas on format changes ?
>
> Thanks.
"Forfiles.exe" needs to be run in a batch file (.bat). Batch files are
interpreted, not compiled. I suspect you're running the command in a .vbs
file.

If you want to run an executable file in a VB Script file then you must use
the "run" or "exec" method. Alternatively you could use the script below. It
is much faster than the forfiles command because it does not need to spawn a
command processor for each file.

sSource = "C:\unzipped\"
sFileSpec = "TECHSHR.TMSD203D.VLT2.VOLSER"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sSource).Files
If InStr(UCase(oFile.Name), sFileSpec) = 1 _
And DateDiff("d", oFile.DateLastModified, Now()) > 5 _
Then WScript.Echo oFile.Name
Next


My System SpecsSystem Spec
Old 01-05-2009   #4 (permalink)
Dave Cox


 
 

Re: FORFILES Command

Thanks for the help to you both. I appreciate it. Yes, I was using a VBS
file, so I will switch to BAT file.

Thanks again,
Dave

"Pegasus (MVP)" wrote:
Quote:

>
> "Dave Cox" <DaveCox@xxxxxx> wrote in message
> newsFF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxx
Quote:

> > Trying to use the following to list files older than 5 days:
> >
> > FORFILES -pC:\unzipped -mTECHSHR.TMSD203D.VLT2.VOLSER* -d-5 -c"CMD /C Echo
> > @FILE"
> >
> > Am getting:
> >
> > CHAR 14
> > Expected Statement
> > Code: 800A0400
> > Compilation error
> >
> > This is under XP SP3.
> >
> > Any ideas on format changes ?
> >
> > Thanks.
>
> "Forfiles.exe" needs to be run in a batch file (.bat). Batch files are
> interpreted, not compiled. I suspect you're running the command in a .vbs
> file.
>
> If you want to run an executable file in a VB Script file then you must use
> the "run" or "exec" method. Alternatively you could use the script below. It
> is much faster than the forfiles command because it does not need to spawn a
> command processor for each file.
>
> sSource = "C:\unzipped\"
> sFileSpec = "TECHSHR.TMSD203D.VLT2.VOLSER"
> Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> For Each oFile In oFSO.GetFolder(sSource).Files
> If InStr(UCase(oFile.Name), sFileSpec) = 1 _
> And DateDiff("d", oFile.DateLastModified, Now()) > 5 _
> Then WScript.Echo oFile.Name
> Next
>
>
>
My System SpecsSystem Spec
Old 01-06-2009   #5 (permalink)
Al Dunbar


 
 

Re: FORFILES Command


"Dave Cox" <DaveCox@xxxxxx> wrote in message
news:18898641-F2A4-421C-A461-19A221122E2C@xxxxxx
Quote:

> Ok, I used the script as given and was presented with file names in
> text-boxes.
Pegasus' script was probably intended to be run with cscript, instead of the
default wscript, i.e.:

cscript.exe //nologo your_script_file.vbs

This will cause the output generated by wscript.echo to appear in a console
window, similar to the output from the ECHO command in batch.
Quote:

> I assume that the logic in the script can be used to interrogate a similar
> directory that has 300 text files in it, so that I can delete all but the
> most recent 45 text files ?
That is a far cry from your original query, and a far cry from what Pegasus'
script does. Perhaps you should restate exactly what it is you are trying to
accomplish, and perhaps post what you have in the way of a script.
Quote:

> I would be running this on a daily basis and
> would want the most current 45 files left alone, but older than that
> should
> be deleted. Is there a way to log the deletes, but not present them to
> anyone in text-boxes ?
Output can, indeed, be logged to log files. Or to the registry. Or as an
event...
Quote:

> This would be scheduled through Control-M daily.
I'm not familiar with that tool. I usually just use scheduled tasks.

/Al
Quote:

> Thanks again,
> Dave
>
> "Dave Cox" wrote:
>
Quote:

>> Thanks for the help to you both. I appreciate it. Yes, I was using a
>> VBS
>> file, so I will switch to BAT file.
>>
>> Thanks again,
>> Dave
>>
>> "Pegasus (MVP)" wrote:
>>
Quote:

>> >
>> > "Dave Cox" <DaveCox@xxxxxx> wrote in message
>> > newsFF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxx
>> > > Trying to use the following to list files older than 5 days:
>> > >
>> > > FORFILES -pC:\unzipped -mTECHSHR.TMSD203D.VLT2.VOLSER* -d-5 -c"CMD /C
>> > > Echo
>> > > @FILE"
>> > >
>> > > Am getting:
>> > >
>> > > CHAR 14
>> > > Expected Statement
>> > > Code: 800A0400
>> > > Compilation error
>> > >
>> > > This is under XP SP3.
>> > >
>> > > Any ideas on format changes ?
>> > >
>> > > Thanks.
>> >
>> > "Forfiles.exe" needs to be run in a batch file (.bat). Batch files are
>> > interpreted, not compiled. I suspect you're running the command in a
>> > .vbs
>> > file.
>> >
>> > If you want to run an executable file in a VB Script file then you must
>> > use
>> > the "run" or "exec" method. Alternatively you could use the script
>> > below. It
>> > is much faster than the forfiles command because it does not need to
>> > spawn a
>> > command processor for each file.
>> >
>> > sSource = "C:\unzipped\"
>> > sFileSpec = "TECHSHR.TMSD203D.VLT2.VOLSER"
>> > Set oFSO = CreateObject("Scripting.FileSystemObject")
>> >
>> > For Each oFile In oFSO.GetFolder(sSource).Files
>> > If InStr(UCase(oFile.Name), sFileSpec) = 1 _
>> > And DateDiff("d", oFile.DateLastModified, Now()) > 5 _
>> > Then WScript.Echo oFile.Name
>> > Next
>> >
>> >
>> >

My System SpecsSystem Spec
Old 01-06-2009   #6 (permalink)
Pegasus \(MVP\)


 
 

Re: FORFILES Command


"Dave Cox" <DaveCox@xxxxxx> wrote in message
news:18898641-F2A4-421C-A461-19A221122E2C@xxxxxx
Quote:

> Ok, I used the script as given and was presented with file names in
> text-boxes.
>
> I assume that the logic in the script can be used to interrogate a similar
> directory that has 300 text files in it, so that I can delete all but the
> most recent 45 text files ? I would be running this on a daily basis and
> would want the most current 45 files left alone, but older than that
> should
> be deleted. Is there a way to log the deletes, but not present them to
> anyone in text-boxes ? This would be scheduled through Control-M daily.
>
> Thanks again,
> Dave
Your new requirement is not so much a scope creep but a scope leap . . .

You could use the hybrid batch/script solution below. It uses the inbuild
sort switch of the "dir" command, followed by some VB Script code to delete
all files beyond the count of 45. Here is how to use it:
1. Copy the code to c:\Dave.bat. Do NOT retype it!
2. Set your own parameters in Lines 2 and 3.
3. Unwrap wrapped lines, then remove the line numbers.
4. Open a Command Prompt. then run these commands:
c:\Dave.bat > c:\Dave.txt{Enter}
notepad c:\Dave.txt{Enter}
5. Examine what you see on the screen.
6. If you're happy with the result, delete Line 19 and remove the
word "echo" at the start of Line 20. This will activate the program.

01. @echo off
02. set count=45
03. set Source=d:\My Files
04. set List=c:\Files.txt
05. dir /b /o-d "%Source%" > "%List%"
06.
07. echo > c:\TempVBS.vbs Set oFSO =
CreateObject("Scripting.FileSystemObject")
08. echo >> c:\TempVBS.vbs Set oListFile = oFSO.OpenTextFile("%List%")
09. echo\>> c:\TempVBS.vbs
10. echo >> c:\TempVBS.vbs For i = 1 To %count%
11. echo >> c:\TempVBS.vbs oListFile.ReadLine
12. echo >> c:\TempVBS.vbs If oListFile.AtEndOfStream Then
13. echo >> c:\TempVBS.vbs oListFile.Close
14. echo >> c:\TempVBS.vbs WScript.Quit
15. echo >> c:\TempVBS.vbs End If
16. echo >> c:\TempVBS.vbs Next
17. echo\>> c:\TempVBS.vbs
18. echo >> c:\TempVBS.vbs While Not oListFile.AtEndOfStream
19. echo >> c:\TempVBS.vbs WScript.Echo "Deleting %Source%" ^&
oListFile.ReadLine
20. rem echo >> c:\TempVBS.vbs oFSO.DeleteFile("%Source%" ^&
oListFile.ReadLine)
21. echo >> c:\TempVBS.vbs Wend
22. echo >> c:\TempVBS.vbs oListFile.Close
23. cscript //nologo c:\TempVBS.vbs
24. del c:\TempVBS.vbs & del "%List%"


My System SpecsSystem Spec
Old 01-06-2009   #7 (permalink)
Pegasus \(MVP\)


 
 

Re: FORFILES Command


"Dave Cox" <DaveCox@xxxxxx> wrote in message
news:18898641-F2A4-421C-A461-19A221122E2C@xxxxxx
Quote:

> Ok, I used the script as given and was presented with file names in
> text-boxes.
>
> I assume that the logic in the script can be used to interrogate a similar
> directory that has 300 text files in it, so that I can delete all but the
> most recent 45 text files ? I would be running this on a daily basis and
> would want the most current 45 files left alone, but older than that
> should
> be deleted. Is there a way to log the deletes, but not present them to
> anyone in text-boxes ? This would be scheduled through Control-M daily.
>
> Thanks again,
> Dave
There was a minor oversight in my initial code - use this version instead.

You could use the hybrid batch/script solution below. It uses the inbuilt
sort switch of the "dir" command, followed by some VB Script code to delete
all files beyond the count of 45. Here is how to use it:
1. Copy the code to c:\Dave.bat. Do NOT retype it!
2. Set your own parameters in Lines 2 and 3.
3. Unwrap wrapped lines, then remove the line numbers.
4. Open a Command Prompt. then run these commands:
c:\Dave.bat > c:\Dave.txt{Enter}
notepad c:\Dave.txt{Enter}
5. Examine what you see on the screen.
6. If you're happy with the result, delete Line 19 and remove the
word "echo" at the start of Line 20. This will activate the program.

01. @echo off
02. set count=45
03. set Source=d:\My Files\
04. set List=c:\Files.txt
05. dir /b /o-d /a-d "%Source%" > "%List%"
06.
07. echo > c:\TempVBS.vbs Set oFSO =
CreateObject("Scripting.FileSystemObject")
08. echo >> c:\TempVBS.vbs Set oListFile = oFSO.OpenTextFile("%List%")
09. echo\>> c:\TempVBS.vbs
10. echo >> c:\TempVBS.vbs For i = 1 To %count%
11. echo >> c:\TempVBS.vbs oListFile.ReadLine
12. echo >> c:\TempVBS.vbs If oListFile.AtEndOfStream Then
13. echo >> c:\TempVBS.vbs oListFile.Close
14. echo >> c:\TempVBS.vbs WScript.Quit
15. echo >> c:\TempVBS.vbs End If
16. echo >> c:\TempVBS.vbs Next
17. echo\>> c:\TempVBS.vbs
18. echo >> c:\TempVBS.vbs While Not oListFile.AtEndOfStream
19. echo >> c:\TempVBS.vbs WScript.Echo "Deleting %Source%" ^&
oListFile.ReadLine
20. rem echo >> c:\TempVBS.vbs oFSO.DeleteFile("%Source%" ^&
oListFile.ReadLine)
21. echo >> c:\TempVBS.vbs Wend
22. echo >> c:\TempVBS.vbs oListFile.Close
23. cscript //nologo c:\TempVBS.vbs
24. del c:\TempVBS.vbs & del "%List%"


My System SpecsSystem Spec
Old 01-06-2009   #8 (permalink)
Matthias Tacke


 
 

Re: FORFILES Command

Pegasus (MVP) wrote:
Quote:

> 01. @echo off
> 02. set count=45
> 03. set Source=d:\My Files\
> 04. set List=c:\Files.txt
> 05. dir /b /o-d /a-d "%Source%" > "%List%"
> 06.

Hi Pegasus,

what about a one liner to be executed from a cmd window in the folder
containing the files:

for /f "skip=45 Delims=" %A in ('dir /B/O-D ".\*"') do @echo Del "%~fA"

To really delete the files remove the echo.
In a batch double the percent signs.

To wrap a vbs in batch code this is IMHO more efficient:

@echo off
set count=45
set Source=d:\My Files\
set List=c:\Files.txt
dir /b /o-d /a-d "%Source%" > "%List%"
set "_=echo\>> c:\TempVBS.vbs"
%_% Set oFSO = CreateObject("Scripting.FileSystemObject")
%_% Set oListFile = oFSO.OpenTextFile("%List%")
%_%
%_% For i = 1 To %count%
%_% oListFile.ReadLine
%_% If oListFile.AtEndOfStream Then
%_% oListFile.Close
%_% WScript.Quit
%_% End If
%_% Next
%_%
%_% While Not oListFile.AtEndOfStream
%_% WScript.Echo "Deleting %Source%" ^& oListFile.ReadLine
%_% ' oFSO.DeleteFile("%Source%" ^& oListFile.ReadLine)
%_% Wend
%_% oListFile.Close
cscript //nologo c:\TempVBS.vbs
del c:\TempVBS.vbs & del "%List%"

--
Regards
Matthias
My System SpecsSystem Spec
Old 01-06-2009   #9 (permalink)
Pegasus \(MVP\)


 
 

Re: FORFILES Command


"Matthias Tacke" <Matthias@xxxxxx> wrote in message
news:6sgtnnF5i9q3U1@xxxxxx
Quote:

> Hi Pegasus,
>
> what about a one liner to be executed from a cmd window in the folder
> containing the files:
>
> for /f "skip=45 Delims=" %A in ('dir /B/O-D ".\*"') do @echo Del "%~fA"
Much nicer. I initially thought that the code would trip over "poison"
characters in the file names but this does not appear to be the case.

Quote:

> To wrap a vbs in batch code this is IMHO more efficient:
>
> @echo off
> set count=45
> set Source=d:\My Files\
> set List=c:\Files.txt
> dir /b /o-d /a-d "%Source%" > "%List%"
> set "_=echo\>> c:\TempVBS.vbs"
> %_% Set oFSO = CreateObject("Scripting.FileSystemObject")
<snip>
Quote:

> Regards
> Matthias
I suppose it depends on what exactly you mean by "efficient". Your code,
after the interpreter has resolved %_%, will do exactly the same as my
original. Your version requires less typing, which is no issue when using
copy & paste. Your code needs an extra step to resolve %_%; my code needs to
read more data from each line. I suspect that the execution time for a large
batch file would be much the same. One way or another I'm not fussed - I
treat it as a matter of preference.

Thanks for your comments.


My System SpecsSystem Spec
Old 01-06-2009   #10 (permalink)
Matthias Tacke


 
 

Re: FORFILES Command

Pegasus (MVP) wrote:
Quote:

> I suppose it depends on what exactly you mean by "efficient". Your code,
> after the interpreter has resolved %_%, will do exactly the same as my
> original. Your version requires less typing, which is no issue when using
> copy & paste. Your code needs an extra step to resolve %_%; my code needs to
> read more data from each line. I suspect that the execution time for a large
> batch file would be much the same. One way or another I'm not fussed - I
> treat it as a matter of preference.
>
I meant efficient in that it doesn't add much to the line length of the
vbs code, so less need to number the lines and lesser line wraps.
Also is the vbs code IMHO easier to read.

I hope I didn't bother you, no offense intended at all.

--
Regards
Matthias
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Forfiles command problem VB Script
Running a command from inside a script, command line is corrupted PowerShell
What is the command line command for unzipping files? Vista General
Solved Make a command prompt run a command as soon as it opens? General Discussion
Forfiles question .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46