Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would appreciate some help please. I am trying to copy two files "test1.txt" and "test2.txt" into an unknown directory, e.g. into the directory "%USERNAME%\Documents\test\04pql7hw.example" The first section of the directory name is a randomized string of letters and numbers and the second section "example" is constant for every pc, e.g the directory name could be "gjl39756.example" or "9kny0x5w.example".

So far i think that the starting point should be a batch file which copies a list of the directory "%USERNAME%\Documents\test" sub directorie's into a text document, and it saves into the same directory.

dir /ad >list.txt

As the only subdirectory in the directory "%USERNAME%\Documents\test" is the unknown directory e.g "04pql7hw.example" the resulting text document will always have the directory name at the same line and character positions of the file. here is the an example of the resulting list.txt

    Volume in drive C has no label.
 Volume Serial Number is 82CE-AEC8

 Directory of C:\Users\%USERNAME%\Test\

08/08/2009  02:51    <DIR>          .
08/08/2009  02:51    <DIR>          ..
06/08/2009  22:49    <DIR>          04pql7hw.example
               0 File(s)              0 bytes
               3 Dir(s)  430,968,176,640 bytes free

I then thought that a vbscript could read that specifice line and characters, and then use this information to copy the two files to the unknown folder.

The script i have so far is this...please dont laugh as i am a scripting novice...

Set FS = CreateObject("Scripting.FileSystemObject")
    Set f1 = fs.OpenTextFile("%USERNAME%\test\", 1, False)
myarr = split(f1.ReadAll, vbnewline)
f1.close
mystr = mid( myarr(8), 36, 16)

If i am going at this from the complete wrong angle please tell me... Thankyou in advance for any help on this issue. Regards Alex

share|improve this question

3 Answers 3

Reading and parsing a directory listing is one way (although I would use "dir /ad /b" in this case to make it simpler to parse) although it is kinda hacky. You can enumerate a directory's subdirectories using the FileSystemObject too, which would be my preferred way.

http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_jozd.mspx?mfr=true

share|improve this answer

I was bored so I quickly made this for you. Hope it helps:

Option Explicit

Dim fsIn, strParentFolder, objSubFolder, colSubFolders, strDestinationFolder
Dim strSearchTerm, strRootFolder

strSearchTerm = ".example"
strRootFolder = "%USERNAME%\Documents\test"


Set fsIn = CreateObject("Scripting.FileSystemObject") 

If Not fsIn.FolderExists(strRootFolder) Then
    WScript.Echo "The parent folder (" & strRootFolder & ") does not exist. Exiting script"
    WScript.quit
End If

Set strParentFolder = fsIn.GetFolder(strRootFolder) 
Set colSubFolders = strParentFolder.SubFolders 

For Each objSubFolder In colSubFolders
    If InStr(strSearchTerm,objSubFolder.Name) > 0 Then
    	wscript.Echo objSubFolder.Name & " is the destination folder"
    	strDestinationFolder = objSubFolder.Name
    End If
Next
  • Just change ".example" on line 6 to whatever you want to search for in the folder name.

  • Change "%USERNAME%\Documents\test" to whatever is the parent folder you want to search in

  • strDestinationFolder is the variable containing the result (the foldername you are looking for)

share|improve this answer

You can use the Windows shell commands "FOR" and "SET" to populate the directory name into an environment variable. You can then use that variable to copy your files as needed.

Let's assume I have the following "unknown" directory:

C:\test>dir
 Volume in drive C has no label.
 Volume Serial Number is 54B3-BFB6

 Directory of C:\test

08/11/2009  01:02 PM    <DIR>          .
08/11/2009  01:02 PM    <DIR>          ..
08/11/2009  01:02 PM    <DIR>          04pql7hw.example
               0 File(s)              0 bytes
               3 Dir(s)  125,798,892,032 bytes free

C:\test>

At the command line I can do this:

for /F %i in ('dir /ad /b *example') do set foo=%i

I can now echo foo to prove it has been assigned the directory name:

C:\test>echo %foo%
04pql7hw.example

C:\test>

Now you can copy at your leisure.

WARNING - if you put these commands into a shell script, then remember to double up the % signs from %i to %%i, something like this:

echo off
for /F %%i in ('dir /ad /b *example') do set foo=%%i
echo %foo%

Is that what u needed?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.