admin privileges screwing up mkdir

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lawrenceejgray
Posts: 3
Joined: 29 Oct 2011 13:15

admin privileges screwing up mkdir

#1 Post by lawrenceejgray » 29 Oct 2011 13:45

hey all,

im very new to batch building, my batch file needs to create some directories and folders on a USB stick, but it must do it with admin rights for a program that i will be running further on in the code. when i run it without admin rights, it creates the dirs and file just fine. I then delete the newly created dirs and files and run the batch in admin mode and it comes up with errors saying that all the subdirectories and files already exist. what am i doing wrong?

Code: Select all

@ECHO OFF
GOTO init

REM *******************************
REM ********* initialise  *********
REM *******************************

:init

REM Setup time and date functions used for log file.
FOR %%A IN (%Date%) DO SET current_date=%%A
SET current_time=%Time%

REM Setup key directories.
SET evidence_results=Extractor_results
SET memory_results=%evidence_results%\memory
SET image_results=%evidence_results%\images
SET errors=%evidence_results%\errors
SET extractor_log=%evidence_results%\extractor_log

REM Create Directory structure to hold results, errors and logs.
MKDIR %evidence_results%
MKDIR %memory_results%
MKDIR %image_results%
MKDIR %errors%
MKDIR %extractor_log%

REM Create and append log file.
ECHO %date% %time% : log started > %evidence_results%\extractor.log


REM check whether user is running batch file as Administrator.
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"&&(
 ECHO admin...
)




....rest of code.


thanks for your help!
Loz.

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: admin privileges screwing up mkdir

#2 Post by aGerman » 29 Oct 2011 14:19

That's a well known problem. As soon as you run it as admin the working directory is c:\windows\system32 (I'm virtually certain your folders are created there).
To avoid that you could prepend the following line to your code:

Code: Select all

@cd /d "%~dp0"

Regards
aGerman

Post Reply