Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 642 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to return to the original directory after invoking change directory in DOS batch?

#1
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?
Reply

#2
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
Reply

#3
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.
Reply

#4
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

Reply

#5
set ORIGINAL_DIR=%CD%

REM #YOUR BATCH LOGIC HERE

chdir /d %ORIGINAL_DIR%
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through