# Need help connecting two DOS commands



## tjesker (Oct 8, 2010)

I am writing a batch file that puts two commands together but I cannot seem to get it to recognize both parts:

SET restPath=dir "*Test Restore*" /b /s
cd & %restPath%

I want the result to be:

cd D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM\Test Restore

but instead I get just the second part:

D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM\Test Restore

I've tried using the pipe symbol - | instead of & - which made no difference, and also tried storing "cd" in a variable and using that as the first command - and that also made no difference.

Can someone tell me how to do this?

Thanks
Tom


----------



## Squashman (Apr 4, 2003)

Are you trying to use the environmental variable %cd% for the current directory or are you literally trying to use the Change Directory command. The Change directory command needs to know where to change to. You didn't give it any input.

If you just want a directory listing of the sub directory then do this.

dir /b /s "Test Restore" /b /s

You need to explain a little better what you are doing and where this batch file is launched from.

How are we suppose to know where this part of the path is coming from:
D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM


----------



## tjesker (Oct 8, 2010)

Thank you for your reply. I am trying to change directory using the cd command. I used the ampersand because when I use:

cd %restPath%

I get the error:

The filename, directory name, or volume label syntax is incorrect.

D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM is the result of the command:
SET restPath=dir "*Test Restore*" /b /s
...and that part is working as intended.

What I am trying to do: Find the location of the "Test Restore" directory and change to that directory. This is a small part of a script that will mount multiple backup images in our Zenith BDR for different server volumes, locate a a word document that gets created on the first day of each month on every volume saved to a "Test Restore" directory, and restores it to a specified location. The purpose is to verify that all volume are being backed up and a file can be restored from each volume. It's incredibly slow and tedious to do this manually so I want to automate the process so that I only have to double click on a batch file to make it all happen. All of the other parts pretty much work but I can't figure out how to change to the darn directory where the file is saved.


----------



## Squashman (Apr 4, 2003)

You can't wildcard to change to a directory. You need to know the directory path to do that. 

If you know the file name you could search for the file name by using the DIR command inside a For Loop and piping the DIR cmd to the FINDSTR command.


----------



## Squashman (Apr 4, 2003)

```
REM Looking for the Test Restore directory.
For /f "tokens=*" %%I In ('Dir /AD /B /S D:\ ^|Findstr /C:"Test Restore"') DO SET _path=%%I
```


----------



## tjesker (Oct 8, 2010)

Not sure I understand. I'm finding the path to the file which is located in the "Test Restore" directory with the command:


```
dir "*Test Restore*" /b /s
```
If I save the result to the variable restPath...


```
SET restPath=dir "*Test Restore*" /b /s
```
..then I can type:


```
%restPath&
```
...and the path I'm looking for displays on the screen:

D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM\Test Restore

I want to put "cd" in front of %restPath& to get:

cd D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM\Test Restore

...inside a batch file. Is there some way to do this?

BTW, I tried the code from your last post (although I don't understand but I think it's trying to find the path - which dir "*Test Restore*" /b /s already does) and I got:

%%I was unexpected at this time.

Thanks again for your help on this.

Tom


----------



## Squashman (Apr 4, 2003)

The only thing your SET statement is doing is assiging a TEXT string to it. You can't assign the output of a command in a SET statement like you can in other shell scripting languages. ie: BASH. You need to do it in a FOR LOOP.


----------



## TheOutcaste (Aug 8, 2007)

```
For /F "Tokens=* Delims=" %%I in ('dir "*Test Restore*" /B /S /AD') Do CD /D %%I
```
If you type that at the command line you only use one % symbol:

```
For /F "Tokens=* Delims=" %I in ('dir "*Test Restore*" /B /S /AD') Do CD /D %I
```


----------



## Squashman (Apr 4, 2003)

tjesker said:


> I want to put "cd" in front of %restPath& to get:
> 
> cd D:\mount\Thursday\C_VOL-b001-i23179.spi__10_7_2010_12.19.43_PM\Test Restore


In theory what your are trying to do is

CD %restpath%

restpath=DIR "Test Restore" /b /s

So when you try to put CD in front of it the command interpreter sees this.

CD DIR "Test Restore" /b /s

It does not see this

CD D:\mount\thursday\etc...etc...


----------



## gfne (Oct 28, 2010)

Your best bet is to comment the @echo off line and run and see whats going on. When your done, uncomment it back.


----------

