读取一个未知的目录名称+复制到它

我希望得到一些帮助。 我试图复制两个文件“test1.txt”和“test2.txt”到一个未知的目录,例如到目录“%USERNAME%\ Documents \ test \ 04pql7hw.example”目录名的第一部分是一个随机化字母和数字串,第二部分“示例”对于每个pc是恒定的,例如目录名称可以是“gjl39756.example”或“9kny0x5w.example”。

到目前为止,我认为起点应该是一个batch file,它将目录“%USERNAME%\ Documents \ test”子目录的列表复制到文本文件中,并保存到同一目录中。

dir /ad >list.txt 

由于目录“%USERNAME%\ Documents \ test”中的唯一子目录是未知目录,例如“04pql7hw.example”,所以生成的文本文件将始终具有与文件的相同行和字符位置的目录名称。 这里是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 

然后,我认为一个VBScript可以读取特定的行和字符,然后使用这些信息将这两个文件复制到未知的文件夹。

我到目前为止的脚本是这样的…请不要笑,因为我是一个脚本新手…

 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) 

如果我从这个错误的angular度来看,请告诉我… …在这个问题上任何帮助Thankyou提前。 亚历克斯问候

阅读和parsing目录列表是一种方法(尽pipe在这种情况下,我会使用“dir / ad / b”来简化parsing),尽pipe这样做还是挺有意思的。 您也可以使用FileSystemObject枚举目录的子目录,这将是我的首选方法。

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

我很无聊,所以我很快就为你做了这个。 希望能帮助到你:

 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 
  • 只需将第6行中的“.example”更改为您要在文件夹名称中search的任何内容即可。

  • 将“%USERNAME%\ Documents \ test”更改为您要search的父文件夹

  • strDestinationFolder是包含结果的variables(您正在查找的文件夹名称)

您可以使用Windows shell命令“FOR”和“SET”将目录名称填充到环境variables中。 然后可以使用该variables根据需要复制文件。

假设我有以下“未知”目录:

 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> 

在命令行我可以这样做:

('dir / ad / b * example')中的/ F%i设置foo =%i

 I can now echo foo to prove it has been assigned the directory name: C:\test>echo %foo% 04pql7hw.example C:\test> 

现在你可以在你的休闲复制。

警告 – 如果你把这些命令放到一个shell脚本中,记得把%i的%符号加倍到%% i,如下所示:

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

那是你需要的吗?