VB script_CReating Folder inside folder - vbscript

For i = 1 To 40 Step 1
ChildFolderPath = ChildFolderPath & "\" & "LargeFolder" & i
If fso.FolderExists(ChildFolderPath) Then
MsgBox ("Folder Exists")
else
fso.CreateFolder (ChildFolderPath)
End If
Next
But after creating 21 folders, I am getting error 53, "File path not found". Why?

You're hitting the Windows limitation on maximum path length, which is 260 characters.
The linked article also mentions a workaround to use extended-length paths up to (approximately) 32767 characters long – add \\?\ before the drive name. For example:
ChildFolderPath = "\\?\C:\MyFolder"
As also noted in that article, even though you can create extended-length paths programmatically, the Windows shell (e.g. Explorer) may be unable to handle them properly. For example, you may get the "The source file name(s) are larger than is supported by the file system" error when trying to delete a long path folder from Explorer or the command prompt.

Related

Unzip file silently and without dialog box to overwrite

I am working on vbscript to unzip the multiple files one after another.
I am using below code
Dim folder(3)
folder(0) = "UBO90R1"
folder(1) = "UBO90R2"
folder(2) = "UBO100R1"
folder(3) = "UBO100R2"
For i = 0 To 3
unzip_Source = "D:\Autobackup\" & folder(i) & ".zip"
unzip_destination = "D:\Autobackup_unzip\" & folder(i) &"\"
Call ExtractFilesFromZip(unzip_Source,unzip_destination)
WScript.Echo "unzip Finished"
Next
Sub ExtractFilesFromZip(pathToZipFile, dirToExtractFiles)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
pathToZipFile = fso.GetAbsolutePathName(pathToZipFile)
dirToExtractFiles = fso.GetAbsolutePathName(dirToExtractFiles)
If (Not fso.FileExists(pathToZipFile)) Then
WScript.Echo "Zip file does not exist: " & pathToZipFile
Exit Sub
End If
If Not fso.FolderExists(dirToExtractFiles) Then
WScript.Echo "Directory does not exist: " & dirToExtractFiles
Exit Sub
End If
dim sa : Set sa = CreateObject("Shell.Application")
Dim zip : Set zip = sa.NameSpace(pathToZipFile)
Dim d : Set d = sa.NameSpace(dirToExtractFiles)
d.CopyHere zip.items, 4
Do Until zip.Items.Count <= d.Items.Count
WScript.Sleep(200)
Loop
End Sub
The Problem currently I have that if the folder or file is already exist than it open the dialog to the user to select any option Overwrite, keep both file etc.
If I change the following line from the code
d.CopyHere zip.items, 4
'Integer 4 didn't show the progress bar
to
d.CopyHere zip.items, 16
'Integer 16 overwrite the existing file but it shows the progress bar.
I would like to overwrite the existing file without any dialog box and without any progress bar.
PS: Code to unzip copied from here.
Looking at help we would find the follwing.
Type: FILEOP_FLAGS
Flags that control the file operation. This member can take a combination of the following flags.
FOF_ALLOWUNDO
Preserve undo information, if possible.
Prior to Windows Vista, operations could be undone only from the same process that performed the original operation.
In Windows Vista and later systems, the scope of the undo is a user session. Any process running in the user session can undo another operation. The undo state is held in the Explorer.exe process, and as long as that process is running, it can coordinate the undo functions.
If the source file parameter does not contain fully qualified path and file names, this flag is ignored.
FOF_CONFIRMMOUSE
Not used.
FOF_FILESONLY
Perform the operation only on files (not on folders) if a wildcard file name (.) is specified.
FOF_MULTIDESTFILES
The pTo member specifies multiple destination files (one for each source file in pFrom) rather than one directory where all source files are to be deposited.
FOF_NOCONFIRMATION
Respond with Yes to All for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR
Do not ask the user to confirm the creation of a new directory if the operation requires one to be created.
FOF_NO_CONNECTED_ELEMENTS
Version 5.0. Do not move connected files as a group. Only move the specified files.
FOF_NOCOPYSECURITYATTRIBS
Version 4.71. Do not copy the security attributes of the file. The destination file receives the security attributes of its new folder.
FOF_NOERRORUI
Do not display a dialog to the user if an error occurs.
FOF_NORECURSEREPARSE
Not used.
FOF_NORECURSION
Only perform the operation in the local directory. Do not operate recursively into subdirectories, which is the default behavior.
FOF_NO_UI
Windows Vista. Perform the operation silently, presenting no UI to the user. This is equivalent to FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR.
FOF_RENAMEONCOLLISION
Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists at the destination.
FOF_SILENT
Do not display a progress dialog box.
FOF_SIMPLEPROGRESS
Display a progress dialog box but do not show individual file names as they are operated on.
FOF_WANTMAPPINGHANDLE
If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object that contains their old and new names to the hNameMappings member. This object must be freed using SHFreeNameMappings when it is no longer needed.
FOF_WANTNUKEWARNING
Version 5.0. Send a warning if a file is being permanently destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.

