Batch Script to find a Specific file and copy it

May 29, 2018 at 06:04:42
Specs: Windows 10
I cant seem to get the hang of it, i have been trying all day to make this batch script that would find a specific document within a directory's sub directories, copy it, and end.

@echo off


echo off

SETLOCAL ENABLEDELAYEDEXPANSION

set backupcmd=xcopy /s /c /d /e /h /i /r /y 

d:

FOR /R D:\ %a in (file.docx) do %backupcmd% "%%~dpa*" E:\b\e

@echo off

cls

This is what i did so far but it never seems to work, i always get many errors. Note that the file i need to copy is on a drive other that the drive i want to copy it to. The batch should be repeatable just by changing the file name within the batch, and i want it to work when not knowing the path of the file i want to copy.

With the current batch this is the response that i get:

~dpa*" E:\b\e was unexpected at this time.

Thanks
<3

message edited by turt3lback


See More: Batch Script to find a Specific file and copy it

Report •

#1
May 29, 2018 at 07:38:54
First of all please do not think i am trying to insult you, I'm just pointing out why you are having trouble & I am willing to help.

"%%~dpa*" should be "%~dp0*" (file where the .bat file is placed), but you might aswell use %CD% (batch file's current directory)

for more info type FOR /? in a cmd.exe window & hit any key a few times ;)

.docx files can not be read by batch, you have to convert it to a .txt file or xml or some other plain text file. (if you can read it with notepad it should be fine.)

for example the header of any docx file will be read as:
PK   ! ߤÒlZ   [Content_Types].xml ¢(   ´”ËnÂ0E÷•ú‘·Ub袪*‹>–-Ré{Vý’Ǽþ¾QU‘

of which i do not think you have a file called that in D:\

here is a preview, not working probably (
::assuming file.docx is located here: "D:\file.docx"
::also assuming "E:\b\e" is an existing folder/directory
::also assuming you know what this does "XCOPY /s /c /d /e /h /i /r /y"
::for /f is a loop, so it will try this command for every line in the (file.docx)
setlocal enabledelayedexpansion should not be needed since you are not using updated !variebles! instead of normal %variables%
in ("XCOPY /s /c /d /e /h /i /r /y") /d specifies a date/time which is not specified, giving an error
also switched /S & /E counter-act each other also giving an error (more info in XCOPY /?)
)

::start of copier.bat
@echo off
cd "D:\"
FOR /F "tokens=*" %%a in (file.docx) DO XCOPY /s /c /h /i /r /y "%CD%%%a" "E:\b\e"
::end of copier.bat

Please explain exactly what you´re trying to do, where you are reading it form, file Snippets(what is written in file.docx)
Which files have to be copied?
Does the code have to copy folder/directories or not?

i5-6600K[delid]@4.8GHz/4.4GHz@1.39v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-13-13-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1430Mhz core@1.256v/1900MHz BiosModded


Report •

#2
May 29, 2018 at 12:27:48
Sorry for my terrible explanation, I would never take anything you say as an insult but as a lesson.

I am trying to create a batch file that would look for any document with a specific term in its filename ( not read the content of that file and determine whether i copy it or not ). I just require a basic function of searching for those files within a directory and its sub directories then if any file matches the criteria, it would copy them to the path i indicated above (E:\b\e).

Here is the situation:-
I have a Drive called D:\ and within this drive there are many files i want to copy, the criteria which i would like the batch script to follow is to search within all directories and sub directories within the D:\ drive for any word document (.docx) that has the term "2017-2018" in its filename then copy them all to an already existing folder within another drive (E:\b\e).

message edited by turt3lback


Report •

#3
May 31, 2018 at 03:05:36
alright, give me some time, the ROBOCOPY command has a lot of switches/parameters& this will be the first time im using it.

in the mean time make a restore point for your system just incase :)

i5-6600K[delid]@4.8GHz/4.4GHz@1.39v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1430Mhz core@1.256v/1900MHz BiosModded


Report •

Related Solutions

#4
May 31, 2018 at 03:24:41
this should not copy files yet, but it will logg the output to the screen, giving you an indication of if the command is working as intended.(please post the output in a new comment if its not too long.)

"right-click --> mark can be used on older consoles"

i do not have 2 disks at home, so i can not replicate this myself, a console-output/log would be very nice

user input may be needed for certain confirmations, for example

"press any key to contiue", or press [y,n], etc


@echo off
robocopy "D:\" "E:\b\e" "*2017-2018*.docx" /L /TEE /V
echo finished, copy the above if needed
pause>nul
pause>nul

i5-6600K[delid]@4.8GHz/4.4GHz@1.39v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1430Mhz core@1.256v/1900MHz BiosModded

message edited by hidde663


Report •

Ask Question