0Day Forums
How to return to the original directory after invoking change directory in DOS batch? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: .bat & .wsf & .cmd (https://0day.red/Forum-bat-wsf-cmd)
+--- Thread: How to return to the original directory after invoking change directory in DOS batch? (/Thread-How-to-return-to-the-original-directory-after-invoking-change-directory-in-DOS-batch)



How to return to the original directory after invoking change directory in DOS batch? - quonevnhvshk - 07-23-2023

I want to create a batch file, `batch.bat`, that accepts 2 mandatory arguments:

- `%1` represents a path relative to the current directory.
- `%2` represents a filaname.

Assume the current directory is `father\me\`.

User can use this batch as follows:

- `batch child/grandchild log`
- `batch ../brother log`

The job description of `batch.bat` is as follows.

1. Moves to `%1` directory,
2. Iterates all `*.tex` file in the `%1` directory.
3. Save the result in the directory before moving.

The following is the incomplete code:

rem batch.bat takes 2 arguments.
cd %1
dir /b *.tex > <original directory>\%2.txt


How to return to the original directory after invoking change directory in DOS batch?


RE: How to return to the original directory after invoking change directory in DOS batch? - serenarvmtf - 07-23-2023

If you want to RETURN to original directory, do the first change with `PUSHD` and return with `POPD`. That is, moves to %1 directory must be achieved with

PUSHD %1

instead of CD %1, and the return is achieved with

POPD

instead of CD where?

If you want to ACCESS the original directory after changed it, store it in a variable this way:

SET ORIGINAL=%CD%

and use %ORIGINAL% later, for example:

dir /b *.tex > %original%\%2.txt


RE: How to return to the original directory after invoking change directory in DOS batch? - propagandism321 - 07-23-2023

You can always set %cd% to a variable before changing directories:

set current="%cd%"
cd "C:\Some\Other\Folder"
cd "%current%"

In most cases, creating a variable with the directory is used in Batch Scripts. If the script is semi-lengthy, I will define my variables in the beginning of the script that includes important paths, files, subs, and/or long commands.

@ECHO OFF
REM Variables
::Programs
SET save_attachments=C:\Program Files\APED\Program\save_attachments.vbs
SET sendemail=C:\Program Files\APED\Program\sendkeys.vbs
SET tb=C:\Program Files\Mozilla Thunderbird\thunderbird.exe
SET fox=C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe
SET spool=C:\WINDOWS\system32\PRNJOBS.vbs

::Directories
SET new=C:\Program Files\APED\New
SET printing=C:\Program Files\APED\Printing
SET finish=C:\Program Files\APED\Finish
SET messages=C:\Program Files\APED\Script_Messages
SET nonpdf=C:\Program Files\APED\NonPDFfiles
SET errorfiles=C:\Program Files\APED\Error Files

::Important Files
SET printlog=C:\Program Files\APED\Script_Messages\PrintLOG.txt
SET printemail=C:\Program Files\APED\Script_Messages\EmailPrintLOG.txt
SET errorlog=C:\Program Files\APED\Script_Messages\ErrorLOG.txt
SET erroremail=C:\Program Files\APED\Script_Messages\EmailErrorLOG.txt
SET movefiles=C:\Program Files\APED\Script_Messages\MoveFiles.txt

However, PUSHD and POPD are great solutions if it is short and sweet imo.


RE: How to return to the original directory after invoking change directory in DOS batch? - kassixjyox - 07-23-2023

Definitely PUSHD / POPD is the preferred way to do this. But there is a (undocumented?) feature of SETLOCAL / ENDLOCAL that accomplishes the same thing (in addition to everything else SETLOCAL does).

If you change directory after a SETLOCAL, then you will return to the original directory upon ENDLOCAL.

cd OriginalLocation
setlocal
cd NewLocation
endlocal
rem we are back to OriginalLocation

One other thing with SETLOCAL that *is* documented - Any SETLOCAL within a called batch or :label routine will be terminated with an implicit ENDLOCAL upon exiting the batch or routine. The implicit ENDLOCAL will return to the original folder just as an explicit ENDLOCAL.

cd OriginalLocation
call :ChangeLocation
rem - We are back to OriginalLocation because :ChangeLocation did CD after a SETLOCAL
rem - and there is an implicit ENDLOCAL upon return
exit /b

:ChangeLocation
setlocal
cd NewLocation
exit /b

I wouldn't recommend using SETLOCAL/ENDLOCAL instead of PUSHD/POPD. But it is a behavior you should be aware of.

***Response to johnny's comment***

It can get confusing when PUSHD/POPD and SETLOCAL/ENDLOCAL are combined. The ENDLOCAL does ***not*** clear the PUSHD stack, as evidenced by the following:

setlocal
cd test
@cd
pushd new
@cd
endlocal
@cd
popd
@cd

--OUTPUT--

D:\test>setlocal

D:\test>cd test
D:\test\test

D:\test\test>pushd new
D:\test\test\new

D:\test\test\new>endlocal
D:\test

D:\test>popd
D:\test\test




RE: How to return to the original directory after invoking change directory in DOS batch? - dicho486 - 07-23-2023

set ORIGINAL_DIR=%CD%

REM #YOUR BATCH LOGIC HERE

chdir /d %ORIGINAL_DIR%