![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | Re: FORFILES Command "Dave Cox" <DaveCox@xxxxxx> wrote in message news FF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxxQuote: > 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 ? 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 Specs![]() |
| | #3 (permalink) |
| | Re: FORFILES Command "Dave Cox" <DaveCox@xxxxxx> wrote in message news FF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxxQuote: > 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. 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 Specs![]() |
| | #4 (permalink) |
| | 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 > news FF4FF32-D3C0-4EF6-9CEC-67FD650E6760@xxxxxxQuote: > > 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 Specs![]() |
| | #5 (permalink) |
| | 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. 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 ? 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 ? event... Quote: > This would be scheduled through Control-M daily. /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 >> > news FF4FF32-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 Specs![]() |
| | #6 (permalink) |
| | 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 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 Specs![]() |
| | #7 (permalink) |
| | 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 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | 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" 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") Quote: > Regards > Matthias 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 Specs![]() |
| | #10 (permalink) |
| | 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. > 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 Specs![]() |
![]() |
| 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 | |||
Make a command prompt run a command as soon as it opens? | General Discussion | |||
| Forfiles question | .NET General | |||