have a list of folder containing one file for each folder, need to find this file path in another folder/subfolder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

have a list of folder containing one file for each folder, need to find this file path in another folder/subfolder

#1 Post by goodywp » 31 Jul 2017 12:19

I have a task in batch file, prefer batch this time:

1) I had a list of folders (temp) to have each file in each of folder:

ex. c:\myfolder\000\1234.S3S
c:\myfolder\001\1235.S3S
c:\myfolder\002\1236.S3S
c:\myfolder\003\1237.S3S
c:\myfolder\004\1238.S3S
c:\myfolder\005\1239.S3S
..............................

2) Now my task is to find each above file's path in another folder, e.x d:/anotherfolder/subfoldlers..

possible solution:

for loop in each folders (000, 001, 002, 003, 004, 005)

or I copy all files into one new folder then to loop in each file within this new folder

or I make a new list txt file containing all these above files, then search one by one from this text file and do the search in d:/anotherfolder/subfolders...

I am stuck in this situation and do not know which one is best to do this task...

Thanks in advance.

Appreciated all your help...

William


Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: have a list of folder containing one file for each folder, need to find this file path in another folder/subfolder

#3 Post by Hackoo » 01 Aug 2017 02:26

In your case ,you should use the command WHERE : To locate and display files in a directory tree
WHERE (Windows 2003 + )

Locate and display files in a directory tree.

The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.

Syntax
WHERE [/r Dir] [/q] [/f] [/t] Pattern ...

WHERE [/q] [/f] [/t] [$ENV:Pattern

key
/r A recursive search, starting with the specified Dir directory.

/q Don’t display the files but return either an exit code of 0 for success
or 1 for failure.

/f Display the output file name in quotation marks.

/t Display the size, time stamp, and date stamp of the file.

pattern The Drive\Directory\file, or set of files to be found.
you can use wildcard characters ( ? * ) and UNC paths.

ENV Path to search where ENV is an existing environment variable containing one or more paths.

By default, WHERE searches the current directory and the paths specified in the PATH environment variable. Optional search paths (in pattern) should not be used in conjunction with /r Dir.

You can give a try for this batch file and make change to yours folders of course

Code: Select all

@echo off
Title Searching for *.S3S files paths and copy them to a new location
Mode con cols=90 lines=20 & color 9E
set "Pattern=S3S"
Set "Location=%userprofile%\Desktop"
Set "Destination=C:\Copy_S3S_Files"
set "LogSearch=%~dp0%~n0.txt"
If exist "%LogSearch%" Del "%LogSearch%"
Cls
echo(
echo       --------------------------------------------------------------------------
echo              Please Wait a while ....... Searching is in progress .........
echo       --------------------------------------------------------------------------
   Where /R %Location% "*.%Pattern%" /F >>"%LogSearch%" 2>&1
)
If "%ErrorLevel%"=="1" (
Cls
echo(
echo       --------------------------------------------------------------------------
echo                         No file(s^) found with this Pattern
echo       --------------------------------------------------------------------------
pause & exit
) else (
If Not exist "%Destination%" MD "%Destination%"
Setlocal enableDelayedExpansion
@for /f "delims=" %%a in ('Type "%LogSearch%"') do (
   @echo found file %%a
   If not exist "%Destination%\%%~nxa" (
      @Copy %%a "%Destination%"
   ) else (
      Set /a Count+=1
      @Copy %%a "%Destination%\%%~na(!Count!)%%~xa"
      )
   )
)
Start "" "%LogSearch%"
If exist "%Destination%" Explorer "%Destination%"

Post Reply