Oh, just noticed you ARE already familiar with redirection!!! Sorry for the previous whaffle.
Main Topics
Browse All TopicsHow can I modify the code\script below to:
-exclude specific file types (eg. mp3, m4a, mpg, etc)
-hide\mask the error message when it searches for the SD\USB drive name on each pass
-run minimized (optional)
Goal: We have laptops with SD drives and I have added a 32GB SD to each PC. I want to run an automated backup controlled by a batch file (see code below) and use Windows Task Scheduler to kick off the backup at a scheduled time daily. Yeah poor man way but boss will not offer any assistance so I have to work with a homebrew :-) My users need a backup safety net...
---------
I have a DOS code below created by one of the great users here. It works great but I need to take it up a notch.
-The script will search for a USB (SD soon) drive name 'IMG DRV' or whatever I call it
-Currently it is set to search drive letters (C,D,E,F,G,I,J). I usually do not see letters higher than J. Beforehand it was set all the way to 'Z' but it hangs on network drives until it times out.
-I get an error (nothing major but will alarm users) when running the drive look-up cycle until it finds the correct drive letter. I added the 'cls' between the script below to try clearing it on each drive letter look-up pass. Beforehand it listed a drive look-up error for each following letter on a new line until the drive was finally found. I WOULD LIKE to hide/mask that if possible until the letter is found.
(extracted from the code below)
"%%y"=="%testfor%" set drive=%test%:
cls <--- I added this was not there before......
goto end
-Last part of the code will copy the needed data to a USB drive or in my case will be the attached SD drive on the laptop. I WILL ADD more lines of data in the script to backup but I want to EXCLUDE certain file types like (music, movies, videos, etc). How can I do that?
Please help me modify or rewrite this code if needed for my two (three) main questions above. My DOS knowledge is beginner.
Thx
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Please try this.... remove the added ECHO and REM statements to perform the XCOPYs etc...
@echo off
REM ---------
REM BACKUP
REM ---------
ECHO --------------------------
ECHO CLOSE ALL PROGRAMS and APPLICATIONS NOW!!
ECHO --------------------------
ECHO.
ECHO Your hard drive is about to be searched for Documents,
ECHO Databases, Spreadsheets, and Email storage files.
ECHO.
ECHO All files of these types will be saved to
ECHO your REMOVABLE DEVICE in their original
ECHO directories\folders.
ECHO.
ECHO This will replace any previous backup on the
ECHO removable drive.
ECHO.
rem PAUSE
REM --------------------------
REM Finding Removable Drive Letter Code...
REM --------------------------
REM The string below will be for the DRIVE Name eg. set testfor=ABS
set testfor=IMG_DRV
set drive=unknown
echo Searching for removable drive...
for %%a in (C,D,E,F,G,I,J) do (
call :testdrive %%a
if not defined drive (
echo Cannot find USB drive. Backup aborted.
exit /b 1
)
)
rem cls
echo.
echo USB drive was found...
echo.
echo The drive name is. %testfor%
echo Drive letter is. %drive%
echo.
echo.
echo xcopy c:\source\*.* %drive%\backup /e /i /y
rem pause
rem cls
REM --------------------------
REM Starting Backup Routine...
REM --------------------------
ECHO Copying "My Documents" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "c:\Documents and Settings\%username%\My Documents\*.*" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "Desktop" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "c:\Documents and Settings\%username%\Deskto
ECHO.
ECHO.
ECHO Copying "Favorites" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "c:\Documents and Settings\%username%\Favori
ECHO.
ECHO.
ECHO Copying "Email Storage" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook\*.p
ECHO.
ECHO.
ECHO Copying "Email Nickname File" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "C:\Documents and Settings\%username%\Applic
ECHO.
ECHO.
ECHO Copying "AXSOne" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "C:\AXSOne\*.*" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "Siebel-SROC Database" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "C:\Program Files\Siebel\7.7\web client\LOCAL\*.*" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "I-ROAM Database" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "C:\Program Files\NikeGolf I-ROAM\*.sdf" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "Wallpaper" Folder to Backup device...
ECHO.
ECHO.
echo xcopy "C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\*.bmp" "%drive%\_Rep_Backup\%user
rem dir %drive%\_Rep_Backup\%usern
rem cls
ECHO.
ECHO Your BACKUP has been completed!!!.
ECHO It is located on your removable device
ECHO in a folder called: _Rep_Backup
ECHO.
ECHO You may disconnect your device from the PC!
ECHO.
ECHO Please continue to periodically backup...
ECHO.
ECHO.
ECHO Systems Tech Support
ECHO.
rem PAUSE
exit /b
rem ==========================
rem TESTDRIVE
rem ==========================
:testdrive
set test=%1
REM Use Dir to get drive label. Sets drive variable
for /f "tokens=5,*" %%x in ('dir %test%:\*.* 2^>NUL ^| find /i "volume in drive"') do (
if /i "%%y"=="%testfor%" set drive=%test%:
)
exit /b
Sorry... Replace:
REM The string below will be for the DRIVE Name eg. set testfor=ABS
set testfor=IMG_DRV
set drive=unknown
with the following:
REM The string below will be for the DRIVE Name eg. set testfor=ABS
set testfor=IMG_DRV
set drive=
This will ensure the batch file aborts if it cannot find the drive.
t0t0
I am not familiar with DOS and the code was written by someone else for me I posted it here to modify it a bit.
1. Thx the tutorial I am read word for word. I will test all the examples shortly
2. Help me out a bit more since I am green. Can you copy part of the code and insert you you recommend in there for me. I am not sure where to add it. I assume between lines 34-41.
3. Any idea on how I can exclude file types in this code for the xcopy process
THX!!!
Create a file "ExcludeExt.txt" with the extensions you want to exclude 1 on each line in the same folder as your batch file.
.mp3
.avi
.mpg
The exclude will exclude if the match is found in the entire path so
C:\Music files all .mp3\AlbumListing.doc
Will be excluded because the folder name has .mp3
Your script doesn't have a label END so I changed it to :EOF.
I added code to remove network drives found with "net use". You might still have a problem with CDROM drives not accessible.
I would probably think changing the script to use WMIC and querying removeable drives would be better.
AmazingTech
Hello.... Just gonna look at your code in a mo to see if I've missed something myself....
parcou
In the meantime, here's your code (edited). It should work to the specs you've given....
@echo off
REM ---------
REM BACKUP
REM ---------
ECHO --------------------------
ECHO CLOSE ALL PROGRAMS and APPLICATIONS NOW!!
ECHO --------------------------
ECHO.
ECHO Your hard drive is about to be searched for Documents,
ECHO Databases, Spreadsheets, and Email storage files.
ECHO.
ECHO All files of these types will be saved to
ECHO your REMOVABLE DEVICE in their original
ECHO directories\folders.
ECHO.
ECHO This will replace any previous backup on the
ECHO removable drive.
ECHO.
PAUSE
REM --------------------------
REM Finding Removable Drive Letter Code...
REM --------------------------
REM The string below will be for the DRIVE Name eg. set testfor=ABS
set testfor=IMG_DRV
set drive=
echo Searching for removable drive...
for %%a in (C,D,E,F,G,I,J) do (
call :testdrive %%a
if not defined drive (
echo Cannot find USB drive. Backup aborted.
exit /b 1
)
)
cls
echo.
echo USB drive was found...
echo.
echo The drive name is. %testfor%
echo Drive letter is. %drive%
echo.
echo.
rem copy c:\source\*.* %drive%\backup /e /i /y
pause
cls
REM --------------------------
REM Starting Backup Routine...
REM --------------------------
ECHO Copying "My Documents" Folder to Backup device...
ECHO.
ECHO.
xcopy "c:\Documents and Settings\%username%\My Documents\*.*" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "Desktop" Folder to Backup device...
ECHO.
ECHO.
xcopy "c:\Documents and Settings\%username%\Deskto
ECHO.
ECHO.
ECHO Copying "Favorites" Folder to Backup device...
ECHO.
ECHO.
xcopy "c:\Documents and Settings\%username%\Favori
ECHO.
ECHO.
ECHO Copying "Email Storage" Folder to Backup device...
ECHO.
ECHO.
xcopy "C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook\*.p
ECHO.
ECHO.
ECHO Copying "Email Nickname File" Folder to Backup device...
ECHO.
ECHO.
xcopy "C:\Documents and Settings\%username%\Applic
ECHO.
ECHO.
ECHO Copying "AXSOne" Folder to Backup device...
ECHO.
ECHO.
xcopy "C:\AXSOne\*.*" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "Siebel-SROC Database" Folder to Backup device...
ECHO.
ECHO.
xcopy "C:\Program Files\Siebel\7.7\web client\LOCAL\*.*" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "I-ROAM Database" Folder to Backup device...
ECHO.
ECHO.
xcopy "C:\Program Files\NikeGolf I-ROAM\*.sdf" "%drive%\_Rep_Backup\%user
ECHO.
ECHO.
ECHO Copying "Wallpaper" Folder to Backup device...
ECHO.
ECHO.
xcopy "C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\*.bmp" "%drive%\_Rep_Backup\%user
dir %drive%\_Rep_Backup\%usern
cls
ECHO.
ECHO Your BACKUP has been completed!!!.
ECHO It is located on your removable device
ECHO in a folder called: _Rep_Backup
ECHO.
ECHO You may disconnect your device from the PC!
ECHO.
ECHO Please continue to periodically backup...
ECHO.
ECHO.
ECHO Systems Tech Support
ECHO.
PAUSE
exit /b
rem ==========================
rem TESTDRIVE
rem ==========================
:testdrive
set test=%1
REM Use Dir to get drive label. Sets drive variable
for /f "tokens=5,*" %%x in ('dir %test%:\*.* 2^>NUL ^| find /i "volume in drive"') do (
if /i "%%y"=="%testfor%" set drive=%test%:
)
exit /b
t0t0
Sorry for the delay suffered a family loss so I was out a few days...back to business...
I tested (coped) the code as is but I cannot get it to complete. I run the bat file and as soon as I see the:
Searching for removable drive...
the command window closes...what am I missing? I have the USB drive attached by the correct volume name but I see nothing else in the code because the window closes after the echo string above
AmazingTech
Your code is working perfectly. I have not tested the exclude code yet will do so in the morning... One question. I ran the script with the USB drive removed although it said it was not found it will run the code xcopy the files but with no backup drv attached it goes no where:
How could you add a simple code IF drv by volume name is not found it goes to :end where I can echo a message 'no drive found terminating backup"....?
Thx
AmazingTech...
I did figure out that the /EXCLUDE:"%~dp0\ExcludeExt
cannot read: "e:\\excludeExt.txt"
by putting a pause in the code. I changed it to: /EXCLUDE:"%~dp0ExcludeExt.
cannot read: "e:\excludeExt.txt"
Although the file is there I still cannot get the script to read it....
AmazingTech
Got the exclude to work found by just putting in a direct path to the c: drive over the %~dp0
I am almost there:
Can you help me with the other question above:
Your code is working perfectly. I have not tested the exclude code yet will do so in the morning... One question. I ran the script with the USB drive removed although it said it was not found it will run the code xcopy the files but with no backup drv attached it goes no where:
How could you add a simple code IF drv by volume name is not found it goes to :end where I can echo a message 'no drive found terminating backup"....?
Thx
Business Accounts
Answer for Membership
by: t0t0Posted on 2009-04-21 at 06:53:01ID: 24194312
why not use redirection?
You can redirect standard output and AND standard errors
if you append '>NUL' to the end of a line, output is redirected (in this case) to the NUL device - ie, it just doesn't go anywhere and therefore won't be displayed. Try this:
ECHO Hello World>NULL
It just disappears. You can also redirect output to a file such as:
ECHO Hello World>c:\file.txt
Again, nothing appears on the screen however, this time, a file is created (C:\FILE.TXT) and if you examine it's contents using TYPE FILE.TXT then you will see the text "Hello World".
Now, if you run a batch file with ECHO ON rather than ECHO OFF, then you'll see DOS expand the command line from:
ECHO Hello World>NUL
to:
ECHO Hello World 1>NUL
Interesting!!!! You can actually just add the line "ECHO Hello World 1>NUL" yourself and DOS won't have to do the conversion for you.
Anyway, the '1>NUL' instructs DOS to output all standard output to the NUL device (or in the case of 1>C:\FILE.TXT, to a text file named FILE.TXT in the root folder of drive C:).
Now then... Just as you can redirect standard output, you can ALSO redirect standard error messages as in:
ECHO Hello World 2>NUL
Hmmm..... What's going on here then. Here, the text "Hello World" is displayed on the screen as usual and because there are no errors, nothing is redirected however, if we did something like:
TYPE X:\file.txt
If the drive X: does not exist, you're going to get an error on the screen hoever, if you redirect all errormessages as in:
TYPE X:\file.txt 2>NUL
The this time, if the drive (or the file) do not exist then the error message will just get sent to NUL (nowhere) - which is great because it doesn't mess up your screen.
Finally, how about this:
TYPE c:\file.txt 1>c:\file2.txt 2>NUL
This says, write the contents of the file C:\FILE.TXT to the file C:\FILE2.TXT (rather than the screen) AND write any errors to the NUL device (should there be any!).
You could have also done:
TYPE c:\file.txt 1>"c:\output file.txt" 2>"error file.txt"
Here's a variation (Try it)....
>>file.txt ECHO Hello World
>>file.txt ECHO Hello Again....
Google redirection, it's fascinating and it has many powerful and everyday uses.
Final word:
ECHO Hello World 1>file.txt
the '>' will CREAT the file FILE.TXT whereas using '>>' will APPEND to the file as in:
ECHO Hello World 1>>file.txt
Try it. Experiment. Have fun.