Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Counting Files

HTVi
TV Discussion Newsgroups

Counting Files
"Max Meier" <nebu666[ at ]hotmail.com> 5/22/2007 9:54:21 AM
Hello Groupies,

i would like to find files an delete them, if they exists. the syntax for
the files is FILE*.txt.
if i try to use the if exists syntax i get the error message, that the file
does not exists. i think its because of the *

maybee i can solve the problem, when i count all files FILE*.txt and if its
more then 0, i delete FILE*.txt

Can someone tell me how i can count the files?? or is there a better way to
do this?

reg. Max

Re: Counting Files
Jeffery Hicks <jhicks[ at ]sapien.com> 5/22/2007 12:13:46 PM
Max Meier wrote:
[Quoted Text]
> Hello Groupies,
>
> i would like to find files an delete them, if they exists. the syntax
> for the files is FILE*.txt.
> if i try to use the if exists syntax i get the error message, that the
> file does not exists. i think its because of the *
>
> maybee i can solve the problem, when i count all files FILE*.txt and if
> its more then 0, i delete FILE*.txt
>
> Can someone tell me how i can count the files?? or is there a better way
> to do this?
>
> reg. Max

If you want to use a batch file, change to the directory and run a
command like this:

for /f %x in ('dir file*.txt /b') do [ at ]del %x

I recommend you run this first so you can see what files would be deleted:

for /f %x in ('dir file*.txt /b') do [ at ]echo %x

The other way you could do this would be to use the FileSystemObject in
VBScript.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Re: Counting Files
z1z1z1 <bogus[ at ]email.address> 5/22/2007 2:08:18 PM
"Max Meier" <nebu666[ at ]hotmail.com> wrote in
news:14863496-3AA1-4C5A-8D52-0669F226AD97[ at ]microsoft.com:

[Quoted Text]
> Hello Groupies,
>
> i would like to find files an delete them, if they exists. the syntax
> for the files is FILE*.txt.
> if i try to use the if exists syntax i get the error message, that the
> file does not exists. i think its because of the *
>
> maybee i can solve the problem, when i count all files FILE*.txt and
> if its more then 0, i delete FILE*.txt
>
> Can someone tell me how i can count the files?? or is there a better
> way to do this?
>
> reg. Max
>

The syntax is
IF EXIST FILE*.txt command
not
IF EXISTS FILE*.txt command

where in your case, "command" might be "DEL FILE*.txt"

Or, why not just do the following line?:

del FILE*.txt

If you don't want to see error messages when there are no files
FILE*.txt, change the line to:

del FILE*.txt 2>nul

the 2>nul redirects stderr to nul, so you don't see error messages
(you can also add >nul or 1>nul to redirect stdout, as well)
Re: Counting Files
Jeffery Hicks <jhicks[ at ]sapien.com> 5/22/2007 2:31:27 PM
z1z1z1 wrote:
[Quoted Text]
> "Max Meier" <nebu666[ at ]hotmail.com> wrote in
> news:14863496-3AA1-4C5A-8D52-0669F226AD97[ at ]microsoft.com:
>
>> Hello Groupies,
>>
>> i would like to find files an delete them, if they exists. the syntax
>> for the files is FILE*.txt.
>> if i try to use the if exists syntax i get the error message, that the
>> file does not exists. i think its because of the *
>>
>> maybee i can solve the problem, when i count all files FILE*.txt and
>> if its more then 0, i delete FILE*.txt
>>
>> Can someone tell me how i can count the files?? or is there a better
>> way to do this?
>>
>> reg. Max
>>
>
> The syntax is
> IF EXIST FILE*.txt command
> not
> IF EXISTS FILE*.txt command
>
> where in your case, "command" might be "DEL FILE*.txt"
>
> Or, why not just do the following line?:
>
> del FILE*.txt
>
> If you don't want to see error messages when there are no files
> FILE*.txt, change the line to:
>
> del FILE*.txt 2>nul
>
> the 2>nul redirects stderr to nul, so you don't see error messages
> (you can also add >nul or 1>nul to redirect stdout, as well)

One advantage to the FOR technique is that you can also recurse through
subdirectories:

