【问题标题】:Batch code - folder dialog - code works occasionally批处理代码 - 文件夹对话框 - 代码偶尔会起作用
【发布时间】:2014-09-15 08:45:40
【问题描述】:

我有一个批处理文件,其中一部分打开一个文件夹选择器对话框,供用户选择要复制的位置。起初我认为问题在于我在桌面上编写了代码,然后他们将其移至服务器上的最终位置。但现在我发现它是否决定工作似乎完全是随机的。有些文件夹可以,有些则不行。很困惑!

我看到的唯一错误是它说此时设置意外。 而且当我删除@echo,并将代码输出到txt文件时,似乎它正在循环代码中的一堆字符串,并将它们存储为我的 %thumbdrivefolder% 变量...

    @echo off

for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
    set chooser=powershell -sta "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.FolderBrowserDialog;$f.SelectedPath='%cd%';$f.Description='About to make a copy of the job folder: %jobfolder% (for transport). Please select a folder on an external/removable drive.';$f.ShowNewFolderButton=$true;$f.ShowDialog();$f.SelectedPath"
) else (
    set chooser=%temp%\fchooser.exe
    if exist !chooser! del !chooser!
    >"%temp%\c.cs" echo using System;using System.Windows.Forms;
    >>"%temp%\c.cs" echo class dummy{[STAThread]
    >>"%temp%\c.cs" echo public static void Main^(^){
    >>"%temp%\c.cs" echo FolderBrowserDialog f=new FolderBrowserDialog^(^);
    >>"%temp%\c.cs" echo f.SelectedPath=System.Environment.CurrentDirectory;
    >>"%temp%\c.cs" echo f.Description="Please choose a folder.";
    >>"%temp%\c.cs" echo f.ShowNewFolderButton=true;
    >>"%temp%\c.cs" echo if^(f.ShowDialog^(^)==DialogResult.OK^){Console.Write^(f.SelectedPath^);}}}
    for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
        if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
    )
    del "%temp%\c.cs"
    if not exist "!chooser!" (
        echo Error: Please install .NET 2.0 or newer, or install PowerShell.
        goto :ERROR
    )
)
for /f "delims=" %%I in ('%chooser%') do set "thumbdrivefolder=%%I"
IF NOT %thumbdrivefolder:~-1%==\ SET thumbdrivefolder=%thumbdrivefolder%\
del "%temp%\fchooser.exe" 2>NUL

msg * %thumbdrivefolder%

【问题讨论】:

  • 实际上,在运行更多测试时,我发现它在大多数文件夹中都能正常工作,当我将它放入我们的工作文件夹时。这些文件夹的路径是:“K:\drafting\jobs\1DETAILING\FLOORTECH JULY'14-ON”----可能是路径名搞砸了,因为我尝试的所有其他路径都没有空格和撇号.
  • 好的,现在我很确定路径何时包含撇号:' -- 有谁知道上面的代码可以在哪里编辑以允许其中包含撇号的路径?
  • 必须在批处理文件中处理空格 - 撇号不是有毒字符。

标签: batch-file command-line folderbrowserdialog


【解决方案1】:

if 语句肯定容易受到空格的影响,请尝试以下操作:

set "chooser=%temp%\fchooser.exe"
if exist "!chooser!" del "!chooser!"

IF NOT "%thumbdrivefolder:~-1%"=="\" SET "thumbdrivefolder=%thumbdrivefolder%\"
del "%temp%\fchooser.exe" 2>NUL

msg * "%thumbdrivefolder%"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    相关资源
    最近更新 更多