batch file to check existance of directory if not exists then use alternate directory for file copy

SteveAnselment picture SteveAnselment · Jul 11, 2013 · Viewed 25k times · Source

I am trying to write a batch file that copies an exe file from a network location to a local location. It currently works but depending on windows version (xp or win7) the user has to select the correct .bat file due to different local paths needed for the copy. (they are going to the startup folder to be ran every time user starts machine). This is the first time i've ever worked with writing batch files and am completely lost when looking at the syntax for if statements. If i could get some help figuring this out it would be great.

Here is what I currently have that works for XP:

    REM @ECHO OFF

    ECHO STARTING MOVEFILES

    SET EXITRC=0
    SET EXITMSG=EXITRC INITIALIZED
    ECHO %EXITRC% -- %EXITMS

    COPY "\\networkDrive\install\Individual\program\MOVEFILES.EXE" "C:\DOCUMENTS AND SETTINGS\ALL USERS\START MENU\PROGRAMS\STARTUP\"

    ECHO COPIED FILES TO YOUR PC  
    SET EXITRC=%ERRORLEVEL%
    IF NOT %EXITRC% == 0 GOTO :EXIT

    SET EXITMSG=PROCESSING COMPLETE

    :EXIT 
    ECHO STEP: %EXITMSG%  RC: %EXITRC%
    ECHO FINISHING MOVEFILES 
    PAUSE

    EXIT %EXITRC%

Here is what I have for Windows 7:

    @ECHO OFF

    ECHO STARTING MOVEFILESWIN7

    SET EXITRC=0
    SET EXITMSG=EXITRC INITIALIZED
    ECHO %EXITRC% -- %EXITMS

    COPY "\\networkDrive\install\Individual\program\MOVEFILES.exe" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"

    ECHO COPIED MOVEFILESWIN7 TO YOUR PC - All Users / Public Startup folder
    SET EXITRC=%ERRORLEVEL%
    IF NOT %EXITRC% == 0 GOTO :EXIT

    SET EXITMSG=PROCESSING COMPLETE

    :EXIT 
    ECHO STEP: %EXITMSG%  RC: %EXITRC%
    ECHO FINISHING MOVEFILESWIN7 

    PAUSE
    EXIT %EXITRC%

I would like to have only one batch file that will cover both scenarios so there is no confusion to the user on which batch file to run.

Answer

paddy picture paddy · Jul 11, 2013

You can utilise the environment variable %ALLUSERSPROFILE%.

On WinXP the default is C:\Documents and Settings\All Users

On Win7/2008 the default is C:\ProgramData

There is a table available here: http://ss64.com/nt/syntax-variables.html

I see you also copy a different file. Not sure why you do that. Maybe you could detect using a method here: https://stackoverflow.com/a/2788764/1553090 -- Otherwise perhaps you should take advantage of the %ProgramFiles% and %ProgramFiles(x86)% variables.

Just to elaborate on how you might choose to use these variables... You can test the Win7 startup folder for existence, and if it's not there just fallback to the XP (regardless of whether it exists).

set STARTUP_WIN7=%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Startup
set STARTUP_WINXP=%ALLUSERSPROFILE%\Start Menu\Programs\Startup

if EXIST "%STARTUP_WIN7%" (
    set STARTUP=%STARTUP_WIN7%
) else (
    set STARTUP=%STARTUP_WINXP%
)