Page 1 of 1
batch file to search and copy particular file
Posted: 18 Jul 2013 10:47
by looksthatmatter
hi,
i have been trying to make a batch file which does the following:
1. search all drives for
*.jpg,ppt,pptx, pdf and doc and docx files.
2. it should
not search the folders like
"windows" and
"program files"3. it should
detect the usb drive and
copy the files in usb in a folder and hide it.
i would appreciate if anyone can share the batch file or the code for this in DOS.

Re: batch file to search and copy particular file
Posted: 18 Jul 2013 20:38
by foxidrive
This seems very much like a class assignment. What have you written so far?

Re: batch file to search and copy particular file
Posted: 19 Jul 2013 06:18
by looksthatmatter
@ foxdrive
thanks for the reply
well i have almost done it but i cant get it straight here is my code....
SET odrive=%odrive:~0,2%
set backupcmd=xcopy
mkdir \data >nul
attrib +h \data >nul
echo off
SET Var=0
ECHO %%~dpA | FIND "Temporary Internet Files">Nul
IF NOT "%ErrorLevel%"=="0" SET Var+=1
ECHO %%~dpA | FIND "WINDOWS">Nul
IF NOT "%ErrorLevel%"=="0" SET Var+=1
ECHO %%~dpA | FIND "Program Files">Nul
IF NOT "%ErrorLevel%"=="0" SET Var+=1
IF "%Var%"=="3" do (
%backupcmd% c:\*.pdf "%drive%\data" /s /C /I /Q /H /Y
)
attrib -s +h data\* >nul
@echo off
can u improve on it
Re: batch file to search and copy particular file
Posted: 19 Jul 2013 06:53
by foxidrive
No offense, but you haven't been paying attention where they teach this stuff.
Firstly, do you have to detect all local drives and only search the non-removable drives? Do you have to search DVD drives, or stop it from searching DVD drives?
Do you have to detect the USB drive - or is the batch file running from the USB drive?
Re: batch file to search and copy particular file
Posted: 19 Jul 2013 07:42
by looksthatmatter
hey bro..
yeah u r right...i am a slow learner
firstly i need it to detect all local drives and only search the non-removable drives only. excluding dvd drive and usb drives
secondly it should search for .pptx,.ppt,.docx,.doc,.pdf files but it should not waste time in searching folders like windows and program files
thirdly it should copy the above mentioned file types to the usb.
fourthly the batch file runs from usb
thanks for your help man, i have been able to search only c drive and copy all files with .ppt to usb but i and not able to exclude the search of folder program files and windows.
i hope now u can re write me a code...???

