[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 【已解决】请问VBS如何获取正在使用的文件夹路径呢?---已解决,感谢版主和网友帮助

本帖最后由 wind5win 于 2023-4-16 22:38 编辑

请问如何获取正在会使用的文件夹路径呢,不是获取脚本所在文件夹路径,也不是获取特定文件所有的路径。
比如我打开了C:\Program Files,我通过右键运行放在D盘的脚本,获取到我正在用的文件夹地址“C:\Program Files“

不明白你的问题竟与格式时间扯上关系!

TOP

RE: 请问VBS如何获取正在使用的文件夹路径呢?---已解决,感谢网友和版主

回复 1# wind5win

TOP

回复 5# 77七
终于成功解决,感谢
  1. Dim fso,msg,tt,ws,d,str
  2. Set ws = WScript.CreateObject("WScript.shell")
  3. Set fso = CreateObject("Scripting.FileSystemObject")
  4. d = Format_Time(Now(),6)
  5. Function Format_Time(s_Time, n_Flag)
  6. Dim y, m, d, h, mi, s
  7. Format_Time = ""
  8. If IsDate(s_Time) = False Then Exit Function
  9. y = cstr(year(s_Time))
  10. m = cstr(month(s_Time))
  11. If len(m) = 1 Then m = "0" & m
  12. d = cstr(day(s_Time))
  13. If len(d) = 1 Then d = "0" & d
  14. h = cstr(hour(s_Time))
  15. If len(h) = 1 Then h = "0" & h
  16. mi = cstr(minute(s_Time))
  17. If len(mi) = 1 Then mi = "0" & mi
  18. s = cstr(second(s_Time))
  19. If len(s) = 1 Then s = "0" & s
  20. Select Case n_Flag
  21. Case 1
  22. ' yyyy-mm-dd hh:mm:ss
  23. Format_Time = y & "-" & m & "-" & d & " "& h &":" & mi &":" & s
  24. Case 2
  25. ' yyyy-mm-dd
  26. Format_Time = y & "-" & m & "-" & d
  27. Case 3
  28. ' hh:mm:ss
  29. Format_Time = h & ":" & mi & ":" & s
  30. Case 4
  31. ' yyyy年mm月dd日
  32. Format_Time = y & "年" & m & "月" & d & "日"
  33. Case 5
  34. ' yyyymmdd
  35. Format_Time = y & m & d
  36. Case 6
  37. ' yyyy-mm-dd-hh-mm-ss
  38. Format_Time = y & m & d & "-"& h & mi & s
  39. End Select
  40. End Function
  41. str = inputbox("输入文件夹名称")
  42. Set objShell = CreateObject("Shell.Application")
  43. Set objWindows = objShell.Windows
  44. strFolder = ""
  45. For i = objWindows.Count - 1 To 0 Step -1
  46.     ' Check if the window belongs to Windows Explorer
  47.     If InStr(1, objWindows.Item(i).FullName, "explorer.exe", vbTextCompare) > 0 Then
  48.         ' Get the location of the last opened window
  49.         strFolder = objWindows.Item(i).Document.Folder.Self.Path
  50. 'set fn = objWindows.Item(i).Document.Folder
  51. if ws.appactivate(objWindows.Item(i).Document.Folder) =true then
  52. Exit For
  53. end if
  54.     End If
  55. Next
  56. ' Output the directory path of the last opened window
  57. 'WScript.Echo strFolder
  58. m = "-" & str
  59. 'tt1 = fso.FolderExists(fldr1) '存在返回true;不存在返回false
  60. 'tt2 = fso.FolderExists(fldr2) '存在返回true;不存在返回false
  61. 'If str = false Then
  62. If left(strFolder,7)<> "\\share" Then
  63. Fldr1 = d & str
  64. Fldr2 = d & m
  65. If isempty(str) or str = "" Then
  66. fso.CreateFolder Fldr1
  67. Else
  68. fso.CreateFolder Fldr2
  69. 'ws.run Fldr2
  70. End If
  71. Else
  72. Fldr3 = strFolder & "\" & d & str
  73. Fldr4 = strFolder & "\" & d & m
  74. If isempty(str) or str = "" Then
  75. fso.CreateFolder Fldr3
  76. Else
  77. fso.CreateFolder Fldr4
  78. End If
  79. End If
  80. Set fso = nothing
  81. Set ws = nothing
复制代码

TOP

回复 2# Batcher


   谢谢站长分享!感觉以后可以多一个方式执行批处理了,写成批处理排除批处理脚本所在目录,只打开两个资源管理器窗口,可以直接cd 到待处理的目录工作了,很实用!
bat小白,请多指教!谢谢!

TOP

本帖最后由 77七 于 2023-4-14 23:36 编辑
  1. Set objShell = CreateObject("Shell.Application")
  2. Set objWindows = objShell.Windows
  3. For Each Window in objWindows
  4.     If InStr(1, Window.FullName, "explorer.exe", vbTextCompare) > 0 Then
  5.         Path = Window.Document.Folder.Self.Path
  6.         If Not Path = Replace(WScript.ScriptFullName, WScript.ScriptName, "") Then
  7.             WScript.Echo Path
  8.         End If
  9.     End If
  10. Next
  11. WScript.Echo "Press any key to continue..."
  12. WScript.StdIn.Read(1)
复制代码


我问了下ai,写了个类似的,让ai排除脚本所在目录,一直不成功
23:35编辑

我又问了一下ai
获取最后打开的windows资源管理器窗口目录
  1. Set objShell = CreateObject("Shell.Application")
  2. Set objWindows = objShell.Windows
  3. strFolder = ""
  4. For i = objWindows.Count - 1 To 0 Step -1
  5.     ' Check if the window belongs to Windows Explorer
  6.     If InStr(1, objWindows.Item(i).FullName, "explorer.exe", vbTextCompare) > 0 Then
  7.         ' Get the location of the last opened window
  8.         strFolder = objWindows.Item(i).Document.Folder.Self.Path
  9.         Exit For
  10.     End If
  11. Next
  12. ' Output the directory path of the last opened window
  13. WScript.Echo strFolder
复制代码
bat小白,请多指教!谢谢!

TOP

回复 2# Batcher
我填加右键调用bat,再使bat运行vbs,结果是在任意右键位置生成日期文件。现在有个问题是在网络共享盘里运行vbs创建文件夹失败,需要加共享网盘目录

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\datefolder]

