Get Parent Directory as a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
TMC
Posts: 12
Joined: 19 Apr 2019 07:53

Get Parent Directory as a variable

#1 Post by TMC » 01 May 2019 07:49

Dear Forum,

I wrote a small batch script in which I want to merge point clouds.

Code:

Code: Select all

echo|set /p= ""C:\Program Files\CloudCompare\cloudcompare.exe" -SILENT -C_EXPORT_FMT BIN " > 01_save_clouds_at_once.bat
for %%f in (%*) DO echo|set /p=-O  %%f >>01_save_clouds_at_once.bat
echo -SAVE_CLOUDS ALL_AT_ONCE FILE: "C:\04_merged\FileName.bin" >>01_save_clouds_at_once.bat
01_save_clouds_at_once.bat
In this approach I can, by dragging and dropping multiple files onto the .bat file, merge (save all at once) all the files into a specific output folder - the result is one big point cloud.
But I have to hard code the file name ["C:\04_merged\FileName.bin"]. Is there a way to soft code the file name?

The original files (couple hundreds) are in an folder ["C:\03_output\ID\original_files.bin"], whereas the ID is a number (e.g 5 or 7 or 99.1 or 344).
So I thought I get the parent of the original file with this line:

Code: Select all

for %%i in (%%CD%%) DO set a=%%~ni
Since the programm CloudCompare is expectin a file extension I added ".bin"

Code: Select all

for %%i in (%%CD%%) DO set a=%%~ni.bin
If I run this code stand alone it is echoing the parent folder with the .bin (eg. 5.bin).

But If I want to implement this line into my script it is not recognizing the variable "a" as a variable.

Code:

Code: Select all

echo|set /p= ""C:\Program Files\CloudCompare\cloudcompare.exe" -SILENT -C_EXPORT_FMT BIN " > 01_save_clouds_at_once.bat
for %%f in (%*) DO for %%i in (%%CD%%) DO set a=%%~ni.bin echo|set /p=-O  %%f >>01_save_clouds_at_once.bat
echo -SAVE_CLOUDS ALL_AT_ONCE FILE: "C:\04_merged\%%a" >>01_save_clouds_at_once.bat
01_save_clouds_at_once.bat
With the script like this the filename is saved into the right directory but is named "a.bin". What do I need to change to soft code the filename?

I hope you guys have any sugesstions.

Best Tobias

Squashman
Expert
Posts: 4106
Joined: 23 Dec 2011 13:59

Re: Get Parent Directory as a variable

#2 Post by Squashman » 01 May 2019 08:46

The help files for the CALL and FOR commands explain the modifiers you can use to get a files parent directory.

CALL

Code: Select all

In addition, expansion of batch script argument references (%0, %1,
etc.) have been changed as follows:


    %* in a batch script refers to all the arguments (e.g. %1 %2 %3
        %4 %5 ...)

    Substitution of batch parameters (%n) has been enhanced.  You can
    now use the following optional syntax:

        %~1         - expands %1 removing any surrounding quotes (")
        %~f1        - expands %1 to a fully qualified path name
        %~d1        - expands %1 to a drive letter only
        %~p1        - expands %1 to a path only
        %~n1        - expands %1 to a file name only
        %~x1        - expands %1 to a file extension only
        %~s1        - expanded path contains short names only
        %~a1        - expands %1 to file attributes
        %~t1        - expands %1 to date/time of file
        %~z1        - expands %1 to size of file
        %~$PATH:1   - searches the directories listed in the PATH
                       environment variable and expands %1 to the fully
                       qualified name of the first one found.  If the
                       environment variable name is not defined or the
                       file is not found by the search, then this
                       modifier expands to the empty string

    The modifiers can be combined to get compound results:

        %~dp1       - expands %1 to a drive letter and path only
        %~nx1       - expands %1 to a file name and extension only
        %~dp$PATH:1 - searches the directories listed in the PATH
                       environment variable for %1 and expands to the
                       drive letter and path of the first one found.
        %~ftza1     - expands %1 to a DIR like output line

    In the above examples %1 and PATH can be replaced by other
    valid values.  The %~ syntax is terminated by a valid argument
    number.  The %~ modifiers may not be used with %*
FOR

Code: Select all

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

TMC
Posts: 12
Joined: 19 Apr 2019 07:53

Re: Get Parent Directory as a variable

#3 Post by TMC » 01 May 2019 09:06

Thank you for your tip,

but I thought I already did found a solution to get the parent of an folder:

Code: Select all

for %%i in (%%CD%%) DO set a=%%~ni
So my question remains: how do I pass the variable "a" to the next line?

Code: Select all

echo -SAVE_CLOUDS ALL_AT_ONCE FILE: "C:\04_merged\%%a"
I tried "SetLocal EnableDelayedExpansion" and exchanged the "%%" with "!" but this doesn't seem to help...

Best Tobias

kwsiebert
Posts: 29
Joined: 20 Jan 2016 15:46

Re: Get Parent Directory as a variable

#4 Post by kwsiebert » 01 May 2019 09:23

You want %a% instead of %%a. Variables are expanded by using % on both sides. %% before the name is for FOR loops, and command line or CALL arguments.

TMC
Posts: 12
Joined: 19 Apr 2019 07:53

Re: Get Parent Directory as a variable

#5 Post by TMC » 01 May 2019 09:37

If I change to line to

Code: Select all

echo -SAVE_CLOUDS ALL_AT_ONCE FILE: "C:\04_merged\%a%"
no file is exported as the variable "a" seems to bee empty.

Even if I try o hardcode the variable a in the following manner:

Code: Select all

for %%f in (%*) DO set a=5.bin echo|set /p=-O  %%f >>01_save_clouds_at_once.bat
It wont take the variable for saving the file... :/

I'm trying to take an other approach to tackle my problem.
Since I have a folder with over 800 sub-folders and all files in those sub-folders needs to be merged, I try to write a nested for loop to do this task, rather then using my drag an drop approach.

folder structure:

C:\parent\1\files\filesToBeMerged
C:\parent\2\files\filesToBeMerged
C:\parent\3\files\filesToBeMerged
[...]
C:\parent\n\files\filesToBeMerged

my approach would be:

Code: Select all

for every subfolder in parentfolder do :mergefiles
Right now I'm quite unsure how to write that bit of code... but I'm trying to figure it out on my own. But hints are always welcome :)

Best Tobias

kwsiebert
Posts: 29
Joined: 20 Jan 2016 15:46

Re: Get Parent Directory as a variable

#6 Post by kwsiebert » 01 May 2019 16:51

Is the code you posted part of a larger block enclosed in parentheses? If so, you need to use delayed expansion and use !a! instead of %a%.

TMC
Posts: 12
Joined: 19 Apr 2019 07:53

Re: Get Parent Directory as a variable

#7 Post by TMC » 02 May 2019 02:58

The full code is in my first post. It is the last code block.

I tried to use delayed expansion but it didn't work either... I guess I'm doing something wrong, but unfortunately I don't have time for this until the weekend.
I'll try to work out a minimal working code snipped so you guys can try this on you machines.

Best

Post Reply