这是代码:
Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
(WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath)
If objFolder Is Nothing Then
Wscript.Quit
End If
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
wscript.Echo objPath
现在这个部分有效,现在我需要将“objPath”写入“file.bat”。 在批处理文件中,它只是“echo objPath>> hello.txt”,但这似乎不适用于vbs!
任何人都可以帮助我吗?
批处理文件调用VBS文件以提示用户选择他/她想要复制的目录,然后选择要复制到的目录。然后批处理文件会在每次计算机启动时自动将所选的第一个位置复制到第二个位置!就像一个备份系统!但仅限于一个特定的文件夹!谢谢你的帮助!
答案 0 :(得分:0)
这是我编写的一个浏览文件夹例程,可以执行您想要的操作。您可以按原样使用它,也可以仅作为代码的参考。如果选择了有效路径,则返回0;如果单击取消按钮,则返回1。
随后的所有内容都会进入批处理文件。
@Echo off
setlocal
Call :BrowseFolder "Choose Source folder" "C:\scripts\batch\" r
echo %r%
echo %errorlevel%
Call :BrowseFolder "Choose Destination folder" "C:\scripts\" r
echo %r%
echo %errorlevel%
cmd /k
Goto :EOF
:BrowseFolder
setlocal
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo set sh=wscript.CreateObject("Shell.Application")
>>%vbs% echo set f=sh.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "Dialog Cancelled"
>>%vbs% echo wscript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo p
for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do set result=%%a
if exist %vbs% del /f /q %vbs%
if "%result%" EQU "Dialog Cancelled" (set a=1) else set a=0
endlocal & set %3=%result% & exit /b %a%
版本1 - 写出.cmd文件并调用它来设置环境变量。
:BrowseFolder
set Result=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
>%vbs% echo set shell=WScript.CreateObject("Shell.Application")
>>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "set Result=Dialog Cancelled"
>>%vbs% echo WScript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo "set Result=" ^& p
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof