Newbie... Need help with a Batch Script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sqldba20
Posts: 1
Joined: 09 Mar 2009 13:26

Newbie... Need help with a Batch Script

#1 Post by sqldba20 » 09 Mar 2009 13:33

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 !

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 09 Mar 2009 14:38

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 19 Mar 2009 09:26

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

*SCRIPTER*
Posts: 18
Joined: 16 Mar 2009 08:18
Location: N/A
Contact:

#4 Post by *SCRIPTER* » 19 Mar 2009 15:39

---- :idea:

Post Reply