3

I'm trying to extract the name of the file that is on loop as a string. to organize many software outputs in different folders, named by the current process file.

So far i got this, but i can't get the string on the variable:

set DirecoryT=%%G
FOR /R D:\LiDAR_Data\LiDAR_DATA_EBA\ %%G in (*.laz) do (
    echo Directory %DirecoryT%
    set File=%DirecoryT:~29,9%
    echo Processing file %File%
pause
)
pause
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 5
    does it make sense to assign `%%G` to a variable, before `%%G` even exists? (Hint; no). You should also learn about [delayed expansion](https://stackoverflow.com/a/30284028/2152082). – Stephan Apr 24 '18 at 21:20
  • 3
    I bet if you read the very last section of the help file for the `FOR` command you will be amazed. – Squashman Apr 24 '18 at 21:22

2 Answers2

1

Have a go at this:

@echo off
for /R "D:\LiDAR_Data\LiDAR_DATA_EBA\" %%G in (*.laz) do (
   echo File Extension only: "%%~xG"
   echo FileName with Extension: "%%~nxG"
   echo FileName without Extension: "%%~nG"
   echo Full path and name: "%%G"
   echo Directory to file only: "%%~pG"
   echo Drive and directory is: "%%~dpG"
 )
 pause

Notice how we get the various parts of the string. For more on variable handling, simply run for /? from command line and read the help.

So a little closer to what you actually want, based on your attempt.

@echo off
for /R "D:\LiDAR_Data\LiDAR_DATA_EBA\" %%G in (*.laz) do (
   echo FileName "%%~nxG"
   echo Directory "%%~dpG"
   echo Processing file "%%~nxG"
 )
 pause

EDIT

As per your last comment to see only the last folder where the file exists:

@echo off
for /R "D:\LiDAR_Data\LiDAR_DATA_EBA\" %%G in (*.laz) do (
   echo FileName "%%~nxG"
   echo Directory "%%~dpG"
   echo Processing file "%%~nxG"
  for %%i in ("%%~dpG\.") do echo Last Folder "%%~nxi"
 )
pause
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

This should do the trick

for %%a in ("%pathToFile%") do set "newvariable=%%~na"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
KeionBeats
  • 37
  • 4
  • This code looks nothing like their existing code. Nor will it do the same thing especially since we do not know what is assigned to the %pathToFile% variable. – Squashman Apr 24 '18 at 21:31
  • No, this is completely wrong. `%pathtoFile%` where is that? Why run a script if you are going to specify exact path to the file? This is not even close to what the OP asked. – Gerhard Apr 25 '18 at 07:04