?
Solved

See hidden file extensions in VBScript

Posted on 2008-06-17
6
Medium Priority
?
1,609 Views
Last Modified: 2012-06-27
I have a script to enumerate the files in a directory, and dump the names to a text file, which is passed to the Windows FTP.exe program using the -s option.  The script works great when I run it from the command line.  I need to schedule it to run automatically under a different account, though -- the SQL Server Agent account, if it makes a difference.  When run under that account, the script fails to include file extensions in the output.  How can I get around this?  I don't have privileges to modify the environment for the account under which it has to run.  Is there a way to get around the explorer settings and retrieve the file extension?  I've tried an alternate implementation using WMI, but enumerating the files that way takes about 1000 times longer than using Shell.Application.
<package>
<job id="ftp">
<script language="VBScript">
Dim user, pass, server, local_path, remote_path
user="Admin"
pass="*******"
server="ftp_server"
local_path="file_path"
remote_path="remote_path"
 
' Create a temp file to hold the script
Dim fso, ofile, fname
Set fso=CreateObject("Scripting.FileSystemObject")
fname=fso.GetTempName()
Set ofile=fso.CreateTextFile("C:\temp\" & fname)
 
' Output login credentials to the script
ofile.WriteLine("open " & server)
ofile.WriteLine(user)
ofile.WriteLine(pass)
ofile.WriteLine("cd """ & remote_path & """")
 
 
Dim app, objFolder, colItems, i
Set app = CreateObject("Shell.Application")
Set objFolder = app.Namespace(local_path)
Set colItems = objFolder.Items
 
' This section finds the most recently-created replication directory.  
' They're named according to date and time, so it suffices to just find the one with the greatest name, lexicographically speaking
Dim latest
latest=""
 
For i = 0 to colItems.Count - 1
	If ((colItems.Item(i).Name > latest) And (colItems.Item(i).IsFolder)) Then
		latest = colItems.Item(i).Name
	End If
Next
 
 
Set colItems = Nothing
Set objFolder = Nothing
 
' set script to create this directory on the FTP server, and change to it
ofile.WriteLine("mkdir " & latest)
ofile.WriteLine("cd """ & latest & """")
 
' now enumerate the files in the local directory of interest, and dump them to the output file
local_path = local_path & "\" & latest
Set objFolder=app.Namespace(local_path)
Set colItems = objFolder.Items
For i = 0 To colItems.Count - 1
	If (Not colItems.Item(i).IsFolder) Then
' Here's where the problem is.  Neither of the following two approaches works.  Maybe something using WMI?
		'ofile.WriteLine("put """ & local_path & "\" & colItems.Item(i).Name & """")
		ofile.WriteLine("put """ & local_path & "\" & objFolder.GetDetailsOf(colItems.Item(i), 0) & """")
	End If
Next
 
ofile.WriteLine("quit")
ofile.Close()
Set ofile=Nothing
Set fso=Nothing
Set colItems = Nothing
Set objFolder = Nothing
Set app = Nothing
 
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "c:\windows\system32\ftp.exe -v -s:" & fname, , True
Set objShell = Nothing
Set fname = Nothing
</script>
</job>
</package>

Open in new window

0
Comment
Question by:smidgie82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:sr75
ID: 21807202
Try using the FSO.GetExtensionName method.  That should output only the extension of the file (if for some reason you do not know).

http://msdn.microsoft.com/en-us/library/x0fxha2a(VS.85).aspx
Set fso = CreateObject("Scripting.FileSystemObject")
GetAnExtension = fso.GetExtensionName("FilePath")

Open in new window

0
 
LVL 9

Author Comment

by:smidgie82
ID: 21807831
I tried this, using GetBaseName + "." + GetExtensionName instead of FolderItem.Name, but all I get is the filename (sans extension) with a period after it.  Apparently GetExtensionName is returning an empty string.  ?
0
 
LVL 15

Accepted Solution

by:
sr75 earned 2000 total points
ID: 21809388
Did you try using it the way I provided?

Try using something like this:

strExtensionName = FSO.GetExtensionName(FolderItem.Path & FolderItem.Name)

You may have to insert a '& "\" &' in the expression.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 9

Author Comment

by:smidgie82
ID: 21818334
I'm out of town, so I'm unable to test this, but I'll try it out as soon as I get back on Monday.
0
 
LVL 9

Author Comment

by:smidgie82
ID: 21911989
Interesting...  After much trial and error, here's where I'm at:

FolderItem.Path returns the full path, including extension.
fso.getExtensionName(FolderItem.Path) returns the extension correctly
fso.getExtensionName(FolderItem) returns an empty string when running as the system account, but returns the extension correctly when running under my user account
FolderItem.Name returns the filename sans extension when running as the system account, but returns the filename with extension under my account

After going to explorer and checking the option for "hide file extensions for known file types", the behavior under my user account mirrors the behavior when run under the system account.

I finally get the behavior I want by using fso.GetBaseName(FolderItem.Path) & fso.GetExtensionName(FolderItem.Path).  Irritating, but it works.

Thanks for your help.
0
 
LVL 15

Expert Comment

by:sr75
ID: 21915429
Glad to help get you a working solution.  And thanks for the points.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question