VBA accessing files from a remote directory (error 76 - path not found)

I have a remote server I have read permission (in Windows I labeled it as my X: drive)
The correct path to a file I need to access is:
"X://some dir/some file"
The file that holds the Macro that is running exists in the C: drive. The code below checks to see if the path exists
If dir("X://some folder/some file", vbDirectory) = "" Then
Debug.Print "dir does not exist"
End If
Running that above enters the branch telling me the file does not exist. My questions are:
Do I have to put the name of the drive shorcut? (eg X: or can I put //: instead?)
How can I debug in Excel if I'm even allowed to enter the drive?
First, use backslashes for Windows paths. Second, don't use double backslashes when referring to a mapped drive. (Labelling the drive is meaningless to VBA.) eg:
X:\some folder\some file
If you didn't actually create a mapped drive, you will need to use the UNC or IP (and then you do use the double backslash) . EG:
\\remotehost\path\to\somefolder\somefile.txt
or
\\127.0.0.1\path\to\some folder\some file.txt
I think Tim is correct.
How can I debug in Excel if I'm even allowed to enter the drive?
If you RECORD macro opening a file in your X drive. You will see all the code that you need to put your original code working ;) . Every VBA coder do that kind of tricks.

Read path from file and check if file exists in a specified path

I am trying to read a PATH from a text file and check whether the file exists in the PATH specified in the "input.txt" file. Though the file is present in the specified location. The loop continues to run.Please help
Below is my VBScript
Set oReadObj = CreateObject("Scripting.FileSystemObject")
set oRead = oReadObj.OpenTextFile("C:\input.txt", 1)
loc = oRead.ReadAll()
Do Until oReadObj.FileExists(loc)
wscript.sleep 5000
Loop
msgbox "file found"
you don't need a loop to check if the file exists (if you have the full path of it), here is the code you need.
Set oReadObj = CreateObject("Scripting.FileSystemObject")
set oRead = oReadObj.OpenTextFile("C:\input.txt", 1)
loc = oRead.ReadAll()
If oReadObj.FileExists(loc) Then msgbox "file found" else msgbox "file not found"
.ReadAll() may include a EOL from the file, which surely isn't part of the path; use .ReadLine(), as you just want to get one path.
On second thought:
There may be other junk (spaces, BOM, ...) in your file. Did you try something like
WScript.Echo ">" & loc & "<"
to check for those or typos?
Reboot:
Based your comments:
If I specify the "path(location of installer)" instead of variable
"loc", I am getting expected result
This is the content of my " input.txt" file
"C:\Users\Administrator\Downloads\Storage_Manager_Server-windows-x86_64-6.0.0.ex‌​e"
we can
rule out permission problems (usual suspects if "Administrator" pops up)
conclude that loc - i.e. the file's content - is to blame
If we are sure that EOL's, blanks, typos are not the cause of .FileExists' failure, then the most likely hypothesis is: The file contains the " (quoted path), which is good for the shell, but fatal for FileSystemObject methods.

Delete file in windows 7 using VB.NET

I have written following code in vb.net to delete the file.
If File.Exists(strPath & "\ReportEng.ini") = True Then
File.SetAttributes(strPath & "\ReportEng.ini", FileAttributes.Normal)
File.Delete(strPath & "\ReportEng.ini")
End If
File.Copy("\\192.168.0.1\SAP_Shared\AddonExtra\ReportEng.ini", strPath & "\ReportEng.ini")
This code works perfectly in windows xp. But in Windows 7,I can not delete it. This OS is hectic OS from developer's point of view. Some or other problem occurs and Microsoft has not considered the developer while building this OS.
How do I delete file in Windows 7 ?
It's so easy to do so;
If My.Computer.FileSystem.FileExists("C:\somefile.ext") Then 'Check whether file exists
My.Computer.FileSystem.DeleteFile("C:\somefile.ext") 'Delete the file!
End If
Have a nice day!
You don't need to delete the file: there is an overload File.Copy Method (String, String, Boolean) which allows overwriting.
You didn't say what error you get. I suspect it is because the user doesn't have write access to the directory. You should probably be using a subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or maybe .LocalApplicationData, and definitely not the directory containing the program.
Also, using Path.Combine(strPath, "ReportEng.ini") is how you're meant to combine paths - it'll take care of, e.g., the trailing path separator for you.
The preferred method for interfacing with the Windows file system uses the following namespace:
Imports Microsoft.VisualBasic.FileIO.FileSystem
To delete a file:
Dim FileLocation As String = strPath & "\ReportEng.ini"
If Not GetDirectoryInfo(FileLocation).Exists Then
GetFileInfo(FileLocation).Delete()
End If

Client-Side VBScript application, Incorrect Current Working Directory

I'm not understanding this behavior. Maybe someone can explain to me why my current working directory is not what I expect.
On my desktop, I have a folder called STKGui:
C:\Documents and Settings\Lauren\Desktop\STKGui
Located in that directory are the following files: gui.html, style.css, save.html, load.html Within STKGui there are also the following directories: Images, Scripts, and SaveData. Scripts contains various .vbs files, including gui.vbs.
I start with gui.html. I click a button which takes me to load.html. load.html uses scripts from Scripts\gui.vbs. One of the functions loads a database, and to do so I provide the location of the database: C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb Of course I want to use a relative file path instead of a fixed path. My initial attempt to load the database failed; it was trying to load from C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb. So to troubleshoot I printed out the current working directory; much to my chagrin it was C:\Documents and Settings\Lauren\Desktop
I don't understand why my desktop is my current working directory. Shouldn't it be where the file is running from? I figured it would be either C:\Documents and Settings\Lauren\Desktop\STKGui (the location of load.html) OR C:\Documents and Settings\Lauren\Desktop\STKGui\Scripts (the location of gui.vbs which contains the function that's trying to load the database/printing debug messages of the current working directory).
Can someone explain why the current working directory is what it is, or better yet tell me how to get what I really want, which is the location of the files executing? (I don't care if it's the main STKGui folder or the scripts folder--as long as it's within the application's directory structure I can work with it!)
EDIT (7/14/10 4:02 pm EDT):
Various attempts at printing the current working directory or grabbing files based on what I -thought- was the relative path from my executing script have resulted in my desktop's path instead of the path of the executed script. I stumbled across this link: http://leereid.wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ but none of the solutions are working for me, as I get run-time errors regarding the Wscript object. So while I don't know if any of the solutions on the aforementioned link will produce different results, if someone can help me get at least one of them working so I can find out that may be a step in the right direction.
One of the solutions, reproduced below:
Set oShell = CreateObject("WScript.Shell")
Set ofso = CreateObject("Scripting.FileSystemObject")
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
produces the following error:
Object required: 'Wscript' line: 659 char: 1
with line 659 being:
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
For Server-Side:
You should be using Server.MapPath() to get your "working directory". For instance, if you want to get the path to your database file in C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb, your app root being C:\Documents and Settings\Lauren\Desktop\STKGui, you would use Server.MapPath("SaveData\SaveData.accdb").
For Client-Side:
Upon closer examination and digging up some memories, I realized that MapPath is only available from the Server class. Instead, you need to create a file system object like this:
''get fs object
Set objFSO = CreateObject("Scripting.FileSystemObject")
''get actual file using path relative to calling vbs file
Set objFile = objFSO.GetFile("SaveData\SaveData.accdb")
''get path to the database
set sPathToDatabase = objFSO.GetAbsolutePathName(objFile)
In case it helps, here is a great resource for working with the file system in vbScript: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/
This solution was NOT ideal, but what I ended up doing was parsing the url in my browser to get the directory.
guiPath = Mid(location.PathName, 2, len(location.PathName))
Set regExp = New RegExp
regExp.IgnoreCase = False
regExp.Global = True
regExp.Pattern = ".*/"
Set matchCollection = regExp.Execute(guiPath)
Set match = matchCollection(0)
guiPath = match.value
regExp.Pattern = "%20"
guiPath = regExp.Replace(guiPath, " ")
systemsDBPath = guiPath & "SaveData\SaveData.accdb"
Like I said, less than ideal. May not even work once I'm working with the application this will be running in. But I couldn't find a better way.

Resources