for /f "tokens=*" %x in ('dir file*.txt /b /s') do [ at ]del "%x"

I added the tokens parameter and put the variable in quotes to handle
file and directory names with spaces. I wrote a TipSheet column on
using this technique to clean up a server or desktop:

http://mcpmag.com/columns/article.asp?EditorialsID=1731


--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Re: Counting Files
"Max Meier" <nebu666[ at ]hotmail.com> 5/22/2007 2:49:09 PM
thx for your help

maybee i didnt made my self clear - i want to put this into a VB Script. The
code im using is:

set objfs = CreateObject("Scripting.FileSystemObject")
strFileName = SPEICHERORTBackup & "KW " & KW1 & "*" & BKFName & "*Tag " &
WochentagZahl & "*(differential)*.bkf"
objfs.DeleteFile strFileName

Now the script stops if the files isnt there ... this error i want to fix

reg
Max

"Jeffery Hicks" <jhicks[ at ]sapien.com> schrieb im Newsbeitrag
news:uyr$mqGnHHA.3736[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text]
> Max Meier wrote:
>> Hello Groupies,
>>
>> i would like to find files an delete them, if they exists. the syntax for
>> the files is FILE*.txt.
>> if i try to use the if exists syntax i get the error message, that the
>> file does not exists. i think its because of the *
>>
>> maybee i can solve the problem, when i count all files FILE*.txt and if
>> its more then 0, i delete FILE*.txt
>>
>> Can someone tell me how i can count the files?? or is there a better way
>> to do this?
>>
>> reg. Max
>
> If you want to use a batch file, change to the directory and run a command
> like this:
>
> for /f %x in ('dir file*.txt /b') do [ at ]del %x
>
> I recommend you run this first so you can see what files would be deleted:
>
> for /f %x in ('dir file*.txt /b') do [ at ]echo %x
>
> The other way you could do this would be to use the FileSystemObject in
> VBScript.
>
> --
> Jeffery Hicks
> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
> VBScript & Windows PowerShell Training -
> www.ScriptingTraining.com/classes.asp
> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>
> blog: http://blog.SAPIEN.com
> blog: http://jdhitsolutions.blogspot.com
>

Re: Counting Files
urkec 5/22/2007 3:21:00 PM
"Max Meier" wrote:

[Quoted Text]
> set objfs = CreateObject("Scripting.FileSystemObject")
> strFileName = SPEICHERORTBackup & "KW " & KW1 & "*" & BKFName & "*Tag " &
> WochentagZahl & "*(differential)*.bkf"
> objfs.DeleteFile strFileName
>
> Now the script stops if the files isnt there ... this error i want to fix


You can check if the file exists before trying to delete it:

if objfs.FileExists (strFileName) then
objfs.DeleteFile strFileName
end if

--
urkec
Re: Counting Files
urkec 5/22/2007 3:30:04 PM
"Jeffery Hicks" wrote:

[Quoted Text]
>
> One advantage to the FOR technique is that you can also recurse through
> subdirectories:

I think DEL /S FILE*.txt would also delete files from subfolders.

--
urkec


Re: Counting Files
Jeffery Hicks <jhicks[ at ]sapien.com> 5/22/2007 3:37:01 PM
Max Meier wrote:
[Quoted Text]
> thx for your help
>
> maybee i didnt made my self clear - i want to put this into a VB Script.
> The code im using is:
>
> set objfs = CreateObject("Scripting.FileSystemObject")
> strFileName = SPEICHERORTBackup & "KW " & KW1 & "*" & BKFName & "*Tag "
> & WochentagZahl & "*(differential)*.bkf"
> objfs.DeleteFile strFileName
>
> Now the script stops if the files isnt there ... this error i want to fix
>
> reg
> Max
>
> "Jeffery Hicks" <jhicks[ at ]sapien.com> schrieb im Newsbeitrag
> news:uyr$mqGnHHA.3736[ at ]TK2MSFTNGP03.phx.gbl...
>> Max Meier wrote:
>>> Hello Groupies,
>>>
>>> i would like to find files an delete them, if they exists. the syntax
>>> for the files is FILE*.txt.
>>> if i try to use the if exists syntax i get the error message, that
>>> the file does not exists. i think its because of the *
>>>
>>> maybee i can solve the problem, when i count all files FILE*.txt and
>>> if its more then 0, i delete FILE*.txt
>>>
>>> Can someone tell me how i can count the files?? or is there a better
>>> way to do this?
>>>
>>> reg. Max
>>
>> If you want to use a batch file, change to the directory and run a
>> command like this:
>>
>> for /f %x in ('dir file*.txt /b') do [ at ]del %x
>>
>> I recommend you run this first so you can see what files would be
>> deleted:
>>
>> for /f %x in ('dir file*.txt /b') do [ at ]echo %x
>>
>> The other way you could do this would be to use the FileSystemObject
>> in VBScript.
>>
>> --
>> Jeffery Hicks
>> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
>> VBScript & Windows PowerShell Training -
>> www.ScriptingTraining.com/classes.asp
>> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>>
>> blog: http://blog.SAPIEN.com
>> blog: http://jdhitsolutions.blogspot.com
>>
>
What about something like this:

