Make batch work on files outside C.W.D
Posted: 07 Dec 2010 13:43
For a lab in school I was asked to create a batch file that uses shift to copy numerous files to a directory. If the directory does not exist make it, if one of the files needed to be copy is a directory copy it and all sub directories.
Here is the code,
rem The command uses the following syntax:
rem mycopy dir file1 file2 ...
if '%1'=='' goto help
if '%2'=='' goto help
if not '%1'=='/?' goto start
rem
rem If user does not enter 2 parameters or enters "/?", display help. Otherwise run program.
rem
:help
echo Enter 2 parameters.
goto end
:start
set todir=%1
if exist %todir%\nul goto getfile
echo Creating Directory %todir%
md %todir%
if not errorlevel 1 goto getfile
echo Error Creating Directory
goto end
rem Start
rem If destination does not exist, create it.
rem Otherwise display error message.
rem
:getfile
shift
if '%1'=='' goto endgood
if exist %1\nul goto copydirectory1
copy %1 %todir% > nul
goto check
rem getfile
rem If source file is a directory use xcopy
rem
:copydirectory1
if exist %todir%\%1 goto copydirectory2
md %todir%\%1
if not errorlevel 1 goto copydirectory2
echo Error creating directory %todir%\%1
goto end
rem copydirectory1
rem If source does not exist create it
rem
:copydirectory2
xcopy %1 %todir%\%1 /s /e >nul
:check
if not errorlevel 1 goto getfile
echo Error copying %1
goto end
goto getfile
:endgood
echo All done! =D
:end
That all works fine. But the last question I need help on.
"Note that the batch file only runs on files and folders from the c.w.d. -- modify the batch file to use the
XP extensions that allow stripping of full paths, etc."
Any help would be great.
Thanks!
Here is the code,
rem The command uses the following syntax:
rem mycopy dir file1 file2 ...
if '%1'=='' goto help
if '%2'=='' goto help
if not '%1'=='/?' goto start
rem
rem If user does not enter 2 parameters or enters "/?", display help. Otherwise run program.
rem
:help
echo Enter 2 parameters.
goto end
:start
set todir=%1
if exist %todir%\nul goto getfile
echo Creating Directory %todir%
md %todir%
if not errorlevel 1 goto getfile
echo Error Creating Directory
goto end
rem Start
rem If destination does not exist, create it.
rem Otherwise display error message.
rem
:getfile
shift
if '%1'=='' goto endgood
if exist %1\nul goto copydirectory1
copy %1 %todir% > nul
goto check
rem getfile
rem If source file is a directory use xcopy
rem
:copydirectory1
if exist %todir%\%1 goto copydirectory2
md %todir%\%1
if not errorlevel 1 goto copydirectory2
echo Error creating directory %todir%\%1
goto end
rem copydirectory1
rem If source does not exist create it
rem
:copydirectory2
xcopy %1 %todir%\%1 /s /e >nul
:check
if not errorlevel 1 goto getfile
echo Error copying %1
goto end
goto getfile
:endgood
echo All done! =D
:end
That all works fine. But the last question I need help on.
"Note that the batch file only runs on files and folders from the c.w.d. -- modify the batch file to use the
XP extensions that allow stripping of full paths, etc."
Any help would be great.
Thanks!