0% found this document useful (0 votes)
132 views7 pages

VBS

The documents demonstrate various methods of copying files and folders in Windows using VBScript, including: 1) Copying a file from one location to another using the FileSystemObject CopyFile method. 2) Choosing a file or folder using an HTA application with input fields to select the source and destination. 3) Listing all files and subfolders recursively in a given folder path.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views7 pages

VBS

The documents demonstrate various methods of copying files and folders in Windows using VBScript, including: 1) Copying a file from one location to another using the FileSystemObject CopyFile method. 2) Choosing a file or folder using an HTA application with input fields to select the source and destination. 3) Listing all files and subfolders recursively in a given folder path.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

///////////////////////////////////////////////////////////////////////////////////

/////////////////////////

just copy:

Const DestinationFile = "C:\Users\Administrator.WMMB-OPWTEST\Desktop\Copy


Test\Destination\"
Const SourceFile = "C:\Users\Administrator.WMMB-OPWTEST\Desktop\Copy
Test\Source\Test.txt"

'Const DestinationFile = "c:\destfolder\anyfile.txt"


'Const SourceFile = "c:\sourcefolder\anyfile.txt"

Set fso = CreateObject("Scripting.FileSystemObject")

' Get name of the file only-----------------------------


Dim objFile
Set objFile = fso.GetFile(SourceFile)
'Wscript.Echo "File name: " & fso.GetFileName(objFile)
filename = fso.GetFileName(objFile)
'-------------------------------------------------------

'Check to see if the file already exists in the destination folder


'If fso.FileExists(DestinationFile+filename) Then
'Check to see if the file is read-only
' If Not fso.GetFile(DestinationFile+filename).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
' fso.CopyFile SourceFile, DestinationFile, True
' Else
'The file exists and is read-only.
'Remove the read-only attribute
' fso.GetFile(DestinationFile+filename).Attributes =
fso.GetFile(DestinationFile+filename).Attributes - 1
'Replace the file
' fso.CopyFile SourceFile, DestinationFile, True
'Reapply the read-only attribute
'fso.GetFile(DestinationFile+filename).Attributes =
fso.GetFile(DestinationFile+filename).Attributes + 1
'End If
'Else
'The file does not exist in the destination folder. Safe to copy file to
this folder.
fso.CopyFile SourceFile, DestinationFile, True
'End If

Set fso = Nothing

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

choose file:

Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file
id=FILE><script>FILE.click();new
ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.val
ue);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
wscript.echo sFileSelected

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

choose folder hta:

<html>
<head>
<HTA: Application
Border = Thick
BorderStyle = Complex
ShowInTaskBar = No
MaximizeButton = No
MinimizeButton = No
>

</object>
<script language = "VBScript">

Sub ChooseSaveFolder
strStartDir = "c:\"
userselections.txtFile.value = PickFolder(strStartDir)
End Sub

