Need Wildcard help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
whenry6000
Posts: 2
Joined: 01 Feb 2012 16:32

Need Wildcard help

#1 Post by whenry6000 » 01 Feb 2012 16:44

All,
I have written a batch file to loop through a directory, but only retrieve and copy files with names that contain the strig 'APR_DATA'. TO get the listing, I use *APR_DATA*.txt, as the string is in the middle of the file name. When I run it, however, it appears to return all of the files in the directory. THe wildcards work from just a regular DOS command line. Here is the text of my batch file. Any ideas why it's not working?


for /f "\\win2kdevfs1\sharedfolders$\FTP\LOAD\APR_DRG\" %%i IN (*APR_DATA*.txt) DO call :moveit %%i

:moveit
IF NOT EXIST '\\dwbaseline\ETLSourceFiles\FlatFiles\Test\%1' COPY \\win2kdevfs1\sharedfolders$\FTP\LOAD\APR_DRG\%1 \\dwbaseline\ETLSourceFiles\FlatFiles\Test\%1

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Need Wildcard help

#2 Post by Squashman » 01 Feb 2012 17:12

Code: Select all

pushd "\\win2kdevfs1\sharedfolders$\FTP\LOAD\APR_DRG\"
for %%i IN (*APR_DATA*.txt) DO call :moveit %%i

:moveit
IF NOT EXIST '\\dwbaseline\ETLSourceFiles\FlatFiles\Test\%1' COPY \\win2kdevfs1\sharedfolders$\FTP\LOAD\APR_DRG\%1 \\dwbaseline\ETLSourceFiles\FlatFiles\Test\%1

I don't see any reason to call another label in this instance.

Code: Select all

pushd "\\win2kdevfs1\sharedfolders$\FTP\LOAD\APR_DRG\"
for %%i IN (*APR_DATA*.txt) DO (
IF NOT EXIST "\\dwbaseline\ETLSourceFiles\FlatFiles\Test\%%i" COPY "%%I" \\dwbaseline\ETLSourceFiles\FlatFiles\Test\
)

whenry6000
Posts: 2
Joined: 01 Feb 2012 16:32

Re: Need Wildcard help

#3 Post by whenry6000 » 01 Feb 2012 17:23

That seems to work. Thanks! I am still confused as to why the wildcard didn't work. The original code I used copied the files, it just didn't filter them the way I wanted.

One other question though: If I run this from a command line (instead of double-clicking in Windows Explorer), it changes the directory. There there any way to avoid that? Or to return to the original directory where it was executed?

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Need Wildcard help

#4 Post by Squashman » 01 Feb 2012 18:54

Sorry that's my fault. Put the POPD command at the end of the batch file.

Post Reply