Re: batch file to search and copy particular file
Posted: 19 Jul 2013 08:54
by penpen
I have not much time today (sry), so here only a little bit code to get all connected volumes to search for.
Maybe wou want to remove some volumes, this can be done easiliy. See the example in the code:
Code: Select all
:main
setlocal
call :getConnectedVolumes connectedVolumes
echo connectedVolumes: %connectedVolumes%
echo example for removing a volume: here volume X
set "connectedVolumes=%connectedVolumes:X=%"
echo connectedVolumes: %connectedVolumes%
for %%a in (%connectedVolumes%) do (
echo do something with volume %%a
)
endlocal
goto:eof
:getConnectedVolumes
:: %1 variable to store the connected volumes name, if it exists, else undefines this variable
setlocal enableDelayedExpansion
set "volumes="
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
for /f "tokens=2* delims= " %%b in ('label %%a:') do (
if "%%b" == ":" set "volumes=!volumes! %%a"
)
) < nul 2< nul
endlocal & set "%1=%volumes:~1%"
exit /b 0
penpen
Edited: Corrected some careless mistakes in the code.
Re: batch file to search and copy particular file
Posted: 19 Jul 2013 09:10
by looksthatmatter
thanks penpen
this solves one of me required problem
can u help for the rest of the code...
Re: batch file to search and copy particular file
Posted: 19 Jul 2013 15:52
by Samir
There's a switch in xcopy that allows you to specify files or using wildcards, extensions to exclude. You can also use it to exclude paths.
My approach would be to detect all the drives I need to search, and run an xcopy with my exlusions file on each drive. Hide the directory and be done.
Re: batch file to search and copy particular file
Posted: 19 Jul 2013 18:09
by foxidrive
xcopy will work that way - but you would need to run the xcopy command once for every filetype, on every drive.
Robocopy can copy a set of filespecs in one pass and can also exclude certain folders, so that is the better tool to use here.
This batch file should do the search on all drives except the USB drive, copy the files and put them in a folder for each drive - to eliminate the possibility of filename clashes - and hide the root folder (untested):
Code: Select all
@echo off
for %%a in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
if /i not "%%a:"=="%~d0" robocopy %%a:\ *.pptx *.ppt *.docx *.doc *.pdf "%~d0\data\drive %%a" /xd "\windows" "\program files" /s
)
attrib +h "%~d0\data" /d
Determining the USB drive letter is simple by using %~d0 as the batch file is running from the USB drive, and using %~d0 can also be used for the target drive.
However determining only the non-removable drives needs WMIC or another tool. I'm not sure if the OP has been introduced to wmic yet.
Re: batch file to search and copy particular file
Posted: 20 Jul 2013 11:17
by penpen
The full code (Samir's algorithm), if robocopy could not be installed to the system you use:
Code: Select all
:main
@echo off
setlocal
:: detect usb stick on assumtion: this batch is started from it.
set "usbVolume=%~d0"
set "usbVolume=%usbVolume:~0,1%"
set "targetDirectory=\targetDirectory"
:: determine connected volumes, excluding usb volume and maybe accidently mounted volumes (CD drive with a CD in there)
call :getConnectedVolumes connectedVolumes "%usbVolume% A B"
:: exclude standard system directories: assumed file exclude.txt in the actual directory is not used, if it exists it will be deleted.
:: only most common system directories are excluded you may edit this
set "excludeFile=exclude.txt"
if exist %excludeFile% del %excludeFile%
(
if defined SystemRoot echo %SystemRoot%
if defined windir echo %windir%
if defined ProgramData echo %ProgramData%
if defined ProgramFiles echo %ProgramFiles%
if defined ProgramFiles^(x86^) echo %ProgramFiles^(x86^)%
if defined ProgramW6432 echo %ProgramW6432%
if exist "C:\Documents and Settings" echo C:\Documents and Settings
if exist "%SystemDrive%\Users" echo %SystemDrive%\Users
for %%a in (%connectedVolumes%) do (
if exist "%%a:\Recovery" echo %%a:\Recovery
if exist "%%a:\$Recycle.Bin" echo %%a:\$Recycle.Bin
)
) > %excludeFile%
:: copy the files
for %%a in (%connectedVolumes%) do (
%SystemRoot%\system32\xcopy.exe %%a:\*.jpg %usbVolume%:%targetDirectory% /EXCLUDE:%excludeFile% /S /V
%SystemRoot%\system32\xcopy.exe %%a:\*.ppt %usbVolume%:%targetDirectory% /EXCLUDE:%excludeFile% /S /V
%SystemRoot%\system32\xcopy.exe %%a:\*.pptx %usbVolume%:%targetDirectory% /EXCLUDE:%excludeFile% /S /V
%SystemRoot%\system32\xcopy.exe %%a:\*.pdf %usbVolume%:%targetDirectory% /EXCLUDE:%excludeFile% /S /V
%SystemRoot%\system32\xcopy.exe %%a:\*.doc %usbVolume%:%targetDirectory% /EXCLUDE:%excludeFile% /S /V
%SystemRoot%\system32\xcopy.exe %%a:\*.docx %usbVolume%:%targetDirectory% /EXCLUDE:%excludeFile% /S /V
)
:: delete exclude file
if exist %excludeFile% del %excludeFile%
endlocal
goto:eof
:getConnectedVolumes
:: version 2
:: %1 variable to store the connected volumes name, if it exists, else undefines this variable
:: %2 (optional) volume names to be excluded, encapsulated in a string.
setlocal enableDelayedExpansion
set "DIRCMD="
set "volumesToTest= A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
if not %2. == . for %%a in (%~2) do set "volumesToTest=!volumesToTest: %%a=!"
set "volumes="
for %%a in (%volumesToTest:~1%) do (
set /A "LINES=0"
for /f "tokens=* delims=" %%b in ('dir %%a:\') do (
set /A "LINES+=1"
)
if !LINES! GTR 1 set "volumes=!volumes! %%a"
) 2> nul
endlocal & set "%1=%volumes:~1%"
exit /b 0
Note:Redesigned the procedure :getConnectedVolumes because it may produce windows error message frames on network drives, old 3.5'' and 5,25'' disk drives using winxp home.
penpen
Edit: Corrected some errors: ^( ^) instead of (), added some missing colons, and repositioned endlocal
Re: batch file to search and copy particular file
Posted: 20 Jul 2013 12:36
by looksthatmatter
r u sure penpen??it doesnt work in win7
Re: batch file to search and copy particular file
Posted: 20 Jul 2013 16:43
by Samir
foxidrive wrote:xcopy will work that way - but you would need to run the xcopy command once for every filetype, on every drive.
Robocopy can copy a set of filespecs in one pass and can also exclude certain folders, so that is the better tool to use here.
Quite true, but a for loop with the filespecs would handle that. Although from an efficiency standpoint, I get what you're saying.
I haven't used robocopy (although I've heard about it). I found xxcopy before I heard of robocopy and have used that in a few instances for manually mirroring drives.
Re: batch file to search and copy particular file
Posted: 20 Jul 2013 16:48
by penpen
I had built in some errors, sry.
I have corrected the code above, hopefully it works now.
penpen
Re: batch file to search and copy particular file
Posted: 20 Jul 2013 16:58
by Samir
penpen wrote:The full code (Samir's algorithm)...
penpen you are AMAZING!! I could only dream about writing my algorithm in such eloquent code.