Function PickFolder(strStartDir)
Dim SA, F
Set SA = CreateObject("Shell.Application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.Items.Item.path
End If
Set F = Nothing
Set SA = Nothing
End Function

</script>

</head>
<body>
<form name="userselections">
<input type = "text" name = "txtFile" size="50" />

<input type = "button" value = "Browse ..." onClick="ChooseSaveFolder" />


</form>
</body>
</html>
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

listAllFiles&SubFolders:

Set objFSO = CreateObject("Scripting.FileSystemObject")

startFolder = "D:\testfolder1"

' start object


Set objFolder = objFSO.GetFolder(startFolder)

If objFSO.FolderExists(startFolder) Then

' get name


Set objFolder = objFSO.GetFolder(startFolder)
sPath = objFolder.ShortName
WScript.Echo sPath
Wscript.Echo "EXISTS"
''----------------

destFolderPath = "D:\"+sPath+" Copy"

objFSO.CreateFolder(destFolderPath)

End If

' echo the folder


Wscript.Echo objFolder.Path

' get the path of the files in the folder


Set colFiles = objFolder.Files

' loop and echo every file in the folder path


For Each objFile in colFiles
Wscript.Echo objFile.Name

' Next = exit loop


Next

'empty echo
Wscript.Echo

' Recursive function


ShowSubfolders objFSO.GetFolder(startFolder)

Sub ShowSubFolders(Folder)

' loop for every sub folder in the path


For Each Subfolder in Folder.SubFolders

' echo sub folder path


Wscript.Echo Subfolder.ShortName
'objFSO.CreateFolder("D:\"Subfolder.Path" Copy")

' start object


Set objFolder = objFSO.GetFolder(Subfolder.Path)

' get the path of the files in the folder


Set colFiles = objFolder.Files

' loop and echo every file in the folder path


For Each objFile in colFiles
Wscript.Echo objFile.Name

Next

Wscript.Echo

' loop again if any folder exists


ShowSubFolders Subfolder

Next

End Sub

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

listAllFiles:

Set objFSO = CreateObject("Scripting.FileSystemObject")

objStartFolder = "D:\testfolder1"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files

'create a reference to the Files property


For Each objFile in colFiles

Wscript.Echo objFile.Name

Next

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

copy hta:
<html>
<head>
<title>My Logfile App</title>
<HTA:APPLICATION
APPLICATIONNAME="My Logfile App"
ID="MyLogfileApp"
VERSION="1.0"/>
</head>

<script language="VBScript">

Sub Window_OnLoad
window.resizeto 500,500
End Sub

Dim destination,source

Sub Start_Button()

source = document.getElementById("source").value
destination = document.getElementById("destfile").value

' Copy script-------------------------------------------------

Set fso = CreateObject("Scripting.FileSystemObject")

'Check to see if the file already exists in the destination folder

'If fso.FileExists(destination) Then


'Check to see if the file is read-only
' If Not fso.GetFile(destination).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile source, destination, True
' Else
'The file exists and is read-only.
'Remove the read-only attribute
' fso.GetFile(destination).Attributes =
fso.GetFile(DestinationFile).Attributes - 1
'Replace the file
' fso.CopyFile source, "C:\destfolder\", True
'Reapply the read-only attribute
'fso.GetFile(destination).Attributes =
fso.GetFile(DestinationFile).Attributes + 1
' End If
' Else
'The file does not exist in the destination folder. Safe to copy file to
this folder.
' fso.CopyFile source, destination, True
' End If

Set fso = Nothing

'-------------------------------------------------------------
' Choose folder script--------------------------------------

'Sub ChooseSaveFolder
' strStartDir = "c:\"
' userselections.txtFile.value = PickFolderSource(strStartDir)
'End Sub
'Function PickFolderSource(strStartDir)
'Dim SA, F
'Set SA = CreateObject("Shell.Application")
'Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
'If (Not F Is Nothing) Then
' PickFolderSource = F.Items.Item.path
'End If
'Set F = Nothing
'Set SA = Nothing
'End Function

'---------------------------------------------------
' chose file-----------------
Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file
id=FILE><script>FILE.click();new
ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.val
ue);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
wscript.echo sFileSelected
soruce = sFileSelected

'----------------------------

Window.Alert "Done!"
End Sub
</script>

<!-- BODY------------------------------------------------------>

<body bgcolor="white">

<center>
<p></>
<p></>
<p></>
<label>Choose your sorce file.</label><br />
<p></>
<!-- <input type = "button" value = "Browse ..." onClick="ChooseSaveFolder" />
-->

<input type="text" id="source" name="sourcefile">


<p></>
<p></>
<p></>

<label>Choose your destination file.</label><br />


<p></>
<input type="text" id="dest" name="destfile">

<p></>
<p></>
<p></>

<input type="button" name="btnStart" id="btnStart" value="Start Copying"


onclick="Start_Button">
</center>
</body>

<!-------------------------------------------------------------->

</html>

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////

You might also like