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%