/ Forside/ Teknologi / Udvikling / ASP / Spørgsmål
Login
Glemt dit kodeord?
Brugernavn 

Kodeord  


Reklame
Top 10 brugere
ASP
#NavnPoint
smorch 9259
Harlekin 1866
molokyle 1040
Steffanst.. 758
gandalf 657
smilly 564
gibson 560
cumano 530
MouseKeep.. 480
10  Random 410
Omdøbe, slette og oprette
Fra : crha
Vist : 451 gange
50 point
Dato : 26-09-03 16:39

Hejsa, jeg sidder her og har brug for at kunne fælgende via ASP:

- Omdøbe filer og mapper
- Slette filer og mapper
- Oprette filer og mapper

Så hvis der er nogen der ligger inde med noget hjælp må i da meget gerne skrive det her :D

Mvh.
Casper

 
 
Kommentar
Fra : sorenw


Dato : 26-09-03 17:26

Hej Casper.

Jeg har fundet denne kodestump på nettet:

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

objFSO.MoveFile Server.MapPath("/mappe/gl_navn.asp"), _
Server.MapPath("/mappe/nyt_navn.asp")

Set objFSO = Nothing


Den kan omdøbe og/eller flytte filer.

/Søren

Kommentar
Fra : crha


Dato : 26-09-03 18:11

Mange tak :D
Så mangler jeg bare et kunne oprette og slette filer og mapper :)

Kommentar
Fra : LisBJensen


Dato : 26-09-03 20:21

Jamen så kan du vel bruge delete i stedet for move og create for at oprette.

Lis

Kommentar
Fra : crha


Dato : 26-09-03 20:35

Virker med delete (tak for det) men ikke med create...

Kommentar
Fra : LisBJensen


Dato : 26-09-03 21:32

så må du prøve med make

Lis

Kommentar
Fra : LisBJensen


Dato : 26-09-03 21:33

Og med hensyn til mapper skal du nok bruge dir i stedet for file
Lis

Kommentar
Fra : smorch


Dato : 27-09-03 07:30
Kommentar
Fra : crha


Dato : 28-09-03 19:20

Disse har jeg set, og disse har jeg brugt, og det er derfor jeg spørger om de ting jeg gør, da der i disse lektioner ikke står noget om hvordan man omdøber, sletter og opretter filer/mapper :)

Kommentar
Fra : smorch


Dato : 28-09-03 19:36
Kommentar
Fra : crha


Dato : 28-09-03 20:20

Den siger:

The page you're looking for has been moved or removed from the site.

:\

Kommentar
Fra : smorch


Dato : 28-09-03 20:46

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/scrguide/sas_scr_gyyj.asp

du skal kopiere hele linket ind i din browser, da Kandu siden åbenbart ikke kan forstå at det hele er et link.

Alterneativt kan du på microsoft.com søge på Scripting.FileSystemObject

Mvh

sMorch

Kommentar
Fra : LisBJensen


Dato : 28-09-03 21:04

Hej
Har du prøvet at kigge på netcoders? eller http://activedeveloper.dk eller en af de mange andre der har koder liggende til asp. som f.eks http://www.w3schools.com/

Lis

Kommentar
Fra : crha


Dato : 28-09-03 21:33

I know smorch, men den skriver det samme :/
Og til LisBJensen, jeg har kigget lidt på activedeveloper.dk uden held

Kommentar
Fra : LisBJensen


Dato : 28-09-03 21:45

Så prøv de andre.

Lis

Kommentar
Fra : smorch


Dato : 28-09-03 22:11

Copying a FileCopying files, either from one folder to another on a single computer or from one computer to another, is a common administrative task. For example, you might want to copy a new monitoring script to all your servers or replace an outdated DLL with a newer version. The CopyFile method provides a way to perform these tasks programmatically.

The CopyFile method has two required parameters and one optional parameter:

Source (required). Path to the file being copied. This can be either a path on the local computer or a UNC path to a remote computer.
Destination (required). Path to the location where the file is to be copied. This can also be a local path or a UNC path.
To specify that the file keep the same name in its destination location, put a trailing forward slash after the destination folder:

objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\"

To give the file a new name in its destination location, specify a full file name as the destination:

objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\NewFileName.txt"

If the destination folder does not exist, it will automatically be created.

