简体   繁体   中英

batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory

So if I have C:\\drivers\\* with * indicating many sub-folders, I want to find out where my inf files are located, and then copy ALL files that are located in the same directory where my inf files are located and all sub-directories.

It has been easy to create a script that will copy all .inf files found to my directory:

FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y %%a C:\test

But copying the other files that are located in the same directory and all sub-directories has been difficult.

Such as, if the inf file is located under C:\\drivers\\sbdrv\\hseries\\usb30\\amdhub\\w7 and the sys file is located in the sub-folder of x86, I need the sys file to be kept in the same sub-folder but under the destination of C:\\test\\x86.

Any ideas?

EDIT: Maybe this will make it easier. As soon as it finds one .inf file in a folder, it should copy the entire folder over to test as well as all sub-folders and move on to the next one. So if it sees the first .inf file located C:\\drivers\\sb3045\\sb407.inf it should copy all files and folders under sb3045 without copying the folder sb3045 itself, and then move on to folder C:\\drivers\\sb4055\\drivers\\oem\\intel\\id6077.inf and copy all files and folders under the intel folder without copying the intel folder itself.

EDIT2: It looks like this will work, but it is slow as it finds every .inf and is copying over any old files if there is more than one .inf file per folder

@ECHO ON

SETLOCAL ENABLEDELAYEDEXPANSION

CD\\

CD drivers

FOR /f "tokens=* delims=" %%B IN ('DIR /b /s /o:gen .inf') DO ( XCOPY "%%~dpB .*" "C:\\test\\" /e /c /h /y

If anyone has a cleaner or quicker idea, let me know please. Until then, I'll have to work with this one.

尝试这个:

 FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y "%%~dpa*" C:\test
for /R C:\WINDOWS\inf %%a in (*.inf*) do (
xcopy /qv /T  %%a .\test\
xcopy /qv  %%a .\test\
)
pause

this code worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM