Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to programmatically encrypt

Status
Not open for further replies.

bluray

Technical User
Mar 3, 2006
12
NO
Hello again,

I would like to know if it is possible to programmatically encrypt files (with EFS) with vbscript - set the 'encryption flag'.

I've tried to search in this forum and elsewhere, but can't find any mention of any methods anywhere.

I'd be really grateful for help or pointers to information!

What I want to do, is to encrypt files with efs, but keep the timestamp of the file. I've already gotten excellent help from members of this forum.

So far, I've tried the following:

Code:
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("cipher.exe /A " & filename, 1, true)

 
I suppose you could get the last modify date from your file prior to executing the command you posted and then set it back with the function in your previous post.

Is that what you are looking at doing?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Yes exactly!

I've got the script working, but I'd like to find a better way to encrypt the files than using WshShell.Run and CIPHER.EXE.

Here's the first attempt to put everything together. This script takes a folder name as parameter, encrypts everything inside it and all subfolders, but preserves the timestamps of all files.

Code:
Dim strDir, objShell, FSO, sf, WshShell
strDir = "C:\test\"

ok = MsgBox("This script will encrypt " & strDir & " and its contents." & vbCrLf & "Continue?", 1)
If ok<>1 Then Wscript.Quit

Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

Set objDir = FSO.GetFolder(strDir)

encryptRecursivePreserveModDate(objDir)

Sub encryptRecursivePreserveModDate(pCurrentDir)
	
	Dim objFolder, correctDate
	
	'Try to catch some errors, most likely access denied
	On Error Resume next
	numFiles = pCurrentDir.Files.Count
	
	If Err.Number <>0 Then
	    Wscript.Echo "Error: " & Err.Number & " " & Err.Description
	    Err.Clear
	Else
	
	On Error Goto 0
	
	Set objFolder = objShell.NameSpace(pCurrentDir.Path)
	Set sf = objShell.NameSpace (pCurrentDir.Path)
	
	For Each aItem In pCurrentDir.Files
		correctDate = aItem.DateLastModified
		Set fi = sf.ParseName (aItem.Name)
		retval = WshShell.Run("CIPHER.EXE /e /a " & chr(34) & aItem.Path & chr(34), 2, true)
		'manipulates the FolderItem's Last-Modificaton-Date
		fi.ModifyDate = correctDate
	Next
	'numSubfolders = pCurrentDir.SubFolders.Count
	For Each aItem In pCurrentDir.SubFolders
	   encryptRecursivePreserveModDate(aItem)
	Next
	End If	
	On Error Goto 0

End Sub

msgbox("done")

I'd be grateful for all suggestions of improvements!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top