Batch file 批处理代码-文件夹对话框-代码偶尔工作

Batch file 批处理代码-文件夹对话框-代码偶尔工作,batch-file,command-line,folderbrowserdialog,Batch File,Command Line,Folderbrowserdialog,我有一个批处理文件,其中一个部分打开文件夹选择器对话框,供用户选择要复制的位置。起初,我认为问题在于我在桌面上编写了代码,然后他们将其移动到服务器上的最终位置。但现在我发现,无论它是否决定工作,似乎都是完全随机的。有些文件夹正常,有些不正常。很困惑 我看到的唯一错误是,它表示此时设置为意外。 另外,当我删除@echo off并将代码输出到txt文件时,代码中的字符串似乎在循环,并将它们存储为我的%thumbdrivefolder%变量 @echo off for %%I in (pow

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

我看到的唯一错误是,它表示此时设置为意外。 另外,当我删除@echo off并将代码输出到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%
if语句肯定容易受到空格的攻击,请尝试以下操作:

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


事实上,通过更多的测试,我发现它在大多数文件夹中都能正常工作,当我把它放到我们的工作文件夹中时除外。这些文件夹的路径是:K:\drawing\jobs\1DETAILING\FLOORTECH 2014年7月14日-可能是路径名弄乱了它,因为我尝试过的所有其他路径都没有空格和撇号。好吧,现在我很确定当路径包含撇号时是这样的:'-有人知道上面的代码可以在哪里编辑以允许包含撇号的路径吗?空格必须在批处理文件中处理-撇号不是有害字符。
IF NOT "%thumbdrivefolder:~-1%"=="\" SET "thumbdrivefolder=%thumbdrivefolder%\"
del "%temp%\fchooser.exe" 2>NUL

msg * "%thumbdrivefolder%"