Overwrite (optional). By default, the CopyFile method will not copy a file if a file by that same name exists in the destination location. This can be a problem; among other things, this prevents you from replacing an older version of a file with a newer version. To allow the CopyFile method to copy over existing files, set the optional Overwrite parameter to True.
The script in Listing 4.22 copies C:\FSO\ScriptLog.txt to the folder D:\Archive. This operation results in the existence of two files:

The original file, C:\FSO\ScriptLog.txt.
The copied file, D:\Archive\ScriptLog.txt.
To ensure that the procedure will be carried out even if D:\Archive\ScriptLog.txt exists, the Overwrite parameter is set to True by using the constant OverWriteExisting.

Listing 4.22 Copying a File

1
2
3

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\", OverwriteExisting


When specifying the destination folder, it is important to include the trailing backslash (for example, D:\Archive\). If the backslash is there, CopyFile will copy the file into the Archive folder. If the backslash is not there, CopyFile will try to create a new file named D:\Archive. If the folder D:\Archive already exists, a "Permission denied error" will be generated, and the copy procedure will fail.

The CopyFile method will also fail if you attempt to overwrite an existing read-only file, even if you have set the OverWrite parameter to True. To copy over a read-only file, you must first delete the file and then call the CopyFile method.

Copying a Set of Files
Wildcard characters provide a way to copy an entire set of files as long as these files are all in the same folder. You can copy a set of files using the same parameters used to copy a single file, but you must include a wildcard as part of the source parameter. For example, the script in Listing 4.23 copies all the .txt files found in C:\FSO to D:\Archive.

Listing 4.23 Copying a Set of Files

1
2
3

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" , OverwriteExisting


Using wildcards with the CopyFile method allows you to copy all the files in a folder without copying any subfolders in that folder; the CopyFolder method, by contrast, copies both files and subfolders. The following code statement copies all the files in the C:\FSO folder without copying any subfolders:

objFSO.CopyFile "C:\FSO\*.*" , "D:\Archive\"




Moving a FileInstead of copying a file, you might want to move it. For example, if a disk is running low on space, you might want to move a file to a new location. If a computer is changing roles, you might want to move certain diagnostic tools to its replacement. In either case, you do not want two or more copies of the file; you want one copy of the file, stored in a new place.

The MoveFile method enables you to move a file from one location to another. The MoveFile method works exactly like the CopyFile method: You create an instance of the FileSystemObject, call the MoveFile method, and pass two parameters:

The complete path to the file to be moved.
The complete path to the new location, making sure to include the trailing backslash.
For example, the script in Listing 4.24 moves C:\FSO\ScriptLog.log to the Archive folder on drive D.

Listing 4.24 Moving a File

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.log" , "D:\Archive\"


Moving a Set of Files
You can also use wildcard characters to move multiple files in a single operation. For example, to move all the files in the FSO folder that begin with the letters data, use the following parameter:

C:\FSO\Data*.*

Wildcard characters are especially useful for moving all the files of a particular type because file types are usually denoted by file name extensions. For example, the script in Listing 4.25 moves all the log files (with the .log file name extension) from the FSO folder on drive C to the Archive folder on drive D.

Listing 4.25 Moving a Set of Files

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\*.log" , "D:\Archive\"

Renaming a FileThe FileSystemObject does not include a direct method for renaming a file. However, in much the same way that a folder can be renamed using the MoveFolder method, files can be renamed using the MoveFile method. To rename a file, call the MoveFile method but leave the file in its current folder.

For example, the script in Listing 4.26 renames ScriptLog.txt to BackupLog.txt. Technically, the script actually moves C:\FSO\ScriptLog.txt to a new path: C:\FSO\BackupLog.txt. The net result, however, is that the file named ScriptLog.txt is now named BackupLog.txt.

Listing 4.26 Renaming a File Using the MoveFile Method

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"


Deleting a FileThe ability to delete files by using the FileSystemObject enables you to create scripts that can automatically perform tasks such as disk cleanup operations. For example, you might have a script that periodically searches for and deletes all temporary files (files with the .tmp file name extension). Alternatively, the script might delete files based on some other criteria, such as those that have not been accessed in the past six months, or those with a particular file name extension (such as .bmp or .mp3).

You can delete a file by creating an instance of the FileSystemObject and then calling the DeleteFile method, passing the path to the file as the parameter. For example, the script in Listing 4.20 deletes the file C:\FSO\ScriptLog.txt.

Listing 4.20 Deleting a File

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\FSO\ScriptLog.txt")


By default, the DeleteFile method will not delete a read-only file; if fact, a run-time error will occur if you attempt to delete such a file. To avoid errors, and to delete read-only files, add the optional Force parameter. When the Force parameter is set to True, the DeleteFile method can delete any file. For example, this line of code deletes the file ScriptLog.txt, even if that file is marked as read-only:

objFSO.DeleteFile("C:\FSO\ScriptLog.txt", True)

Deleting a Set of Files
There might be occasions when you require a script to delete a single, specified file. More likely, though, you will want to use scripts to delete multiple files. For example, at the end of the week, you might want to delete a set of log files that has been archived or delete all the temporary files that have been created but not removed.

Wildcard characters allow you to delete a set of files within a single folder. However, you cannot use the DeleteFile method to directly delete files from multiple folders. Instead, your script needs to iterate through the folders and use the DeleteFile method to individually delete the files in each folder. To delete files from multiple folders in a single operation (for example, to delete all the .TMP files stored anywhere on a computer), you should use WMI instead of the FileSystemObject.

To delete a set of files, call the DeleteFile method, supplying the path of the folder and the wildcard string required to delete files based on name or file name extension. For example, this line of code deletes all the .doc files in the C:\FSO folder:

objFSO.DeleteFile("C:\FSO\*.doc")

This line of code deletes all the files with the letters log somewhere in the file name:

objFSO.DeleteFile("C:\FSO\*log.* ")

As noted previously, the DeleteFile method does not delete any documents marked as read-only. If a script attempts to delete a read-only document, a run-time error will occur, and the DeleteFile method will stop, even if the script uses the On Error Resume Next statement. For example, suppose you are trying to delete 1,000 .txt files, and one of those files is marked as read-only. As soon as the script attempts to delete that file, an error will occur, and the DeleteFile method will stop. The script will make no attempt to delete any other files, even though none of them are read-only.

Because of that, you can use an optional second parameter, Force, that can be set to True. When the Force parameter is set to True, the DeleteFile method can delete read-only documents. When the Force parameter is set to False (the default value), the DeleteFile method cannot delete read-only documents.

The script in Listing 4.21 deletes all the .txt files in the folder C:\FSO. To ensure that all files, including read-only files, are deleted, the Force parameter is set to True using the constant DeleteReadOnly.

Listing 4.21 Deleting a Set of Files

1
2
3

Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\FSO\*.txt"), DeleteReadOnly


Tip

What if you want to delete all files except those marked as read-only? In that case, you can retrieve the complete set of files by using the Folder object Files property. You can then cycle through the collection, check to see whether each individual file is read-only, and, if it is not, delete the file.


Creating a FolderIt is unlikely that you will ever sit down, implement your file system infrastructure (that is, your folders and subfolders), and then never have to touch that infrastructure again. Instead, a file system tends to be dynamic: because of ever-changing needs, existing folders might be deleted and new folders might be created. For example, if your organization provides users with storage space on file servers, you need to create a new folder each time a new user account is created.

The FileSystemObject gives script writers the ability to programmatically create folders, a capability that can make your scripts even more powerful and more useful. For example, the script in Listing 4.6 checks to see whether a specified folder exists. If the folder exists, the script uses the GetFolder method to bind to the folder. If the folder does not exist, the script echoes a message to that effect.

Although this approach prevents the script from crashing, you might prefer that your script create the folder rather than simply report that the folder does not exist. To do this, create an instance of the FileSystemObject, and then call the CreateFolder method, passing the complete path to the new folder as the sole parameter. For example, the script in Listing 4.7 creates a new folder named C:\FSO.

Listing 4.7 Creating a New Folder

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\FSO")


If the folder already exists, a "File exists" error will occur. Because of that, you might want to check for the existence of the folder before trying to create (or, in that case, re-create) it.

Note

The FileSystemObject can only create folders on the local computer. If you need to create folders on a remote computer, you will need to use the WshController object. Alternatively, you can create a folder locally and then use WMI to move that folder to the remote computer. (The folder must be created and then moved because WMI does not have a method for creating folders.)