strFolder="S:\"
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFolder=objFSO.GetFolder(strFolder)

Set colFiles=objFolder.Files
For Each file In colFiles
If Left(UCase(file.name),4)="FILE" Then WScript.Echo file.name
Next

Modify the code to delete the file instead of displaying the name.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Re: Counting Files
Jeffery Hicks <jhicks[ at ]sapien.com> 5/22/2007 4:00:36 PM
urkec wrote:
[Quoted Text]
> "Jeffery Hicks" wrote:
>
>> One advantage to the FOR technique is that you can also recurse through
>> subdirectories:
>
> I think DEL /S FILE*.txt would also delete files from subfolders.
>

Thats much too easy! Where's the fun in that?!? :-)


--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Re: Counting Files
"Max Meier" <nebu666[ at ]hotmail.com> 5/23/2007 8:55:43 AM
Thx, that was helpfull,

one more question: how can i append this echo in a textfile that i know
which file was deleted?? in DOS i would type echo File.name >>
c:\whatever.txt. How can i do this in VB?

thx
Max

[Quoted Text]
> What about something like this:
>
> strFolder="S:\"
> Set objFSO=CreateObject("Scripting.FileSystemObject")
> Set objFolder=objFSO.GetFolder(strFolder)
>
> Set colFiles=objFolder.Files
> For Each file In colFiles
> If Left(UCase(file.name),4)="FILE" Then WScript.Echo file.name
> Next
>
> Modify the code to delete the file instead of displaying the name.
>
> --
> Jeffery Hicks
> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
> VBScript & Windows PowerShell Training -
> www.ScriptingTraining.com/classes.asp
> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>
> blog: http://blog.SAPIEN.com
> blog: http://jdhitsolutions.blogspot.com

Re: Counting Files
Jeffery Hicks <jhicks[ at ]sapien.com> 5/23/2007 9:39:49 AM
Max Meier wrote:
[Quoted Text]
> Thx, that was helpfull,
>
> one more question: how can i append this echo in a textfile that i know
> which file was deleted?? in DOS i would type echo File.name >>
> c:\whatever.txt. How can i do this in VB?
>
> thx
> Max
>
>> What about something like this:
>>
>> strFolder="S:\"
>> Set objFSO=CreateObject("Scripting.FileSystemObject")
>> Set objFolder=objFSO.GetFolder(strFolder)
>>
>> Set colFiles=objFolder.Files
>> For Each file In colFiles
>> If Left(UCase(file.name),4)="FILE" Then WScript.Echo file.name
>> Next
>>
>> Modify the code to delete the file instead of displaying the name.
>>
>> --
>> Jeffery Hicks
>> SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
>> VBScript & Windows PowerShell Training -
>> www.ScriptingTraining.com/classes.asp
>> Windows PowerShell? - www.SAPIENPress.com/powershell.asp
>>
>> blog: http://blog.SAPIEN.com
>> blog: http://jdhitsolutions.blogspot.com
>

The fast way is to run the script from the command line using CScript
and redirect output to a text file:

cscript //nologo myscript.vbs >results.txt

I use //nologo so you don't get the Cscript version banner.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen