Post by xxgeek on Jul 18, 2021 12:46:21 GMT
Copy Paste this into JB IDE and save it to it's own folder before running . It creates a vbs script .
Use this to copy folders from a source folder to a destination folder including sub folders and files.
Adapt it to use as a sub or function
Problem was I only tested copying a folder to a drive.
When a drive is destination, the path returned a "\" at the end of the path
When a folder was selected, there was no "\" at the end of the path
To copy to a destination folder it needed the "\"
Sorry folks, I messed up !!!
Fixed and improved - Hopefully bullet proof. Save this file to it's own folder and I recommend using test folders first.
Use this to copy folders from a source folder to a destination folder including sub folders and files.
Adapt it to use as a sub or function
Problem was I only tested copying a folder to a drive.
When a drive is destination, the path returned a "\" at the end of the path
When a folder was selected, there was no "\" at the end of the path
To copy to a destination folder it needed the "\"
Sorry folks, I messed up !!!
Fixed and improved - Hopefully bullet proof. Save this file to it's own folder and I recommend using test folders first.
'Author -xxgeek - a member of the Just Basic Forums - https://justbasiccom.proboards.com/
'Purpose - Copies selected source folder to selected destination folder includes all sub folders and all files
'posted to help others learn by example and keep Just Basic relevant by showing
'what Just Basic and Windows can do working together
'Two prompts: First to select a source folder, and the second to select a destination folder
nomainwin
global selectedpath$, t
t = 0
q$ = chr$(34) 'double quotes
'get source folder
a$ = getFolder$(a$)
sourceFolder$ = selectedpath$
'get destination folder
a$ = getFolder$(a$)
if right$(selectedpath$,1) <> "\" then selectedpath$ = selectedpath$;"\"
destFolder$ = selectedpath$
' VBScript to copy folder
'make sure no previous copyfolder.vbs exists, delete if true
copy$ = "copyfolder.vbs"
if fileExists(DefaultDir$,copy$) then kill DefaultDir$;"\";copy$
open copy$ for output as #1
#1, "Dim FSO"
#1, "Set FSO = CreateObject(";q$;"Scripting.FileSystemObject";q$;")"
#1, "FSO.CopyFolder ";q$;sourceFolder$;q$;", ";q$;destFolder$;q$
close #1
'run folder copy script
run "wscript ";copy$
'function for checking file existence
function fileExists(path$, filename$)
dim info$(0, 0)
files path$, filename$, info$()
fileExists = val(info$(0, 0)) 'non zero is true
end function
'function for checking folder existence
function pathExists(path$)
pathExists = (mkdir(path$)=183)
end function
'functions for making the folder dialog window
'Must add selectedpath$ to "global" at top of your bas file eg: global, selectedpath$
function getFolder$(a$)
q$ = chr$(34) ' quotes
'write the following vbs script to temp file named FolderDialog.vbs
folderDialog$ = "FolderDialog.vbs"
open folderDialog$ for output as #1
'print each line to file (FolderDialog.vbs)
#1,"Function BrowseFolder( myStartLocation, blnSimpleDialog )"
#1,"Const MY_COMPUTER = &H11&"
#1,"Const WINDOW_HANDLE = 0"
#1,"Dim numOptions, objFolder, objFolderItem"
#1,"Dim objPath, objShell, strPath, strPrompt"
if t <> 0 then [destDialog]
#1,"strPrompt = ";q$;"Select Source Folder (copies folder, sub folders and all files) ";q$ 'source dialog
if t = 0 then t = t + 1 : goto [bypass]
[destDialog]
#1,"strPrompt = ";q$;"Select Destination Folder (pastes selected source folder) ";q$ 'destination dialog
[bypass]
#1,"If blnSimpleDialog = True Then"
#1,"numOptions = 0"
#1,"Else"
#1,"numOptions = &H10&"
#1,"End If"
#1,"Set objShell = CreateObject( ";q$;"Shell.Application";q$;" )"
#1,"If UCase( myStartLocation ) = ";q$;"MY COMPUTER";q$;" Then"
#1,"Set objFolder = objShell.Namespace( MY_COMPUTER )"
#1,"Set objFolderItem = objFolder.Self"
#1,"strPath = objFolderItem.Path"
#1,"Else"
#1,"strPath = myStartLocation"
#1,"End If"
#1,"Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _"
#1,"numOptions, strPath )"
#1,"If objFolder Is Nothing Then"
#1,"BrowseFolder = ";q$;q$
#1,"Exit Function"
#1,"End If"
#1,"Set objFolderItem = objFolder.Self"
#1,"objPath = objFolderItem.Path"
#1,"BrowseFolder = objPath"
#1,"End Function"
#1,"Dim bf"
#1,"bf = BrowseFolder( ";q$;"My Computer";q$;", False )"
#1,"Dim fso : Set fso = CreateObject(";q$;"Scripting.FileSystemObject";q$;")"
#1,"Dim sp : sp = fso.GetFile(Wscript.ScriptFullName)"
#1,"Dim fp: fp = fso.GetParentFolderName(sp) & ";q$;"\FolderPath.txt";q$
#1,"Dim f : Set f = fso.CreateTextFile(fp)"
#1,"f.WriteLine bf"
#1,"f.Close"
close #1
'loop until FolderDialog.vbs existence is verified
do
res = fileExists(DefaultDir$,folderDialog$)
if res then exit do
scan
loop until res
'run the script (creates temp file FolderPath.txt)
run "wscript ";"FolderDialog.vbs"
'loop until the selected folder path is written to temp file (FolderPath.txt)
do
folderpath$ = "FolderPath.txt"
res = fileExists(DefaultDir$,folderpath$)
if res then exit do
scan
loop until res
'get the text in FolderPath.txt (the selected path of folder)
open folderpath$ for input as #1
line input #1, line$(x)
selectedpath$ = line$(x)
close #1
'delete both temp files (FolderPath.txt, and FileDialog.vbs)
res = fileExists(DefaultDir$,folderpath$)
if res then kill folderpath$
res = fileExists(DefaultDir$,folderDialog$)
if res then kill folderDialog$
if selectedpath$ = "" or left$(selectedpath$,1) = ":" then notice "No Folder selected" : end
if p = 0 and right$(selectedpath$,1) = "\" then confirm "You have chosen Drive "+selectedpath$+" Are you sure you want to copy an entire drive?";r$ : if r$ <> "yes" then end
end function