batch file to search and copy particular file please help
Posted: 15 Nov 2019 12:28
hi,
i have been trying to make a batch file which does the following:
1. search all usb drives for *.jpg,ppt,pptx, pdf and doc and docx files.
2. it should detect the usb drive and copy the files in hard disk in a folder and hide it.
i would appreciate if anyone can share the batch file or the code for this in DOS.
i have this script to copy from hard disk to usb but i can't make it to copy usb to hard any help please !!
i have been trying to make a batch file which does the following:
1. search all usb drives for *.jpg,ppt,pptx, pdf and doc and docx files.
2. it should detect the usb drive and copy the files in hard disk in a folder and hide it.
i would appreciate if anyone can share the batch file or the code for this in DOS.
i have this script to copy from hard disk to usb but i can't make it to copy usb to hard any help please !!
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