Help with For /r loop

June 3, 2009 at 11:54:50
Specs: Windows XP SP3
Is there a way to include multiple sets (both positive and negative) in one variable inside of a for /r loop? Here's the command I'm using:

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

But, I also want to check for something like this:

for /r %%i in (*.shp) and not in (*_wgs.shp) do copy "%%i" "%%~pni_wgs.shp" /-Y

See More: Help with For /r loop

Report •

June 3, 2009 at 23:17:48
tell us what exactly are you wanting to do.

Report •

June 4, 2009 at 08:57:08
I wrote a batch file to make a copy of a file and add "_wgs" to the filename (not the extension). However, I don't know whether the folders (which could number 300 or so and contain 29 or more subfolders 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 filename.

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. Does that help?

Report •

June 4, 2009 at 13:04:58
So something like this?
for /r %%i in (*.shp) do IF NOT EXIST "%%~PNi_wgs.shp" copy "%%i" "%%~pni_wgs.shp"

Report •

Related Solutions

June 4, 2009 at 13:52:12
Yes, exactly ... however, this command still duplicates the existing "%%~PNi_wgs.shp" copy so I end up with "%%~PNi_wgs_wgs.shp".

I got that result with another attempt, but different set of commands:

did not work:
if not exist *_wgs.shp for /r %%i in (*.shp) do copy "%%i" "%%~pni_wgs.shp"

I understand you can include multiple file sets in the in section, such as:

in (*.shp *.doc *.txt)

However, I have not been successful in performing a negative set.

Report •

June 10, 2009 at 09:28:33
In case anyone is interested, here's the solution. Text in bold font is what you change.

For /F "delims=" %%I In ('Dir /A-D /B /S *.dbf^|Findstr /I /V /G:excludelist.txt^|Findstr /E /I /L /V "_wgs.dbf"') Do If Not Exist "%%~pnI_wgs.dbf" Copy "%%I" "%%~pnI_wgs.dbf"

Contents of excludelist.txt file:

Thanks anyway!

Report •

Ask Question