Link to home
Start Free TrialLog in
Avatar of Anuk
Anuk

asked on

Create temp directory

Create a teporary directory using a batch file
Avatar of webbster20
webbster20
Flag of United States of America image

mkdir c:\temp2
set temp=c:\temp2
Avatar of greg ward
or if the user is not admin
cd %userprofile%
mkdir temp2
temp2 will now be created here c:docs and setts/usersname/temp2
 
Greg

Avatar of Anuk
Anuk

ASKER

What if  C:\temp2  already exists?
don't run mkdir, mkdir was just for illustrative purposes, really.

just don't include mkdir C:\temp2.
Avatar of Anuk

ASKER

What if c:\temp2 exists but I don't know it exists?
Avatar of Anuk

ASKER

What I need to do is create a new temporary directory that has no files in it. I can't use a directory that already exists.
Ahh. Can we know the purpose of this? Is it going to be a Windows Temp directory? You could use a file name that is not common, say, Temp12345, but there is always a slim chance that a folder can already be in a directory.

I did temp2 because my work PC didn't have it and I wanted to watch it create it and change it to the temp dir.
Avatar of Anuk

ASKER

Iit is used on an installation of a program from an install program that is downloaded from a web site. It is installed on some unknown computer somewhere out there on the internet.

Does creating a directory that exists cause an error ... I can try it.
No, the directory will not be created because it already exists.
It will error you with:

C:\>mkdir c:\temp2

A subdirectory or file c:\temp2 already exists.
Avatar of Anuk

ASKER

I should be able to deal with that in the Batch file until I get a file name that works. Thanks
Try this for a dir based on the current time under the existing temp dir.

@echo off
REM Take the time and remove the : from it
set mytemp=%time::=%
REM Remove any spaces in the time leaving %temp%\anuk=hhmmss.hh
set mytemp=%temp%\anuk-%mytemp: =%
REM If it should exist then try again with a 1 on the end
if exist %mytemp%\nul set mytemp=%mytemp%1
mkdir %mytemp% 2>NUL
echo %mytemp%%
If you create based on time its going to be tricky later.
but so are all solutions where you dont know where your temp directory is.
you could do something inside C:\WINDOWS\Temp
why dont you want it to overwite a folder?
Greg
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops!! I meant (15 characters long would be):


Putting all this together, suppose you want to generate a 15-character wide temporary foldername of mixed case and digits 0~9. You would use the following code:


   setlocal enabledelayedexpansion

   set MinimumLength=15
   set MaximumLength=15

   set Integers=0123456789
   set LowerCase=abcdefghijklmnopqrstuvwxyz
   set UpperCase=ABCDEFGHIJKLMNOPQRSTUVWXYZ

   set RandomCharacters=%Integers%%Uppercase%%Lowercase%
   set /a TotalLength=(%random% %% (%MaximumLength% - %MinimumLength% +1)) + %MinimumLength%
   
   :: -----------------------------------------------------------------------
   :: Generate and create temporary folder
   :: -----------------------------------------------------------------------
   :loop
      set MyTemp=
     
      for /l %%a in (1,1,%TotalLength%) do (
         set /a ptr=!random! %% 61
         for %%b in (!ptr!) do (
            set MyTemp=!MyTemp!!RandomCharacters:~%%b,1!
         )
      )

      set MyTemp=%temp%\%MyTemp%
      mkdir %MyTemp%
   if errorlevel 1 goto Loop
   :: -----------------------------------------------------------------------

   ::
   :: The rest of your code.
   :: At this point, the new temporary folder has been created.
   :: You can reference it with the variable %MyTemp% or !MyTemp!
Actually, when attempting to create folders, as a precaution, where there might be spaces in the folder names, it is good practice to use double-quotes like this:

   MKDIR "%MyTemp%"
Oh, (I'm making a pigs ear of this)...

In the code above, please change the line (near the end):

      mkdir %MyTemp%

to:

      mkdir "%MyTemp%" 2>nul

Hi Anuk

You explained earlier:
"It is used on an installation of a program from an install program that is downloaded from a web site.  It is installed on some unknown computer somewhere out there on the internet."

Does this mean that YOU are creating a setup package that others can install from the Web?

If so, I think it's best if you use some standard methods to create your setup package, or you run the risk of:
1. Messing up the computer on which the program installs
2. Causing a failed installation when AntiVirus or Windows doesn't like the methods
3. Leaving behind all kinds of "temp" files on the computer it installed to.

Standard installer packages are designed to validate requirements, comply with accepted and safe setup routines, only install what they need to install dependent on the validation checks, create a method of uninstalling, and clean up again after they run.

If that is NOT what you meant, then just ignore this.

There is one method that you may not yet have considered, and that is the creation of a "Globally Unique Identifier" or GUID number to use for a folder name.  This can be achieved using a simple Visual Basic Script:

'CreateGuid.vbs
Dim objTL : Set objTL = CreateObject("Scriptlet.TypeLib")
Wscript.Echo objTL.Guid

When run using WScript.exe it will show a little message dialog with the GUID, and run using CScript.exe it displays the GUID in a "DOS" window.

If you are curious to know how "Unique" the generated number is, and what the chances of a randomly generated one being the same as another, then read these pages:

http://en.wikipedia.org/wiki/Globally_Unique_Identifier
http://en.wikipedia.org/wiki/UUID
http://blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspx

A batch file can be used to create and run a temporary *.vbs file, capture the GUID as a variable for re-use, and then delete the *.vbs file.  The following batch file demonstrates the theory.

Bill
@echo off
SetLocal EnableDelayedExpansion

set CurrDir=%~dp0
set CurrDir=%CurrDir:~0,-1%

REM Write a VBS file on the fly
echo Dim objTL : Set objTL = CreateObject("Scriptlet.TypeLib")>"%CurrDir%\CreateGUID.vbs"
echo Wscript.Echo objTL.Guid>>"%CurrDir%\CreateGUID.vbs"

REM GUID format is {F931AF3A-1EF4-45FB-A616-A04BF02634D7}

REM Run the Script and store results in variable
for /f %%a in ('call CScript //NoLogo "%CurrDir%\CreateGUID.vbs"') do (
    set GUID=%%a
    set GUID=!GUID:~1,-1!
)

REM Optional part for verifying only.
REM Create a log of numbers to demonstrate uniqueness
echo !GUID!>>"%CurrDir%\GUID_Log.txt"

echo The GUID generated is: !GUID!
echo.
echo This can be used as the name of a new folder.
echo.

pause
REM Remove temporary VBS file
del "%CurrDir%\CreateGUID.vbs" > nul

Open in new window

I guess we'll never know any more about what was needed specifically.
Steve
Avatar of Anuk

ASKER

I am using Winzip self extractor to install the files. It runs a batch file that starts an excel file. I have a VBA program in excel that sets the references and copies the files to the default directory (or a directory the user selects). However when I run the install it cannot find the files to copy them. The winzip program deletes them before they can be copied.

Winzip creates a temp folder where it puts all the files to be installed. Then it runs my batch program which loads my excel program which then sets the references and cpies the files to a directory that the user designates. Winzip then is supposed to delete the files and its temorary directory.

But my excel program can't find the files to copy.

So if I create my own temp directory, copy the files to it, then run my excel program, and after my files are copied from my temp directory ... then I can delete them.

Winzip has a way to delay the erasing of he files in its temp directory ... but I haven't been able to make the delay work.
Hi Anuk

That all seems like a roundabout way to do things.

You say:
"I have a VBA program in excel that sets the references..."

What do you mean by "sets the references"?
When referring to a "VBA program" are you referring to a Macro in the Excel workbook?

I have to question whether you actually need an "Excel Program" to copy files.

I suppose it depends what the files are and where they are from.  If your "Excel Program" is a VBA Macro in the Excel Workbook unpacked (*.xls, *.xlt, *.xlsm, or whatever), and shows a a "File > Save As" dialog to the user, then that all seems a bit unnecessary.

You could instead have separate Excel files (workbooks) unpacked by the self-extractor, to the default or named Temp folder you set when creating the self-extractor, and then run a proper setup program that allows the user to choose the file copy destination.  That's all your self-extractor is doing?  Just copying Excel files to the user's choice of folder?

There are a few different ways that you could show a dialog to a user to choose a destination directory, and I'm sure it wouldn't involve using a full-blown programming interface like Visual Studio to do.

Perhaps if you explain what you mean by "sets the references" then we can suggest what I'm sure will be a more reliable and better method.

Bill
Even having two WinZip Self extracting *.exe files would be better than using batch files.  One with the files inside it that is set to prompt for an "Extract To" folder, and have that packed inside your main one that is set to run the internal one after unpacking.

Have you considered using the Windows "IExpress Wizard" to create a self-extracting package?
On a Windows XP System - %SystemRoot%\system32\iexpress.exe
Just run it, choose the options yu want, the files to pack into it, and make sure you set it to prompt the user for the destination to extract the contents, along with optional messages.

I have attached a 66KB example of an IExpress package containing 3 bare *.xls files.  It is set to prompt you for an extract path and has the expected "Browse" button.  The mssages are just generic.

Save and rename, removing the *.TXT extension from the file name, and run it.
IExpress-Test-EXE.txt
Avatar of Anuk

ASKER

BillDL
The Windows Express looks good. I will try it. Thanks for the tip.