【问题标题】:VB execute .bat inside subfolder to rename files [closed]VB在子文件夹中执行.bat来重命名文件[关闭]
【发布时间】:2016-08-27 12:45:13
【问题描述】:

我想在VB中执行一个子文件夹中的.bat来重命名一些文件。文件夹是dinamyc,子文件夹的名称是静态的。

     path = System.IO.Path.GetDirectoryName( _
     System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
     Dim psi As New ProcessStartInfo(path & "\subfolder\kappa.bat")

这会在我 start.exe 时产生一个错误*,而且这不会做任何事情

 Shell(CreateObject("Wscript.Shell").CurrentDirectory & "\subfolder\" & "kappa.bat", AppWinStyle.Hide)

path 变量在我尝试时显示超出我的需要

MessageBox.Show(path)

http://image.prntscr.com/image/e0398d6e69ff48ce80fd826cced03612.png

所以

path

不是我需要的,但我没有找到与我的问题相关的任何内容

编辑:也试过这个

 Dim psi As New ProcessStartInfo(path & "\subfolder\kappa.bat")
        psi.RedirectStandardError = True
        psi.RedirectStandardOutput = True
        psi.CreateNoWindow = False
        psi.WindowStyle = ProcessWindowStyle.Hidden
        psi.UseShellExecute = False
         Process.Start(psi)

但是path & "\subfolder\kappa.bat会在执行中生成偏移量并出错

The `GetExecutingAssembly` error is :

System.IO.FileNotFoundException: File not found.
   at Microsoft.VisualBasic.Interaction.Shell(String PathName, AppWinStyle Style, Boolean Wait, Int32 Timeout)
   at Patcher.Form1.BTN_Click(Object sender, EventArgs e) in D:\Patcher\Form1.vb:line 58
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1080.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Patcher
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.0.0
    CodeBase: file:///D:/Patcher.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 14.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1081.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1078.0 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1064.2 built by: NETFXREL3STAGE
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 4.0.0.0
    Win32 Version: 4.6.1038.0 built by: NETFXREL2
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

【问题讨论】:

  • 您的问题缺少很多信息,在目前的状态下,这使得它非常模糊。如果您没有给我们足够的信息,您不能指望我们提供帮助,例如:this will generate an error - 这是什么错误,它说什么? -- the path varible show more than I need - 那么它显示了什么以及您希望它显示什么?
  • @VisualVincent more than I need 意味着这将是一个像 file\c:\.. 这样的字符串,所以不是 C:\\ 并且错误是当我执行程序并告诉我一个偏移量跨度>
  • 您可以直接从您的程序中重命名文件,而不是使用 .bat 文件。请参阅File.Move Method。这将更有效,可能更容易编程,并消除对外部文件的依赖。
  • @AndrewMorton 我需要多次执行此操作,我不能这么快地重命名 20k 文件,我想用 vba 重命名它们,但看起来很复杂,用 .bat 更快我只需要执行它以某种方式
  • @AndrewMorton 对不起先生,我删除了它,并添加了我使用的所有代码

标签: vb.net shell batch-file directory


【解决方案1】:

您的问题出在System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase 行。该方法返回以file:\ 开头的路径,当您从路径中删除文件名时,它不再是您尝试访问的文件,这就是它不起作用的原因。

有几种方法可以获取应用程序的文件夹。两个例子是Application.StartupPathSystem.AppDomain.CurrentDomain.BaseDirectory。我个人最喜欢后者,不要问我为什么。

因此,要解决您的问题,您只需要执行以下操作:

path = System.AppDomain.CurrentDomain.BaseDirectory

【讨论】:

  • 首先它可以工作,但是如果我把它放在一个没有 .bat 的文件夹中会产生那个错误(这不是问题),最大的问题是它执行主文件夹中子文件夹中的.bat,它不会在其位置执行.bat。另外 psi.CreateNoWindow = False psi.WindowStyle = ProcessWindowStyle.Hidden 不起作用,因为我可以看到弹出窗口。所以它执行 .bat 但在错误的位置。
  • 我添加了 Directory.SetCurrentDirectory 现在它在正确的位置执行但我可以看到窗口
  • @CiolteAlin :哈哈,反复试验是最好的学习方法!不客气,祝你未来好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2014-05-11
  • 1970-01-01
相关资源
最近更新 更多