Page 1 of 1

check multiple folder for file if file exists copy to folder

Posted: 15 May 2009 04:50
by ninjao
Hi!

I am trying to come up with a way to check multiple folders for a file. If the folder contains a certain file it needs to paste a .jpg image there. If not it needs to goto the next folder and do the same check.

My batch experience is limited atm my code looks like this:

@echo off
u:
cd\
cd "U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\LG-F2400 MIC\"
pause
if not exist *.jar goto notthere
COPY U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\*.JPG
echo file there and copied
pause
goto end
:notthere
echo file not there... going to next folder.
cd..
cd "LG-F3000 MIC\"
pause
:end
echo this the end
pause

Now that would work, but only once. I would need to make tons of variations of this batch file for it to do all of the 300+ folders.

Is there a way I can do this easier?

Thanks in advance!

Posted: 15 May 2009 09:00
by RElliott63
Here's a start --- Untested

Just add the folders you need to test in FOLDERS.LST as below then run the script.

Code: Select all

@Echo Off

Set "Folders=C:\Folders\Folders.Lst"
Set "CDdir=%CD%"

Cls
For /F "Delims=" %%F IN (%Folders%) Do (

    Echo Checking Folder "%%F" for JAR files
    If Exist "%%F*.Jar" (
     Echo -- Found JAR file ... Copying JPG
     CD \D "%%F"
     Copy ..\*.JPG /v
     If Exist "%%F\*.JPG" (
      Echo -- JPG file Copied!
     )  Else (
      Echo -- Problem with Copy to "%%F"
     )
    )  Else (
     Echo -- File not found, Skipping this folder
    )

)

CD /D %CDir%

================================================================================
::: File - Folders.Lst
U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\LG-F2400 MIC\
Q:\13_ETC\13_Submissions\Griechenland\VDF Legacy\LG-F2300 MIC\
I:\13_ETC\14_Submissions\Griechenland\VDF Legacy\LG-F2200 MIC\
U:\13_ETC\15_Submissions\Griechenland\VDF Legacy\LG-F2100 MIC\
H:\13_ETC\16_Submissions\Griechenland\VDF Legacy\LG-F2000 MIC\
C:\Temp\SomeFolder\


-Rick

Posted: 15 May 2009 10:29
by avery_larry
If all the folders you care about are in the same "parent" folder, and you want to search ALL the folders and subfolders in some parent folder, you can modify what Rick did by using

cd /d drive:\path
for /d /r %%F in (*) Do (

And then you don't have to create the folders.lst file (it'll run through all the directories and subdirectories).

You could also do something with this type of for statement:

for /f "usebackq tokens=* delims=" %%F in (`dir /b /s u:\*.jar`) do (

You'll have to use "%%~dpF" for the copy statement:

copy "%%~dpF..\*.jpg"

Posted: 18 May 2009 01:31
by ninjao
Ah thanks guys!!

avery_larry would I put the
cd /d drive:\path
for /d /r %%F in (*) Do ( ... after the Cls?


like this?


Cls
cd /d drive:\path
for /d /r %%F in (*) Do (

Echo Checking Folder "%%F" for JAR files

Posted: 18 May 2009 11:45
by avery_larry
Yep. I didn't test it -- but it should go pretty easy from there.

Re: check multiple folder for file if file exists copy to fo

Posted: 19 May 2009 12:27
by SenHu
ninjao:

You want to

check a folder "U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\LG-F2400 MIC\" and all its sub and sub-sub folders.

In each folder,

if .jar file exists, copy files U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\*.JPG to that folder.


Here is a slightly different, more controlable script using biterscripting (http://www.biterscripting.com/install.html for free installation) .

Code: Select all

# Collect a list of folders under the main folder.
var string flist ; lf -n "*" "U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\LG-F2400 MIC\" ($ftype=="d") > $flist
# Process one by one
while ($flist <> "")
do
    var string folder ; lex "1" $flist > $folder
    # Does file *.jar exist ?
    var str output ; lf -n "*.jar" $folder > $output
    if ($output <> "")
        # .jar file exists. Copy .jpg files.
        system copy "U:\13_ETC\12_Submissions\Griechenland\VDF Legacy\*.JPG" $folder
    endif
done
 


Sen