Deleting a FolderFrom time to time, folders need to be deleted. For example, you might have a file server that includes a folder for each individual user. When a user leaves the organization, the folder belonging to that user should be deleted; this helps ensure that the orphaned folder does not use up valuable disk space. Likewise, you might have a script that stores temporary files within a folder. Before the script finishes, you might want to delete that folder and thus remove all the temporary files.

The DeleteFolder method provides a way to delete a folder and all its contents. The DeleteFolder method requires a single parameter: the path of the folder to be deleted. For example, the script in Listing 4.8 deletes the folder C:\FSO and everything in it.

Listing 4.8 Deleting a Folder

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder("C:\FSO")


The DeleteFolder method deletes all items immediately; it does not ask for confirmation of any kind or send the items to the Recycle Bin.

Using Wildcards to Delete Folders
One of the main advantages of using scripts as a management tool is that scripts can operate on multiple items at the same time. For example, rather than delete a series of folders one by one, you can use scripts to delete a set of folders in a single operation.

The FileSystemObject allows you to use wildcard characters to delete a specific set of folders. For example, suppose you have the folder structure shown in Figure 4.1 and you want to delete all the subfolders beginning with the letter S.

Figure 4.1 Sample Folder Structure


This can be done by using the following command; when run against the sample folder structure, the command deletes the folders Scripts, Subfolder1, and Subfolder2:

objFSO.DeleteFolder("C:\FSO\S*")

This command deletes all the subfolders beginning with the letters Su, meaning only Subfolder1 and Subfolder2 will be deleted:

objFSO.DeleteFolder("C:\FSO\Su*")

Wildcard characters can appear only in the final part of the path parameter. For example, this command, which features a wildcard character in the middle of the path parameter, generates a "Path not found" error:

objFSO.DeleteFolder("C:\*\Subfolder1")



Copying a Folder and Its ContentsThe ability to copy a folder, and every item contained within that folder, is important in system administration. Sometimes you need to copy folders in order to create backups; by having the same folder on Computer A that you have on Computer B, you are less likely to experience data loss should Computer B unexpectedly fail. At other times, you might want to deploy all the files contained in a particular folder to a large number of computers. Using a script to copy this folder to each computer is far more efficient than performing the task manually.

The CopyFolder method allows you to copy a folder and its contents to another location. When used without any wildcard characters, the CopyFolder method functions like the Xcopy /E command: It copies all the files and all the subfolders, including any empty subfolders. The CopyFolder method requires two parameters:

Source folder (the folder being copied). This folder can be specified either as a local path (C:\Scripts) or as a UNC path (\\helpdesk\scripts).
Destination folder (the folder that will hold the copied information). This folder can also be specified either as a local path or as a UNC path. If the destination folder does not exist, the script automatically creates the folder.
In addition, the CopyFolder method accepts an optional third parameter, Overwrite. When this parameter is set to True, the default setting, the script overwrites any existing folders in the destination folder. For example, if you are copying a folder named Scripts, and the destination already contains a folder by that name, the destination folder will be replaced by the newly copied information. By setting this parameter to False, the script will not overwrite existing information and instead generates a run-time error.

Note

The CopyFolder method stops the moment it encounters an error, even if the script contains an On Error Resume Next statement. For example, suppose the script has 100 subfolders to copy, and CopyFolder successfully copies 3 of those subfolders before encountering an error. At that point, the CopyFolder method ends and the script fails; the script will not even attempt to copy the remaining 97 subfolders.
The script in Listing 4.9 uses the CopyFolder method to copy the contents of C:\Scripts to C:\FSO, overwriting any existing files in the destination folder. Note that this will not result in a folder named C:\FSO\Scripts; instead, the folder C:\FSO will simply contain the same files and folders as C:\Scripts. To create a folder named C:\FSO\Scripts, you would need to specify C:\FSO\Scripts as the destination folder.

Listing 4.9 Copying a Folder

1
2
3

Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Scripts" , "C:\FSO" , OverWriteFiles


Note

Because CopyFolder is a single operation, there is no way to track its progress; you simply have to wait until the operation has finished. If you want to monitor the progress of the copy command, you should use the Shell Application object instead. This object is discussed in "Files and Folders" in this book.
Using Wildcards to Copy Folders
The CopyFolder command copies the files stored in a folder as well as the files stored in any subfolders of that folder. This can be a problem; after all, what if you want to copy only the files in C:\FSO and not all the files stored in C:\FSO\Subfolder1, C:\FSO\Subfolder2, and C:\FSO\Subfolder3?

