Files are the backbone of any information system. They hold the data you work with and make up the programs you use. As a computer
As administrators, we've all dealt with users who tried to back up their entire system to the server or start their own MP3 (Motion Pictures Experts Group Layer-3 Audio) server with their user directory. Although Windows 2000 provides disk quota management, it does not include a method to target and remove the offending files. In addition to
And while users are slowly
This chapter contains many scripting examples on copying and moving files. Copying and moving files and folders has various affects on NTFS encryption, compression, or permissions. For example, a task as simple as copying a file may cause you to lose NTFS permissions set on a secure file. The following list explains the affects of copying and moving NTFS files and folders:
Copying Files/Folders within NTFS
Moving Files/Folders between two NTFS drives will cause the object to inherit NTFS permissions of the target folder.
Moving Files/Folders within the same NTFS drive will cause the object to retain its NTFS permissions.
Copying Compressed Files/Folders within NTFS drives will cause the object to inherit the compression setting of the target folder.
Moving Compressed Files/Folders between two NTFS drives will cause the object to inherit the compression setting of the target folder.
Moving Compressed Files/Folders within the same NTFS drive will cause the object to retain its compression.
Copying or Moving Compressed Files/Folders to a non-NTFS drive will cause the object to lose its compression.
Copying or Moving Encrypted Files/Folders within NTFS drives will cause the object to retain its encryption.
Copying or Moving Encrypted Files/Folders to a non-NTFS drive will cause the object to lose its encryption.
Copying Unencrypted Files/Folders to an NTFS Encrypted Folder will cause the object to become encrypted.
Moving Unencrypted Files/Folders to an NTFS Encrypted Folder will not cause the object to become encrypted.
Files and folders are the building blocks of any system. They contain the data we
Shell scripting provides limited functionality for manipulating the file system. Although Resource Kit utilities extend the capabilities of shell scripting, it still cannot compare to the more powerful functions of KiXtart and Windows Script Host. So, why use shell scripting? Shell scripting comes built into every operating system, and you will run into situations where shell scripting is your only alternative.
The Windows 2000/XP DELETE command supports many options that the Windows 9 x command does not. To remove files based on extension in Windows 2000/XP, start the command prompt and enter the following:
DEL *. ext /F /Q /S
Here,
ext
is the file extension of the files to delete; the
/F
switch forces the deletion of read-only files; the
/Q
switch
Windows XP includes the RMDIR (Remove Directory) command that mimics the Windows 9 x DELTREE.EXE (Delete Tree) command. To delete a root folder and all its subfolders with RMDIR, start the command prompt and enter the following:
RMDIR /Q /S directory
Here,
directory
is the
FILEVER.EXE is a Resource Kit utility to display file versions from the command line. To determine a file version, start the command prompt and enter the following:
FILEVER filename
Here,
filename
is the
| Note |
Remember, only application files have versions. |
REPLACE is a command that can be used to update older files with newer file versions. To update a file with a
REPLACE /R /S /U source destination
Here, source is the path and name of the source file; destination is the directory to start the replacement; the /R switch allows for readonly file replacement; the /S switch performs the replacement in the current directory and all subdirectories; and the /U switch specifies to only replace files with a newer version.
You can tell users to back up their files to the server, but whether the users actually do back the files up is a different story. ROBOCOPY is a Resource Kit utility to copy, move, or replicate files from the command line. To replicate files, start the command prompt and enter the following:
ROBOCOPY /MIR /ETA /NP /LOG+: logfile source destination
Here, the
/MIR
mirrors a directory tree; the
/ETA
switch displays the estimated time of arrival of
The ATTRIB command allows you to display file or folder attributes. To display the attributes of a file or folder, start the command prompt and enter the following:
ATTRIB filefolder
Here, filefolder is the file or folder that contains the attributes you wish to display.
The ATTRIB command allows you to set or remove file and folder attributes. To set attributes of a file or folder, start a command prompt and enter the following:
ATTRIB +R +H +S filefolder
| Tip |
Here, filefolder is the file or folder that contains the attributes you want to set. The + R , + H , and + S set filefolder 's Read Only, Hidden, and System attributes respectively. |
To remove attributes of a file or folder, start a command prompt and enter the following:
ATTRIB -R -H -S filefolder
Collecting information from log files can be a
TYPE file1 >> file2
Here, file1 is the file whose contents you want to append to file2 .
With Windows NT/2000/XP/2003 and a drive formatted with NTFS (New Technologies File System), you can take advantage of NTFS compression to save disk space by compressing your files and folders. The COMPACT command allows you to compress/uncompress NTFS files and folders from the command line. To compress all the files and subfolders of a folder, start a command prompt and enter the following:
COMPACT /c /s rootfolder \*.*
Here, rootfolder is the folder that contains the files and folders you want to compress. The /c option sets the intended action to compress, and /s specifies that the intended action should be applied to all files and subfolders of the rootfolder .
KiXtart is a scripting language I introduced in Chapter 1 that is best used when you know the exact file or directory you want to manipulate. KiXtart provides poor directory parsing capabilities with its limited DIR command and lack of recursive support. To compensate, you can call external commands for indirect file management and KiXtart commands for direct file management.
KiXtart provides two statements to run an external 16- or 32-bit application or command: SHELL and RUN . The SHELL statement will wait for the external command to complete, but the RUN statement will not. Both the SHELL and RUN statements have the same syntax:
statement " command "
Here, statement is the RUN or SHELL statement, and command is the command to run. To delete all the files in the temp directory using the RUN statement, you would enter:
RUN " %COMSPEC% /C DEL C:\TEMP\*.* /F /Q /S"
| Note |
%COMSPEC% /C is used to run commands from the DOS environment. |
KiXtart does not contain a function to rename a file or folder. Instead, you can move the current item to a new item with the desired name, providing an item with the new name does not already exist. To rename a file or folder, proceed as
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart from http://www.kixtart.org to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and the file name of a script file that contains the following:
REN( ' oldname ', ' newname ' ) Function REN( $OldFileName, $NewFileName ) MOVE $OldFileName $NewfileName /h EndFunction
Here, oldname is the name of the file or folder to rename and newname is the name to rename the oldname to. The REN function uses the MOVE command to rename the file or folder. The /h option specifies to include system and hidden files.
The KiXtart command GetFileAttr allows you to display file or folder attributes. To display the attributes of a file or folder, proceed as follows:
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:
DisplayAttr('
path
)
Sleep 5
Function DisplayAttr($FileFolder)
$ReadOnly = 0 $Hidden = 0 $System = 0
$Dir = 0 $Archive = 0 $Encrypt = 0
$Normal = 0 $Temp = 0 $Sparse = 0
$Reparse = 0 $Compress = 0 $Offline = 0
If GetFileAttr($FileFolder) & 1 $ReadOnly = 1 EndIf
If GetFileAttr($FileFolder) & 2 $Hidden = 1 EndIf
If GetFileAttr($FileFolder) & 4 $System = 1 EndIf
If GetFileAttr($FileFolder) & 16 $Dir = 1 EndIf
If GetFileAttr($FileFolder) & 32 $Archive = 1 EndIf
If GetFileAttr($FileFolder) & 64 $Encrypt = 1 EndIf
If GetFileAttr($FileFolder) & 128 $Normal = 1 EndIf
If GetFileAttr($FileFolder) & 256 $Temp = 1 EndIf
If GetFileAttr($FileFolder) & 512 $Sparse = 1 EndIf
If GetFileAttr($FileFolder) & 1024 $Reparse = 1 EndIf
If GetFileAttr($FileFolder) & 2046 $Compress = 1 EndIf
If GetFileAttr($FileFolder) & 4096 $Offline = 1 EndIf
? "File: " + $FileFolder
? ""
? "ReadOnly: " + $ReadOnly
? "Hidden: " + $Hidden
? "System: " + $System
? "Directory: " + $Dir
? "Archive: " + $Archive
? "Encrypted: " + $Encrypt
? "Normal: " + $Normal
? "Temporary: " + $Temp
? "Sparse: " + $Sparse
? "Reparse: " + $Reparse
? "Compressed: " + $Compress
? "Offline: " + $Offline
EndFunction
Here, path is the file or folder that contains the attributes you wish to display.
| Note |
Windows 2000 adds several new file attributes with NTFS 5. For more information, see Chapter 17 of the Windows 2000 Professional Resource Kit. |
The KiXtart command SetFileAttr allows you to set file or folder attributes. To modify the attributes of a file or folder, proceed as follows:
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:
;Sets Read Only and Hidden attributes
SetAttribs('
filefolder
',1,1,0,0,0,0)
Function SetAttribs($File, $ReadOnly, $Hidden, $System, $Archive,
$Temp, $Offline)
$Rcode = SetFileAttr($File,128) ;Reset file to normal
$Attribs = 0
If $ReadOnly = 1 $Attribs = $Attribs + 1 EndIf
If $Hidden = 1 $Attribs = $Attribs + 2 EndIf
If $System = 1 $Attribs = $Attribs + 4 EndIf
If $Archive = 1 $Attribs = $Attribs + 32 EndIf
If $Temp = 1 $Attribs = $Attribs + 256 EndIf
If $Offline = 1 $Attribs = $Attribs + 4096 EndIf
$SetAttribs = SetFileAttr($File,$Attribs)
EndFunction
Here, filefolder is the file or folder that contains the attributes you want to set. To modify filefolder 's attributes, change the value of the corresponding input parameters ( $ReadOnly , $Hidden , $System , $Archive , $Normal , $Offline ) to 1 to enable, or 0 to disable.
To append the contents of one text file to another, proceed as follows:
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:
$File1 = " file1 " $File2 = " file2 " $Rcode = Open(1, $File1 ) $Rcode = Open(2, $File2 ,5) $File1 = ReadLine(1) While @Error=0 If $File1 $Rcode = WriteLine(2, $File1 + Chr(13) + Chr(10)) EndIf $File1 = ReadLine(1) Loop $Rcode = Close(1) $Rcode = Close(2)
Here, file1 is the file whose contents you want to append to file2 .
Replacing specific lines within text files is a common administrative task. To search and replace a line within a text file, proceed as follows:
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:
$File = " somefile " $DLine = ' searchline ' $RLine = ' replaceline ' $TempFile = $File + ".TMP" $LineNum = 0 $Rcode = OPEN (1, $File , 2) DEL $TempFile $Rcode = OPEN (2, $TempFile , 5) $Line = READLINE(1) WHILE @Error = 0 $LineNum = $LineNum + 1 IF $Line = $DLine $Rcode = WRITELINE(2, $RLine + Chr(13) + Chr(10)) ELSE $Rcode = WRITELINE(2, $Line + Chr(13) + Chr(10)) ENDIF $Line = READLINE(1) LOOP $Rcode = CLOSE(1) $Rcode = CLOSE(2) COPY $TempFile $File DEL $TempFile
Here, somefile is the file to parse, and replaceline is the text to replace the searchline with.
INI files, or initialization files, are text files that were originally created to store configuration information for 16-bit applications. KiXtart is the
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:
$LoadKey
= ReadProfileString("
inifile"
,
section
,
key
)
If
$LoadKey
=
oldvalue
WriteProfileString("
inifile
",
section
,
key
,
newvalue
)
EndIf
Here, inifile is the complete name and path of the INI file; section is the name of the INI section to search (without the brackets); key is the name of the key to search; oldvalue is the value to find; and newvalue is the value to replace it with.
| Note |
WriteProfileString
in this example
|
Many of the file management tasks administrators would like to script are too complex or cannot be done with shell scripting or KiXtart. Through the FileSystemObject object, Windows Script Host (WSH) provides direct access to the file system, allowing you to create complex and unique file management scripts.
The FileSystemObject object stores all the functions that allow you to manipulate the file system through a script file. To create an instance of the FileSystemObject , proceed as follows:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
This subroutine will work through the subfolders of a main directory, calling another subroutine called MainSub :
Sub GoSubFolders (
objDIR
)
If
objDIR
<> "\System Volume Information" Then
MainSub
objDIR
For Each
eFolder
in
objDIR
.SubFolders
GoSubFolders
eFolder
Next
End If
End Sub
| Note |
The System Volume Information Directory is a system directory that stores system files,
|
Before performing certain WSH actions on a file, you must first connect to it using the GetFile method. Here is a function to connect to a file:
Function GetFile(
sFILE
)
On Error Resume Next
Set
GetFile
=
FSO
.GetFile(
sFILE
)
If Err.Number <> 0 Then
Wscript.Echo "Error connecting to: " &
sFILE
& VBlf & _
"[" & Err.Number & "] " & Err.Description
Wscript.Quit Err.Number
End If
End Function
| Tip |
On Error Resume Next allows the script to continue to the next statement if an error occurs. This allows you to perform error checking and alerting. |
In this script, a connection to a file is attempted, and the
Before performing certain WSH actions on a folder, you must first connect to it using the GetFolder method. Here is a function to connect to a folder:
Function GetFolder(
sFOLDER
)
On Error Resume Next
Set
GetFolder
=
FSO
.GetFolder(
sFOLDER
)
If Err.Number <> 0 Then
Wscript.Echo "Error connecting to folder: " & sFOLDER & _
VBlf & "[" & Err.Number & "] " & Err.Description
Wscript.Quit Err.Number
End If
End Function
Most applications use temporary files ”files with random and or unique
Function GetRandomName()
Set
FSO
= CreateObject("Scripting.FileSystemObject")
GetRandomName =
FSO
.GetTempName
End Function
To generate a directory list, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
Set
objDIR
= GetFolder(sDIR)
GoSubFolders
objDIR
Sub ListFiles (
objDIR
)
For Each
efile
in
objDIR
.Files
Wscript.Echo
efile
Next
End Sub
Sub GoSubFolders (
objDIR
)
If
objDIR
<> "\System Volume Information" Then
ListFiles
objDIR
For Each
eFolder
in
objDIR
.SubFolders
Wscript.Echo
eFolder
GoSubFolders
eFolder
Next
End If
End Sub
Here, directory is the root folder containing the files and folders to list. The subprocedure ListFiles rotates through all the files within the current directory and lists their names.
| Note |
You need to append the GetFolder routine, listed earlier in this chapter, to this script in order for it to run. |
| Tip |
If you want to send the directory list to a text file, you can use the DOS append command (
>>
) when running the script from the command line (for example,
cscript scriptfile.vbs >>
|
To delete a file with WSH, you can use the DeleteFile method. Here is a subroutine to delete a file:
Sub DelFile(
sFILE
)
On Error Resume Next
FSO
.DeleteFile
sFILE
, True
If Err.Number <> 0 Then
Wscript.Echo "Error deleting file: " &
sFILE
End If
End Sub
In this script, a file deletion is attempted, and the user is prompted if any errors occur.
To delete all files within a root folder and its subfolders, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
Set
objDIR
= GetFolder(sDIR)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
DelFile
efile
Next
End Sub
Here, directory is the root folder containing the files to delete.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
It happens to all of us, but every now and then a user chooses to upload hundred meg files to a public share. To delete all files within a root folder and its subfolders depending on size, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
lSIZE
=
lowersize
uSIZE
=
uppersize
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
If
lSIZE
= Null and
uSIZE
= Null Then
If
efile
.Size = 0 Then
DelFile
efile
End If
ElseIf
lSIZE
<> Null and
uSIZE
= Null Then
If
efile
.Size <
lSIZE
Then
DelFile
efile
End If
ElseIf
lSIZE
= Null and
uSIZE
<> "" Then
If
efile
.Size >
uSIZE
Then
DelFile
efile
End If
ElseIf
lSIZE
=
uSIZE
Then
If
efile
.Size =
lSIZE
Then
DelFile
efile
End If
Else
If
efile
.Size >
lSIZE
and _
efile
.Size <
uSIZE
Then
DelFile
efile
End If
End If
Next
End Sub
Here, directory is the folder containing the files to delete, lowersize is the lower size limit, and uppersize is the upper size limit. If both limits are null, the script will delete all empty files. If just the upper limit is null, the script will delete files smaller than the lower limit. If just the lower limit is null, the script will delete files larger than the upper limit. If both limits are not null but equal, the script will delete files equal to the limit. If both limits are not null and not equal, the script will delete files within the two limits.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
A common administrative task is deleting old files from public shares. To delete all files within a root folder and its subfolders depending on last modified date, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
lDATE
= "
lowerdate
"
uDATE
= "
upperdate
"
lDATE
= CDate(
lDATE
)
uDATE
= CDate(
uDATE
)
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
If
lDATE
= Null and
uDATE
= Null Then
If
efile
.DateLastModified = Date Then
DelFile
efile
End If
ElseIf
lDATE
<> Null and
uDATE
= Null Then
If
efile
.DateLastModified <
lDATE
Then
DelFile
efile
End If
ElseIf
lDATE
= Null and
uDATE
<> Null Then
If
efile
.DateLastModified >
uDATE
Then
DelFile
efile
End If
ElseIf
lDATE
=
uDATE
Then
If
efile
.DateLastModified =
lDATE
Then
DelFile
efile
End If
Else
If
efile
.DateLastModified >
lDATE
and _
efile
.DateLastModified <
uDATE
Then
DelFile
efile
End If
End If
Next
End Sub
Here, directory is the folder containing the files to delete, lowerdate is the lower date limit, and upperdate is the upper date limit. If both limits are null, the script will delete files last modified today. If just the upper limit is null, the script will delete files smaller than the lower limit. If just the lower limit is null, the script will delete files larger than the upper limit. If both limits are not null but equal, the script will delete files equal to the limit. If both limits are not null and not equal, the script will delete files within the two limits.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
From hacker tools to new viruses, deleting files with a specific name is a common administrative task. To delete all files with a specific name within a root folder and its subfolders, proceed according to the steps on the next page.
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
sFILE
= "
filename
"
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
If LCase(
efile
.Name) = LCase(
sFILE
) Then
DelFile
efile
End If
Next
End Sub
Here, directory is the folder containing the files to delete, and filename is the name of the file to search for.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
Cleaning a system of specific file types, such as TMP (Temporary), MP3 (Motion Picture Experts Group Layer 3 Audio), AVI (Audio Video Interleave), and other file types, is a very common administrative task. To delete all files with a specific extension within a root folder and its subfolders, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
sEXT
= "
EXT
"
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
fEXT =
FSO
.GetExtensionName(
efile
.Path)
If LCase(
fEXT
) = LCase(
sEXT
) Then
DelFile
efile
End If
Next
End Sub
Here, directory is the folder containing the files to delete, and EXT is the file extension to search for. The sub procedure MainSub rotates through every file within the current directory, checks the file extension, and deletes the file if specified.
| Note |
You need to append the GoSubFolders, DelFile, and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
To delete a folder with WSH, you can use the DeleteFolder method. Here is a subroutine to delete a folder:
Sub DelFolder(
sFOLDER
)
On Error Resume Next
FSO
.DeleteFolder
sFOLDER
, True
If Err.Number <> 0 Then
Wscript.Echo "Error deleting folder: " &
sFOLDER
End If End Sub
To delete all subfolders within a directory, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub GoSubFolders (
objDIR
)
If
objDIR
<> "\System Volume Information" Then
For Each
eFolder
in
objDIR
.SubFolders
DelFolder
eFolder
Next
End If
End Sub
Here, directory is the folder containing the subfolders to delete.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
By maintaining public shares, you get to notice all the bad habits of a typical user. One of these habits includes leaving empty folders spread throughout the public share. To delete all folders depending on size within a root folder and its subfolders, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
lSIZE
=
lowersize
uSIZE
=
uppersize
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
If
objDIR
<> "\System Volume Information" Then
For Each
eFolder
in
objDIR
.SubFolders
If
lSIZE
= Null and
uSIZE
= Null Then
If
efolder
.Size = 0 Then
DelFolder
efolder
End If
ElseIf
lSIZE
<> Null and
uSIZE
= Null Then
If
efolder
.Size <
lSIZE
Then
DelFolder
efolder
End If
ElseIf
lSIZE
= Null and
uSIZE
<> Null Then
If
efolder
.Size >
uSIZE
Then
DelFolder
efolder
End If
ElseIf
lSIZE
=
uSIZE
Then
If
efolder
.Size =
lSIZE
Then
DelFolder
efolder
End If
Else
If
efolder
.Size >
lSIZE
and _
efolder
.Size <
uSIZE
Then
DelFolder
efolder
End If
End If
Next
End If
End Sub
Here, directory is the root folder containing the subfolders to delete, lowersize is the lower size limit, and uppersize is the upper size limit. If both limits are null, the script will delete all subfolders with a size of 0. If just the upper limit is null, the script will delete subfolders smaller than the lower limit. If just the lower limit is null, the script will delete subfolders larger than the upper limit. If both limits are not null but equal, the script will delete subfolders equal to the limit. If both limits are not empty and not null, the script will delete subfolders within the two limits.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
If you let them, users will leave files and folders forever on a public share. To delete all folders depending on last modified date within a root folder and its subfolders, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
lDATE
= "
lowerdate
"
uDATE
= "
upperdate
"
lDATE
= CDate(
lDATE
)
uDATE
= CDate(
uDATE
)
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
If
objDIR
<> "\System Volume Information" Then
For Each
eFolder
in
objDIR
.SubFolders
If
lDATE
= Null and
uDATE
= Null Then
If
efolder
.DateLastModified = 0 Then
DelFolder
efolder
End If
ElseIf
lDATE
<> Null and
uDATE
= Null Then
If
efolder
.DateLastModified <
lDATE
Then
DelFolder
efolder
End If
ElseIf
lDATE
= Null and
uDATE
<> Null Then
If
efolder
.DateLastModified >
uDATE
Then
DelFolder
efolder
End If
ElseIf
lDATE
=
uDATE
Then
If
efolder
.DateLastModified =
lDATE
Then
DelFolder
efolder
End If
Else
If
efolder
.DateLastModified >
lDATE
and _
efolder
.DateLastModified <
uDATE
Then
DelFolder
efolder
End If
End If
Next
End If
End Sub
Here, directory is the root folder containing the subfolders to delete, lowerdate is the lower date limit, and upperdate is the upper date limit. If both limits are null, the script will delete subfolders last modified today. If just the upper limit is null, the script will delete subfolders smaller than the lower limit. If just the lower limit is null, the script will delete subfolders larger than the upper limit. If both limits are not null but equal, the script will delete subfolders equal to the limit. If both limits are not null and not equal, the script will delete subfolders within the two limits.
| Note |
You need to append the GoSubFolders , DelFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
Any user public folder called GAMES or QUAKE is most likely not work-
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
sFOLDER
= "
foldername
"
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
If
objDIR
<> "\System Volume Information" Then
For Each
eFolder
in
objDIR
.SubFolders
If
LCase
(eFolder.Name) =
LCase
(
sFOLDER
) Then
DelFolder
efolder
End If
Next
End If
End Sub
| Note |
You need to append the GoSubFolders and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
To copy a file with WSH, you can use the CopyFile method. Here is a subroutine to copy a file:
Sub CopyFile(
sFILE
,
sDIR
)
If Right(
sDIR
,1) <> "\" Then
sDIR
=
sDIR
& "\"
On Error Resume Next
FSO
.CopyFile
sFILE
,
sDIR
, True
If Err.Number <> 0 Then
Wscript.Echo "Error copying file: " &
sFILE
End If
End Sub
Here, sFILE is the file to copy, and sDIR is the location to copy the file to.
To copy a folder with WSH, you can use the CopyFolder method. Here is a subroutine to copy a folder:
Sub CopyFolder(
sFOLDER
,
sDIR
)
If Right(
sFOLDER
,1) = "\" Then
sFOLDER
= Left(
sFOLDER
,(Len(
sFOLDER
)-1))
End If
If Right(
sDIR
,1) <> "\" Then
sDIR
=
sDIR
& "\"
On Error Resume Next
FSO
.CopyFolder
sFOLDER
,
sDIR
, True
If Err.Number <> 0 Then
Wscript.Echo "Error copying folder: " &
sFOLDER
End If
End Sub
Here, sFOLDER is the folder to copy, and sDIR is the location to copy the folder to.
To move a file with WSH, you can use the MoveFile method. Here is a subroutine to move a file:
Sub MoveFile(
sFILE
,
sDIR
)
On Error Resume Next
FSO
.MoveFile
sFILE
, sDIR
If Err.Number <> 0 Then
Wscript.Echo "Error moving file: " &
sFILE
End If
End Sub
Here, sFILE is the file to move, and sDIR is the location to move the file to.
Although certain file types, such as MP3s, do not belong in the public share, you may want to keep them for your own purposes. To move files with a specific extension to a central directory, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sEXT
= "
extension
"
sDIR
= "
startdir
"
sNEW
= "
enddir
"
Set
objDIR
= GetFolder(sDIR)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in objDIR.Files
fNAME =
efile
fEXT =
FSO
.GetExtensionName(
efile
.Path)
If
LCase
(
fEXT
) = LCase(
sEXT
) Then
sEXIST
=
sNEW
&
efile
.Name
If ((
FSO
.FileExists(
sEXIST
)) AND _
(
efile
<>
sEXIST
)) Then
DelFile
sEXIST
End If
On Error Resume Next
MoveFile
efile
, sNEW
End If
Next
End Sub
Here, extension is the name of the extension to search for, startdir is the name of the directory to start the search, and enddir is the directory to store all files.
| Note |
You need to append the GoSubFolders , DelFile , MoveFile , and GetFolder routines, listed earlier in this chapter, to this script in order for it to run. |
To move a folder with WSH, you can use the MoveFolder method. Here is a subroutine to move a folder:
Sub MoveFolder(
sFOLDER
,
sDIR
)
If Right(
sFOLDER
,1) = "\" Then
sFOLDER
= Left(
sFOLDER
,(Len(
sFOLDER
)-1))
End If
If Right(
sDIR
,1) <> "\" Then
sDIR
=
sDIR
& "\"
On Error Resume Next
FSO
.MoveFolder
sFOLDER
,
sDIR
If Err.Number <> 0 Then
Wscript.Echo "Error moving folder: " &
sFOLDER
End If
End Sub
Here, sFOLDER is the folder to move, and sDIR is the location to move the folder to.
To rename a file, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
MoveFile " filename ", " newname "
Here, filename is the name of the file to rename, and sname is the name to rename the file.
| Note |
You need to append the MoveFile routine, listed earlier in this chapter, to this script in order for it to run. |
I don't know what planet of bad habits this came from, but some users like to name files with their own personal extensions. Although this might be beneficial to them when searching for their files, it becomes an administrator's nightmare when these files are being shared. Unfortunately, the DOS RENAME command does not have the ability to act through subdirectories. To rename files with specific extensions with a new extension, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sEXT
= "
oldext
"
sNEW
= "
newext
"
sDIR
= "
directory
"
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
fEXT
=
FSO
.GetExtensionName(
efile
.Path)
If LCase(
fEXT
) = LCase(
sEXT
) Then
fNAME
=Left(
efile
.name,(Len(
efile
.Name)-Len(
fEXT
)))+sNEW
efile
.name =
fNAME
End If
Next
End Sub
Here, oldext is the name of the extension to search for, newext is the name of the extension to replace with, and directory is the name of the directory to start the search.
| Note |
You need to append the GetFolder and GoSubFolders routines, listed earlier in this chapter, to this script in order for it to run. |
To rename a file with its short DOS 8.3 name, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
sFILE = " filename " Set gFILE = GetFile sFILE ShortName = gFILE .shortname MoveFile sFile, ShortName
Here, filename is the name of the file to rename. An important thing to know is that you can't rename a file from a long file name to its short name directly because Windows sees long and short file names collectively, and you can't name a file the same name as another file in the current directory. In this example, we first append an SN to the file name and then change the file name to its short name.
| Note |
You need to append the GetFile and MoveFile routines, listed earlier in this chapter, to this script in order for it to run. |
|
Related solution: |
Found on page: |
|---|---|
|
Using SCANDSKW.EXE to Convert Long File Names to Short |
90 |
To update a program file with a newer version, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
sFILE
= "
filename
"
Set
nFILE
= GetFile(
sFILE
)
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
fVER
=
FSO
.GetFileVersion(
efile
)
nVER
=
FSO
.GetFileVersion(
sFILE
)
If
LCase
(
efile
.Name) =
LCase
(nFILE.Name) Then
If
fVER
=
nVER
Then
CopyFile
nFILE
,
efile
.ParentFolder
End If
End If
Next
End Sub
Here, directory is the folder containing the files to update, and filename is the file used to update the older file versions.
| Note |
You need to append the GetFile , GetFolder , GoSubFolders , and CopyFile routines, listed earlier in this chapter, to this script in order for it to run. Remember, only program files have versions. |
To display the attributes of a file, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
fNAME
= "
filename
"
Set
gFILE
= GetFile(
fNAME
)
gATTRIB
=
gFILE
.Attributes
If
gATTRIB
and 1 Then
ReadOnly
= 1 Else
ReadOnly
= 0
If
gATTRIB
and 2 Then
Hidden
= 1 Else
Hidden
= 0
If
gATTRIB
and 4 Then
System
= 1 Else
System
= 0
If
gATTRIB
and 5 Then
Volume
= 1 Else
Volume
= 0
If
gATTRIB
and 16 Then
Directory
= 1 Else
Directory
= 0
If
gATTRIB
and 32 Then
Archive
= 1 Else
Archive
= 0
If
gATTRIB
and 64 Then
Alias
= 1 Else
Alias
= 0
If
gATTRIB
and 128 Then
Compressed
= 1 Else
Compressed
= 0
Wscript.Echo "FILE: " &
UCase
(
fNAME
) & vblf & vblf & _
"Readonly: " & vbtab &
ReadOnly
& vblf & _
"Hidden: " & vbtab &
Hidden
& vblf & _
"System: " & vbtab &
System
& vblf & _
"Volume: " & vbtab &
Volume
& vblf & _
"Directory: " & vbtab &
Directory
& vblf & _
"Archive: " & vbtab &
Archive
& vblf & _
"Alias: " & vbtab & vbtab &
Alias
& vblf & _
"Compressed:" & vbtab &
Compressed
Here, filename is the file that contains the attributes you want to get.
| Note |
You need to append the GetFile routine, listed earlier in this chapter, to this script in order for it to run. |
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
|
Related solution: |
Found on page: |
|---|---|
|
Getting File or Folder Details |
110 |
To set the attributes of a file, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
fNAME
= "
filename
"
ReadOnly
= 0
Hidden
= 0
System
= 0
Archive
= 0
Set
gFILE
= GetFile(
fNAME
)
gFILE
.Attributes = 0
Attribs = 0
If
ReadOnly
= 1 Then Attribs = Attribs + 1
If
Hidden
= 1 Then Attribs = Attribs + 2
If
System
= 1 Then Attribs = Attribs + 4
If
Archive
= 1 Then Attribs = Attribs + 32
gFILE
.Attributes = Attribs
Here, filename is the file that contains the attributes you want to set. To modify filename 's attributes, change the value of the corresponding variable names ( ReadOnly , Hidden , System , Archive ) to 1 to enable, or 0 to disable.
| Note |
You need to append the GetFile routine, listed earlier in this chapter, to this script in order for it to run. |
To set the attributes of all files within a folder and its subfolders, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host from http://www.microsoft.com to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
sDIR
= "
directory
"
sReadOnly
= 0
sHidden
= 0
sSystem
= 0
sArchive
= 0
Set
objDIR
= GetFolder(
sDIR
)
GoSubFolders
objDIR
Sub MainSub (
objDIR
)
For Each
efile
in
objDIR
.Files
Set
gFILE
= GetFile(
efile
)
gFILE
.Attributes = 0
Attribs = 0
If
sReadOnly
= 1 Then Attribs = Attribs + 1
If
sHidden
= 1 Then Attribs = Attribs + 2
If
sSystem
= 1 Then Attribs = Attribs + 4
If
sArchive
= 1 Then Attribs = Attribs + 32
gFILE
.Attributes = Attribs
Next
End Sub
Here, directory contains the files whose attributes you want to set. To modify the attributes, change the values of the corresponding variable names ( ReadOnly , Hidden , System , Archive ) to 1 to enable, or 0 to disable.
| Note |
You need to append the GetFile routine, the GetFolder routine, and the GoSubFolders routine listed earlier in this chapter to this script in order for it to run. |
To append the contents of one text file to another, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
Set
FSO
= CreateObject("Scripting.FileSystemObject")
File1
= "
1stfile
"
File2
= "
2ndfile
"
Set
txtFile1
= FSO.OpenTextFile(File1, 1)
Set
txtFile2
= FSO.OpenTextFile(File2, 8)
Do While
txtFile1
.AtEndOfline <> True
txtFile2
.WriteLine(txtFile1.Readline & vbcr)
Loop
txtFile1.close
txtFile2.close
Here, 1stfile is the file whose contents you want to append to 2ndfile .