# batch file program to check directory existence



## qutesanju

Hi experts,
I have one folder as C:\input
and i need to copy all files from c:\inout to c:\output

Through simple copy command i can achieve this
COPY c:\input\*.* c:\output

but i need to check if c:\Test directory exist or not if NOT then it should create that directory OUTPUT and then copy from c:\input\ to c:\output


----------



## qutesanju

pls help


----------



## devil_himself

qutesanju , Welcome to TSG

Try this:



Code:


@echo off & setlocal enableextensions

:: usage details
if "%~2"=="" (
  echo Usage: %~0 [InputFolder] [OutputFolder]
  goto :EOF
  )

:: check for existence of [OutputFolder]
:: if [OutputFolder] doesn't exist, create it
if not exist "%~2\" (
  echo folder "%~2" not found
  echo creating folder "%~2"
  md "%~2"	
  )

:: copy all files from [InputFolder] to [OutputFolder]
copy "%~1" "%~2"

Usage: %~0 [InputFolder] [OutputFolder]

Ex: cp.cmd "c:\input" "d:\output"


----------



## qutesanju

i created a batch file with above code

but when i tried to execute below code...I got error

*Usage: %~0 [InputFolder] [OutputFolder]

Ex: cp.cmd "c:\input" "d:\output" *


----------



## qutesanju

I saved above code in movefiles.bat file
and then i tried to run it from command line as *movefiles.bat "c:\input" "d:\output"* it worked fine.........
*but *can i hard code the values *"c:\input" "d:\output" *
*in batch file?*

*as i dont need to execute it as movefiles.bat "c:\input" "d:\output"*


----------



## devil_himself

here ya go



Code:


@echo off & setlocal enableextensions

:: folder details
set inf="c:\input"
set outf="c:\output"

:: check for existence of [OutputFolder]
:: if [OutputFolder] doesn't exist, create it
if not exist "%outf%\" (
  echo folder "%outf%" not found
  echo creating folder "%outf%"
  md "%outf%"	
  )

:: copy all files from [InputFolder] to [OutputFolder]
copy "%inf%\" "%outf%\"


----------



## qutesanju

thanks devil_himself....excellent........it's working


----------

