1

I need to get file name with out the file extension, folder name out putted to a csv file. I am able to get file name and folder name using:

@ECHO OFF
SETLOCAL
PUSHD "%~1"
FOR /f "delims=" %%i IN ("%cd%") DO SET directory=%%~nxi
(
FOR /f "delims=" %%i IN ('dir /b /a-d /on') DO (
SETLOCAL enabledelayedexpansion
ECHO "%%i","!directory!"
endlocal
)
)>filelist.csv

How can I rewrite this so the file extension is removed and if there are subfolders it will grab the subfolder name too?

2 Answers 2

1
@echo off
    setlocal enableextensions disabledelayedexpansion
    if not "%~1"=="" (
        (for /f "tokens=*" %%i in ('dir /s /b /on "%~1\*"') do (
            set "file=%%~dpni"
            setlocal enabledelayedexpansion
            echo(!file:%~dp1=!
            endlocal
        )) > filelist.csv
    ) else (
        call "%~f0" "%cd%"
    )
    endlocal

Not sure about the final format. Try and comment.

EDITED - to handle case exposed by Andriy M

Sign up to request clarification or add additional context in comments.

4 Comments

Seems nice. I would probably move the setlocal/endlocal "parentheses" inside the loop body to avoid premature consumption of exclamation marks in case the argument directory's full path has any.
@sum1: Perhaps you are not aware that you can formally accept an answer as the solution of your problem. You might want to consider looking through all your questions' answers to see if there are any worth accepting.
Ah, you understood my comment better than I meant it! However, as a final touch, perhaps adding disabledelayedexpansion to the first setlocal would not be a bad idea either. I know it is usually disabled by default, but it wouldn't hurt to be on the safe side in this case, would it. :)
@AndriyM: nice. Two things more to remember :)
0
@echo off
setLocal
pushd "%~1"
set "cur_path=%cd:~2%"
setLocal enableDelayedExpansion
FOR /f "delims=" %%i IN ('dir /b /s /a-d /on') DO (
    SET "file_path=%%~dpni"
    SET "file_path=!file_path:~2!"
    SET "file_path=!file_path:%cur_path%=!"
    ECHO "!file_path!","%%~di%cur_path%"
)
endLocal
endLocal

EDIT with additional ~ replacing (comparatively slow - could be optimized with macros... ):

@echo off
pushd .
set "cur_path=%cd:~2%"
call :wavereplacer "%cur_path%" "-" nw_cur_path
setlocal enableDelayedExpansion
FOR /f "delims=" %%i IN ('dir /b /s /a-d /on') DO (
    SET "file_path=%%~dpni"
    SET "file_path=!file_path:~2!"
    SET "file_path=!file_path:%cur_path%=!"
    CALL :wavereplacer "!file_path!" "-" file_path  
    ECHO "!file_path!","%%~di%nw_cur_path%"
)

endlocal

endlocal

goto :eof

:wavereplacer String Replacer [RtnVar]
setlocal
rem  the result of the operation will be stored here
set "result=#%~1#"
set "replacer=%~2"
call :strlen0.3 result wl
call :strlen0.3 replacer rl

:start

  set "part1="
  set "part2="

  rem splitting the string on two parts
  for /f "tokens=1* delims=~" %%w in ("%result%") do (
   set "part1=%%w"
   set "part2=%%x"
  )

  rem calculating the count replace strings we should use
  call :strlen0.3 part1 p1l
  call :strlen0.3 part2 p2l
  set /a iteration_end=wl-p1l-p2l

  rem creating a sequence with replaced strings
  setlocal enableDelayedExpansion
  set "sequence="
  for /l %%i in (1,1,%iteration_end%) do (
   set sequence=!sequence!%replacer%
  )
  endlocal & set "sequence=%sequence%"

  rem adjust the string length
  set /a wl=wl+iteration_end*(rl-1)

  rem replacing for the current iteration
  set result=%part1%%sequence%%part2%
  rem if the second part is empty the task is over
  if "%part2%" equ "" (
   set result=%result:~1,-1%
   goto :endloop
  )


  goto :start

:endloop
endlocal & if "%~3" neq "" (set %~3=%result%) else echo %result%
exit /b

:strlen0.3  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%A in (2187 729 243 81 27 9 3 1) do (
   set /A mod=2*%%A
   for %%Z in (!mod!) do (
      if "!s:~%%Z,1!" neq "" (
         set /a "len+=%%Z"
         set "s=!s:~%%Z!"

      ) else (
         if "!s:~%%A,1!" neq "" (
            set /a "len+=%%A"
            set "s=!s:~%%A!"
         )
      )
   )
  )
  endlocal & if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b

8 Comments

similar results to what I have, still missing the sub folder name. I am trying to get rootfolder\subfolder and for every file.
should the csv entry look like this "rootfolder\subfolder\file_no_ext","rootdir" ?
"root\sub\file_no_ext"
I tried this "@ECHO OFF (for /r %%F in (*) do @echo %%~pnF) >filetest.csv" but I am getting "users\initials\Desktop" which I dont need.
to get full path without extension use %%~dpnF
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.