batch file to search and copy particular file please help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sam201
Posts: 5
Joined: 15 Nov 2019 12:22

batch file to search and copy particular file please help

#1 Post by sam201 » 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. :D :?: :?:

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

aGerman
Expert
Posts: 3775
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch file to search and copy particular file please help

#2 Post by aGerman » 15 Nov 2019 13:12

So, my understanding is that you already have the code to copy the files you're looking for. The missing thing is how to find the USB drives.
Give this a go:

Code: Select all

@echo off &setlocal

for /f tokens^=2^ delims^=^" %%i in (
  'WMIC Path Win32_DiskDrive Where "InterfaceType='USB'" Assoc /assocclass:Win32_DiskDriveToDiskPartition 2^>nul ^|findstr /c:"Disk #"'
) do for /f tokens^=4^ delims^=^" %%j in ('WMIC Path Win32_LogicalDiskToPartition 2^>nul ^|findstr /c:"%%i"') do echo %%j

pause
Steffen

sam201
Posts: 5
Joined: 15 Nov 2019 12:22

Re: batch file to search and copy particular file please help

#3 Post by sam201 » 15 Nov 2019 13:30

aGerman wrote:
15 Nov 2019 13:12
So, my understanding is that you already have the code to copy the files you're looking for. The missing thing is how to find the USB drives.
Give this a go:

Code: Select all

@echo off &setlocal

for /f tokens^=2^ delims^=^" %%i in (
  'WMIC Path Win32_DiskDrive Where "InterfaceType='USB'" Assoc /assocclass:Win32_DiskDriveToDiskPartition 2^>nul ^|findstr /c:"Disk #"'
) do for /f tokens^=4^ delims^=^" %%j in ('WMIC Path Win32_LogicalDiskToPartition 2^>nul ^|findstr /c:"%%i"') do echo %%j

pause
Steffen
Please help me where i can put it in my code (( i cloudn't do it right way

aGerman
Expert
Posts: 3775
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch file to search and copy particular file please help

#4 Post by aGerman » 15 Nov 2019 13:38

What did you try? And what particular problems are you facing?

sam201
Posts: 5
Joined: 15 Nov 2019 12:22

Re: batch file to search and copy particular file please help

#5 Post by sam201 » 15 Nov 2019 13:56

aGerman wrote:
15 Nov 2019 13:38
What did you try? And what particular problems are you facing?
I understand that code
@echo off &setlocal

for /f tokens^=2^ delims^=^" %%i in (
'WMIC Path Win32_DiskDrive Where "InterfaceType='USB'" Assoc /assocclass:Win32_DiskDriveToDiskPartition 2^>nul ^|findstr /c:"Disk #"'
) do for /f tokens^=4^ delims^=^" %%j in ('WMIC Path Win32_LogicalDiskToPartition 2^>nul ^|findstr /c:"%%i"') do echo %%j

pause
help me to get the USB drives.

in my code

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"
this part i understand that it's help me to make this drive
this part

Code: Select all

  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
   )
answer that take file with extention that intersted for me

Problem i think is here do i need this part or no ((

Code: Select all

:: 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

aGerman
Expert
Posts: 3775
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch file to search and copy particular file please help

#6 Post by aGerman » 15 Nov 2019 14:03

Problem i think is here do i need this part or no ((
It's your code. You should know it better than me. I'd incorporate my loop instead and use variable %%j in the rest of this code snippet instead of %%a.

sam201
Posts: 5
Joined: 15 Nov 2019 12:22

Re: batch file to search and copy particular file please help

#7 Post by sam201 » 15 Nov 2019 14:47

aGerman wrote:
15 Nov 2019 14:03
Problem i think is here do i need this part or no ((
It's your code. You should know it better than me. I'd incorporate my loop instead and use variable %%j in the rest of this code snippet instead of %%a.
Can you help m with search & copy command to take file from usb to my hdd for example in c:/Folderusb

aGerman
Expert
Posts: 3775
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch file to search and copy particular file please help

#8 Post by aGerman » 15 Nov 2019 15:35

You're not asking for help, you're asking for someone who writes the code for you. Actually not the purpose of a support forum.

Code: Select all

@echo off &setlocal
set "target=C:\Folderusb"

if not exist "%target%\" md "%target%"
for /f tokens^=2^ delims^=^" %%i in (
  'WMIC Path Win32_DiskDrive Where "InterfaceType='USB'" Assoc /assocclass:Win32_DiskDriveToDiskPartition 2^>nul ^|findstr /c:"Disk #"'
) do for /f tokens^=4^ delims^=^" %%j in ('WMIC Path Win32_LogicalDiskToPartition 2^>nul ^|findstr /c:"%%i"') do (
  pushd "%%j\"
  for /r %%k in (*.jpg *.ppt *.pptx *.pdf *.doc *.docx) do copy "%%~fk" "%target%\"
  popd
)

sam201
Posts: 5
Joined: 15 Nov 2019 12:22

Re: batch file to search and copy particular file please help

#9 Post by sam201 » 15 Nov 2019 15:43

aGerman wrote:
15 Nov 2019 15:35
You're not asking for help, you're asking for someone who writes the code for you. Actually not the purpose of a support forum.

Code: Select all

@echo off &setlocal
set "target=C:\Folderusb"

if not exist "%target%\" md "%target%"
for /f tokens^=2^ delims^=^" %%i in (
  'WMIC Path Win32_DiskDrive Where "InterfaceType='USB'" Assoc /assocclass:Win32_DiskDriveToDiskPartition 2^>nul ^|findstr /c:"Disk #"'
) do for /f tokens^=4^ delims^=^" %%j in ('WMIC Path Win32_LogicalDiskToPartition 2^>nul ^|findstr /c:"%%i"') do (
  pushd "%%j\"
  for /r %%k in (*.jpg *.ppt *.pptx *.pdf *.doc *.docx) do copy "%%~fk" "%target%\"
  popd
)
Thx Pro I don't Know How i Can Thank You !! :oops: :oops:

Post Reply