Page 1 of 1

Newbie... Need help with a Batch Script

Posted: 09 Mar 2009 13:33
by sqldba20
I am a newbie to DOS Batch Scripts and need help writing a code. My requirement is to copy file names starting with "ml_*" (asterick to act as wildcard) in folder (X:\ABC) and all other files in folder (X:\PQR). Any help on this is appreciated.


Code: Select all

for %%a IN (U:\*) do (

IF %%a = "ml_*" (

 xcopy %%a X:\ABC\ /y

) ELSE (
 
 xcopy %%a X:\PQR\ /y

)

)



Thanks !

Posted: 09 Mar 2009 14:38
by RElliott63
Here's a start...


Code: Select all

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%F IN ('Dir X:\ABC\*') DO (
     Set "f=%%~nF"
     Set "pre=!f:~0,3!"

     If /I [!pre!] equ [ml_] (
        Echo --Copying  File Name: !f! to folder A
        Copy /v %%F \NewFolderA
     )  Else (
        Echo -- Copying File Name: !f! to folder B
        Copy /v %%F \NewFolderB
     )
)

:Exit

Posted: 19 Mar 2009 09:26
by avery_larry
Well, the first part is easy (and probably obvious):

Code: Select all

xcopy ml_* x:\abc


And the 2nd part can be done 2 ways:

Code: Select all

echo \ml_>tmp.txt
echo \tmp.txt>>tmp.txt
xcopy .\*.* X:\PQR /exclude:tmp.txt
del tmp.txt

That creates a temp file which includes the string to be excluded and the temp file name and then proceeds to use xcopy which will exclude the ml_* files as well as the temp file.

The alternate (and possibly easier) 2nd portion would be like this (though it'll run a bunch of separate xcopy's instead of 1, but that probably doesn't matter -- plus it avoids using a temp file):

Code: Select all

for /f "usebackq tokens=" %%a in (`dir /a-d /b^|findstr /v /B "ml_"`) do xcopy %%a X:\PQR

Posted: 19 Mar 2009 15:39
by *SCRIPTER*
---- :idea: