0

I have a batch file with one part that opens a folder chooser dialog for the user to select a location to copy. At first i thought the problem was i wrote the code on my desktop, and them moved it to its final location on a server. But now im finding that it seems completely random whether it decides to work or not. Some folders are ok, some not. Quite confused!

The only error i see is that it says Set unexpected at this time. and also when i remove @echo off, and output code to txt file, seems it is looping through a bunch of strings inside the code, and storing them as my %thumbdrivefolder% variable...

    @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%
  • actually, running more testing, i found that it worked in most folders expect when i put it into our job folders. these folders have the path: "K:\drafting\jobs\1DETAILING\FLOORTECH JULY'14-ON" ---- could it be that the pathname is messing it up, as all the other paths i tried didnt have spaces and apostrophes. –  Sep 15 '14 at 08:48
  • ok now im quite sure its when the path contains an apostrophe: ' --does anyone know where the above code could be edited to allow for paths with apostrophes in them? –  Sep 15 '14 at 08:52
  • Spaces have to be handled in batch files - apostrophes are not a poison character. – foxidrive Sep 15 '14 at 08:53

1 Answers1

0

The if statement is certainly vulnerable to spaces, try the following:

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

and

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

msg * "%thumbdrivefolder%"
foxidrive
  • 40,353
  • 10
  • 53
  • 68