VB Script copy file
Hi I am trying to copy a file into a folder but the folder name is random on each PC so how can i do this with FSO or another way in a VB script? For example. The folder structure maybe c:\folder1\test\live\765765675\ and i need to copy a file called hello.txt into that folder but on each pc the folder after \live is a random number ? can anyone give me some example code to do this ? Any ideas ? Thanks
December 2nd, 2009 7:48pm

You can use the FileSystemObject for this. If the randomly named folder is the only sub folder of the "live" folder, the code could be similar to the following: Option Explicit Dim objFSO, objFolder, objSubFolder, strFile, strSource strSource = "\\MyServer\MyShare" strFile = "hello.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder("c:\rlm\Scripts\Test") For Each objSubFolder In objFolder.SubFolders objFSO.CopyFile strSource & "\" & strFile, _ objSubFolder.Path & "\" & strFile Next Or, if there is more than one sub folder, the above will copy the designated file to all sub folders. If only one of the sub folders has a numeric name, you can use the IsNumeric function to find it. For example: Option Explicit Dim objFSO, objFolder, objSubFolder, strFile, strSource strSource = "\\MyServer\MyShare" strFile = "hello.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder("c:\rlm\Scripts\Test") For Each objSubFolder In objFolder.SubFolders If (IsNumeric(objSubFolder.Name) = True) Then objFSO.CopyFile strSource & "\" & strFile, _ objSubFolder.Path & "\" & strFile End If Next Richard MuellerMVP ADSI
Free Windows Admin Tool Kit Click here and download it now
December 3rd, 2009 7:02am

Hello Richard, that worked great. Thanks.
December 3rd, 2009 7:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics