1

I am trying to write a simple batch file, that when run, will move and rename some files within a certain directory. I know I can navigate to a directory in command prompt with

cd "path_to_directory"

The problem is that this directory's name changes frequently as the program that uses it is updated. I do not want to have change the batch file every time the directory's name changes to get it to work correctly. However, it is the only directory that is in its parent directory. Knowing this, is there any way to make my batch file navigate to it automatically?

9
  • try go to directory on this way: cd * May 29, 2015 at 13:11
  • Are you asking if there is a way to navigate to a folder in a batch, of a folder who's name changes, without updating the batch file each time the name of the folder changes?
    – Ramhound
    May 29, 2015 at 13:16
  • @Ramhound yes, the program is patched every few weeks and that changes the directory's name. I would prefer not to have to search and find what the name is changed to and then modify the batch file.
    – user339948
    May 29, 2015 at 13:17
  • 1
    The fact I asked for clarification means you should make sure the question is clear.
    – Ramhound
    May 29, 2015 at 13:27
  • AT ramhound, He did reply, so if you still want him to clarify further, perhaps you should be more clear about what you want clarified
    – barlop
    May 30, 2015 at 1:46

1 Answer 1

1

From the parent directory either of the following will work:

for /d %d in (*) do cd "%~d"

for /f "delims=" %d in ('dir /ad /b') do cd "%~d"

Note that this will only work given the condition you've specified, i.e. that the parent directory contains only a single sub-directory that you want to switch to.

6
  • I think %d is fine. If a directory has spaces CD still doesn't need quotes around it
    – barlop
    May 30, 2015 at 1:55
  • I suppose so. I just like to quote all file/dir names with spaces as a matter of habit to prevent any possible unforeseen hiccups.
    – Karan
    May 30, 2015 at 1:59
  • It's also worth noting re your answer that this only works because he has only one directory to go into. With more than one, this happens pastebin.com/RPAQNtRw (as you know) and one would then need a pushd/popd/cd .. after the do
    – barlop
    May 30, 2015 at 2:08
  • Yes, I didn't mention it because that's clearly part of the question. This is not meant to be a generic solution to recurse into multiple sub-dirs.
    – Karan
    May 30, 2015 at 2:12
  • Yeah I wasn't suggesting a generic solution to recurse multiple subdirs.. rather I was just suggesting stating the point that this is for when you have one dir there. It makes the answer more digestable because one doesn't then have to refer back to the question to read a finer point of his question. And it makes it clearer what the answer is/isn't.
    – barlop
    May 30, 2015 at 2:14

You must log in to answer this question.