Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

Solved: Help with For /r loop


(!)

mcwilsong's Avatar
mcwilsong mcwilsong is offline
Computer Specs
Junior Member with 2 posts.
THREAD STARTER
 
Join Date: Jun 2009
Experience: Advanced
09-Jun-2009, 10:21 AM #1
Red face Solved: Help with For /r loop
I wrote a batch file to make a copy of a file and add "_wgs" to the file name (not the extension). However, I don't know whether the folders (which could number 300 or so and contain 29 or more sub folders each) if there are any existing files already with a "filename_wgs.shp" name. I have to assume that since I haven't run my batch on the folder yet, that the file was created manually and I don't want to overwrite it.
I would like the command that creates the copy to take into account all *.shp files in the
loop, but exclude anything that already contains "_wgs.shp" in the file name.

I tried using the /-Y, but it
requires a manual reply and I don't want to use /Y because, as I said earlier, I don't want to overwrite the file. This is what I started with:

for /r %%i in (*.shp) do copy "%%i" "%%~pni_wgs.shp"

It works great, however, I realized that there are sub directories I want to skip as well. So I figured I'd move copy into xcopy. However, this does not work:

for /r %%i in (*.shp) do xcopy %%i %%~pni_wgs.shp /t /e /Exclude:excludelist.txt

excludelist.txt contains:
left (hoping that it would exclude every folder name that has "left")
right (hoping that it would exclude every folder name that has "right")
wgs.shp (hoping that it would exclude every file name that has "wgs.shp")

All I get is a "Cannot perform a cyclic copy" error. Is what I'm trying to do even possible as the command stands? I'm trying to keep it as simple as possible. Any help would be greatly appreciated!

Glenn
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Jun-2009, 08:39 AM #2
Welcome to TSG!

When using Xcopy and copying a directory tree, the destination can't be a subfolder of the source. While the For /r loop is returning single file names, the /T and /E switches tell Xcopy that it's copying a tree, so it gives the cyclic copy error.
You've left off the d modifier, so there is no drive letter specified in the Destination. It will still work, but in some situations, that can give unexpected results, so best to include it: %%~dpnI

If you remove the /E and /T switches, you'll get another prompt for each file:
Does C:\Test\filename_wgs.shp specify a file name
or directory name on the target
(F = file, D = directory)?


Xcopy was meant to copy files, not rename them. When copying single files, it expects the Destination to be a folder name ending with \. If you don't have the \, it gives the above prompt. So if you want to rename the file, there is no way to avoid the prompt.

And neither example will overwrite an existing file. If filename_wgs.shp exists, your code will copy it to filename_wgs_wgs.shp.

Your best bet is to use a Dir command, pipe it to Findstr to remove the folders you want to exclude, then pipe it to a 2nd Findstr to exclude files that already have the _wgs added to their name

You then check if filename_wgs.shp already exists, and if not, then copy the file.

This should do the trick:
Code:
Set _root=C:\Test
PushD %_root%
For /F "delims=" %%I In ('Dir /A-D /B /S *.shp^|Findstr /I /V /G:excludelist.txt^|Findstr /E /I /L /V "_wgs.shp"') Do If Not Exist "%%~dpnI_wgs.shp" Copy "%%I" "%%~dpnI_wgs.shp"
PopD
If excludelist.txt is not in the _root folder (C:\Test in this example), you'll need to specify the path to the file.
To specify a folder name in excludelist.txt, best to use \foldername\ format, or you may get partial matches and exclude folders you didn't intend to.
For example, if you put folder1 in the excludelist.txt file, it will exclude folder1, folder11, folder1mustbeincluded, and dofolder125now.
\folder1\ will only exclude folder1.

HTH

Jerry
mcwilsong's Avatar
mcwilsong mcwilsong is offline
Computer Specs
Junior Member with 2 posts.
THREAD STARTER
 
Join Date: Jun 2009
Experience: Advanced
10-Jun-2009, 10:52 AM #3
Excellent! Thank you very much Jerry ... that did work perfectly. One more question ... if I exclude the path part, is there any harm in just running the batch file at the root of the folder structure you want to effect?

I was just thinking, in terms of ease of use, by copying and pasting the appropriate batch files (in my case: copy_shp.bat, copy_shx.bat, copy_dbf.bat, copy_wgs.bat, excludelist.txt) to the root directory and then running them by calling copy_wgs.bat, someone else using the set of files wouldn't need to alter anything if performing the same function.
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Jun-2009, 11:00 AM #4
The Set, PushD and PopD are only needed if the folders you want to work on are not the same as the one the batch file is in, so all you need is the For statement and put the file in the folder you want to work on.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


Tags
dos, for loop, xcopy

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑

Content Relevant URLs by vBSEO 3.3.2