"MUIVerb"="新建日期文件夹"
"Icon"="shell32.dll,20"
"Position"="top"

[HKEY_CLASSES_ROOT\Directory\Background\shell\datefolder\command]
@="C:\\portable\\adddatefolder.bat"
  1. @echo off
  2. cls
  3. set a=20
  4. set b=1
  5. :re
  6. set /a a+=2
  7. set /a b+=1
  8. mode con:cols=%a% lines=%b%
  9. if %a% lss 4 goto re
  10. ::set rr="HKCU\Console\%%SystemRoot%%_system32_cmd.exe"
  11. ::reg delete %rr% /f>nul
  12. ::reg add %rr% /v "WindowPosition" /t REG_DWORD /d 0x012c0384 /f>nul
  13. ::if “%1”==”h” goto begin
  14. ::start mshta vbscript:createobject(“wscript.shell”).run(“”“%~nx0”” h”,0)(window.close)&&exit
  15. ::begin
  16. call "C:\portable\adddatefolder.vbs">nul
复制代码
Dim fso,msg,tt,ws,d,str
Set ws = WScript.CreateObject("WScript.shell")
Set fso = CreateObject("Scripting.FileSystemObject")

d = Format_Time(Now(),6)

Function Format_Time(s_Time, n_Flag)
        Dim y, m, d, h, mi, s
        Format_Time = ""
        If IsDate(s_Time) = False Then Exit Function
        y = cstr(year(s_Time))
        m = cstr(month(s_Time))
        If len(m) = 1 Then m = "0" & m
        d = cstr(day(s_Time))
        If len(d) = 1 Then d = "0" & d
        h = cstr(hour(s_Time))
        If len(h) = 1 Then h = "0" & h
        mi = cstr(minute(s_Time))
        If len(mi) = 1 Then mi = "0" & mi
        s = cstr(second(s_Time))
        If len(s) = 1 Then s = "0" & s
        Select Case n_Flag
                Case 1
                        ' yyyy-mm-dd hh:mm:ss
                        Format_Time = y & "-" & m & "-" & d & " "& h &":" & mi &":" & s
                Case 2
                        ' yyyy-mm-dd
                        Format_Time = y & "-" & m & "-" & d
                Case 3
                        ' hh:mm:ss
                        Format_Time = h & ":" & mi & ":" & s
                Case 4
                        ' yyyy年mm月dd日
                        Format_Time = y & "年" & m & "月" & d & "日"
                Case 5                        
                        ' yyyymmdd
                        Format_Time = y & m & d
                Case 6
                        ' yyyy-mm-dd-hh-mm-ss
                        Format_Time = y & m & d & "-"& h & mi & s
        End Select
End Function

str = inputbox("输入文件夹名称")
i = "-" & str
Fldr1 = d & str
Fldr2 = d & i
'tt1 = fso.FolderExists(fldr1) '存在返回true;不存在返回false
'tt2 = fso.FolderExists(fldr2) '存在返回true;不存在返回false
'If str = false Then
If isempty(str) or str = "" Then
fso.CreateFolder Fldr1
Else
fso.CreateFolder Fldr2
'ws.run Fldr2
End If
Set fso = nothing
Set ws = nothing

TOP

回复 2# Batcher


  你好,请问vbs里怎么写呢,我需要提取的路径在vbs中引用

TOP

回复 1# wind5win


test-1.bat
  1. @echo off
  2. powershell "@((New-Object -com shell.application).Windows()).Document.Folder.Self.Path"
  3. pause
复制代码
1

评分人数

    • 77七: 感谢分享技术 + 1
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表