0

I've written a script to search for a shape file (.shp), I want to change to that directory once found: Here's what I have

 FOR /F %%X IN ('DIR /S /B *.shp') DO SET shapefolder=%%~DPNX
 IF DEFINED shapefolder (
 ECHO %shapefolder%
 CD /d shapefolder REM doesn't work
 ) ELSE (
 ECHO File not found
 )

What am I missing?

2 Answers 2

1

Two things are wrong with your code. The first, as Oliver already pointed out, is that cd /d shapefolder should be cd /d %shapefolder% so that you're calling the actual variable.

The other thing is that SET shapefolder=%%~DPNX should beSET shapefolder=%%~DPX instead.

This is because the N refers to the filename of the file it finds, so if you have a square.shp, your code currently will look for C:\files\shapes\square instead of C:\files\shapes\ like it's supposed to.

Sign up to request clarification or add additional context in comments.

1 Comment

cd \d %shapefolder% ?? Are you sure you don't mean cd '/d %shapefolder%
0

Just add the percent sign to your variable in the cd command:

CD /d %shapefolder%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.