search and copy by file type between drives

Microsoft Windows vista ultimate 64-bit
January 29, 2010 at 21:48:30
Specs: Windows Vista
i am new to batch writing and needed a little help to get started.

i need to make a batch file can be run from a usb stick that searches for all files with a certain extension (*.xls) and copies them from the computer back to the usb stick.

i have been searching and found the following but it didn't work for me:

C: (to change to the computer's C drive for searching)
copy /s *.xls %systemdrive%

i used %systemdrive% since every time you put a usb stick into a different computer it might assign a different drive letter.
i was also planning on using bat to exe and make the file autorun when i insert the usb to the computer to avoid any problems where the OS is blocking scripts from running.

it seemed like a simple enough task but i'm having some trouble getting going. any help would be much appreciated, thanks!


See More: search and copy by file type between drives

Report •

#1
January 30, 2010 at 09:56:41
You can use the following to complete the job. I am not sure
about that Autorun scheme of yours, but I'm pretty sure I can
help you copy those file to the USB drive.

Code:
@echo off
echo Please input folder path (DO NOT QUOTE PATH)
echo Wrong: "C:\DIR\FILES\"
echo Correct: C:\DIR\FILES
set /p source=Directory:
echo Now, retrieving USB drive label...
for /f "tokens=1* delims=:" %%a IN "%CD%" DO set
label=%%a
echo USB Label : %label%
echo.
echo Copying files to %label%:\xls files
set dest="%label%:\xls_files"
mkdir "%label%:\xls_files"
xcopy "%source%\*.xls" %dest%
if /i not exist "%label%:\xls_files\*.*" goto error
echo.
echo Files successfully copied!
pause>nul
exit

:error
echo.
echo Error: files not copied!
pause>nul
exit

There! That should help you out.

XD-51


Report •

#2
January 30, 2010 at 12:11:53
thanks so much for the response! i just wanted to make sure i understood it correctly...

echo Please input folder path (DO NOT QUOTE PATH)
echo Wrong: "C:\DIR\FILES\"
echo Correct: C:\DIR\FILES

is this asking me to input the folder i wish to search? so if i wanted to search the entire C: drive i would just type "C:" (just not in quotes) when it prompts me?

echo Copying files to %label%:\xls files
set dest="%label%:\xls_files"
mkdir "%label%:\xls_files"

this is creating a new directory (named xls) in which to copy all of the files to, correct?

again, thanks so much for this code, it is greatly appreciated.


Report •

#3
February 1, 2010 at 07:41:13
Just don't make it too difficult, ON THE USB STICK, make this script:

@echo off
xcopy C:\*.xls . /S /D


--------------
The dot represents the current drive (and path). You can also do this :

@echo off
mkdir \mybackupfolder
xcopy C:\*.xls \mybackupfolder /S /D
--------------

If you omit a drive in a command like XCOPY, he assumes the CURRENT drive. You would need XCOPY /S to go though subdirectories, just assuming this is what you want.

As a genenal programming rule, avoid using commands that navigate through drives or directories. The navigation commands may always fail, but subsequent command will continue, causing unwanted behaviour.


Report •
Related Solutions


Ask Question