Unfortunately, there is no straightforward method for copying the files in a parent folder without also copying the files stored in child folders. You can use wildcard characters to limit the set of subfolders that are copied; for example, the following command copies only those folders that start with the letters log. However, when you use wildcard characters, no files other than those in the specified folders will be copied, not even files that begin with the letters log:

objFSO.CopyFolder "C:\Scripts\Log*" , "C:\Archive", True

When the preceding line of code is run, the folders C:\Scripts\Logs and C:\Scripts\Logfiles are copied, along with all the files stored within those folders. However, the files within the C:\Scripts folder are not copied.

When you use the CopyFolder method, you cannot copy only the files in a folder without also copying the files in any subfolders. To copy only the files and not the subfolders, use the CopyFile method instead. (This method is discussed later in this chapter.)


Moving a Folder and Its ContentsWhen you copy a folder from one location to another, you end up with duplicate copies of the information. Sometimes that is exactly what you want. On other occasions, however, you do not want two copies of the information; instead, you want to move the sole copy from Computer A to Computer B, or from hard disk C to hard disk D.

Moves such as this are often done to free disk space on a particular drive; for example, you might periodically move seldom-accessed folders to an archive drive. Alternatively, you might have a monitoring script that logs information to the local computer. When monitoring is complete, you might want that information uploaded to a central monitoring station and then deleted from the local computer. That way, the local computer will be prepared for the next round of monitoring.

The MoveFolder method accepts two parameters:

Source folder (the folder to be moved). This folder can be specified either as a local path or as a UNC path.
Destination folder (the location where the folder is to be moved). This folder can be specified either as a local path or as a UNC path.
If the destination folder does not exist, the source folder will be moved. If the destination folder already exists, however, the move operation will fail. You cannot use MoveFolder to overwrite an existing folder.

The script in Listing 4.10 moves the local folder, C:\Scripts, to the shared folder \\helpdesk\management.

Listing 4.10 Moving a Folder

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\Scripts" , "\\helpdesk\management"


Note that the MoveFolder method cannot perform any sort of rollback should the script fail. For example, suppose a network connection fails before a script has been able to move all the files from one computer to another. In a case such as that, you will end up with some files on Computer A, some files on Computer B, and possibly even a file or two lost in transit. However, there is no way for MoveFolder to roll back the failed transactions and restore the two computers to their previous states.

Because of that, you might want to use two methods, CopyFolder and DeleteFolder, when transferring folders and their contents across the network. You can use CopyFolder to copy the folder from Computer A to Computer B. If the copy operation succeeds, you can then use DeleteFolder to delete the folder on Computer A. If the operation fails, you can cancel the delete command and rest assured that the folder and all its contents are still safely stored on Computer A.



Renaming a FolderThe FileSystemObject does not include a method, such as RenameFolder, that provides an obvious way to rename a folder. However, you can rename a folder by using the MoveFolder method and maintaining the same relative location. For example, suppose you have a folder with the following path:

C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Current Logs

If you rename the folder by using the Rename command in Windows Explorer, the path remains identical except for the endpoint, the folder itself:

C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Archived Logs

The MoveFolder method enables you to achieve the same end result by moving the folder from C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Current Logs to C:\Scripts\PerformanceMonitoring\Servers\Domain Controllers\Archived Logs. The net result is exactly the same as that of using Windows Explorer to rename the folder.

For example, the script in Listing 4.11 uses MoveFolder to rename the folder C:\FSO\Samples to C:\FSO\Scripts. Before the script runs, Samples is the only subfolder in C:\FSO. After the script runs, Scripts is the only subfolder in C:\FSO. Furthermore, Scripts contains all the files and subfolders previously contained in Samples.

Listing 4.11 Renaming a Folder Using the MoveFolder Method

1
2

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\FSO\Samples" , "C:\FSO\Scripts"




Du har følgende muligheder
Dette spørgsmål er blevet annulleret, det er derfor ikke muligt for at tilføje flere kommentarer.
Søg
Reklame
Statistik
Spørgsmål : 173634
Tips : 31664
Nyheder : 719565
Indlæg : 6383595
Brugere : 218258

Månedens bedste
Årets bedste
Sidste års bedste