Clipping filespec path

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MvGulik
Posts: 7
Joined: 08 Jun 2011 06:18

Clipping filespec path

#1 Post by MvGulik » 26 Jun 2012 19:05

Hi.
Back again after a serious lack of DOS practice.

Trying to do something in DOS that probably can be done way simpler (in DOS) than what I'm doing now/here.

The Target is to get a "Dir /b/s ... > file.log" output file that has some leading part of the paths removed.
Which part of the path should be removed is based on a given folder name.

Example: If we have the following folders.

Code: Select all

X:\aaa\bbb\xyz\cc1
X:\aaa\bbb\xyz\cc2\dd1
X:\aaa\bbb\xyz\cc3\dd2\ee1
And the target folder name is "xyz".
The target result would be. (leading backslash not important.)

Code: Select all

\xyz\cc1
\xyz\cc2\dd1
\xyz\cc3\dd2\ee1


Seemed simple enough at first. ... But I ended up with this. :?
Its working ... but way to slow to be of any real use. (+1000 files) (assuming the maximum supported path length of 26 folders wringing the speed out of it)

Is there a better way I missed? (or better switch to a other language for this, like python)

Code: Select all

@echo off
setlocal enableDelayedExpansion
set "target=xyz"

::dir /b/s /a/a-d > out1.tmp
:: OR, (depending on how its used)
dir /b/s /a/a-d %target%\ > out1.tmp
echo.>out2.tmp

:: Set num to "1,2,..,26" for token use.
set "num=1" & FOR /L %%A IN (2,1,26) DO set "num=!num!,%%A"

set "A1="
for /F "delims=\ tokens=%num%" %%a in (out1.tmp) do (
   setlocal enableDelayedExpansion
   set "A1=" &rem - clear for new path.
   for %%1 in (%%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y %%z) do (
      if not "%%1"=="" (
         REM ~ echo.1=%%1
         call:myFunc A1, "!A1!", "%%1", "!target!"
      )
   )
   echo.A1=!A1!
   REM ~ if not "!A1!"=="" echo.A1=!A1!&echo.!A1!>>out2.tmp
   endlocal
)
goto:eof

:myFunc - out, in2, in3, in4 ... remark
   SETLOCAL
   set "out="
   if "%~2"=="" (
      if "%~3"=="%~4" set "out=\%~3"
   ) else (
      set "out=%~2\%~3"
   )
ENDLOCAL&set "%~1=%out%"
goto:eof

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Clipping filespec path

#2 Post by Aacini » 26 Jun 2012 19:12

This is the way I would do it:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "target=xyz"
for /D /R %%a in (%target%*) do (
   for /D %%b in ("%%a\*") do (
      set "filename=%%~Fb"
      echo !filename:*%target%=\%target%!
   )
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clipping filespec path

#3 Post by foxidrive » 27 Jun 2012 01:19

Similar but different.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
cd /d "x:\aaa\bbb\xyz"
for /f "delims=" %%a in ("%cd%") do set "folder=%%~nxa"
for /f "delims=" %%a in ('dir /a:-d /b /s') do (
set "var=%%a"
echo !var:*\%folder%=\%folder%!
)

MvGulik
Posts: 7
Joined: 08 Jun 2011 06:18

Re: Clipping filespec path

#4 Post by MvGulik » 27 Jun 2012 01:58

Could not get the code from Aacini adjusted to get it working for me ... before the second code from foxidrive was posted.
(Did not spend that much time on Aacini's code as it was about bed time for me when I got it.)

foxidrive's code: 6sec on 4000 files. That I can use. :)

Thank you, to both.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clipping filespec path

#5 Post by foxidrive » 27 Jun 2012 08:02

Just be aware of the limitations: folders\files with ! and & and possibly % characters are probably an issue.

If you process more than 6000 files (say 100,000) then another issue will arise where the time to read the dir list blows out.
That can be dealt with by doing the dir into a short filename file and then using the filename where the 'dir /switches' command is now.

MvGulik
Posts: 7
Joined: 08 Jun 2011 06:18

Re: Clipping filespec path

#6 Post by MvGulik » 27 Jun 2012 15:33

Roger.
Will move the Dir out of the For command just to be on the save side.
The !,& and % limitation is most likely not going to be a problem in this case. But its good to now.
Added it below the :: *** Courtesy of http://www.dostips.com *** line. ;)

One minor thing (which was not specified in OP) that I run into was that the

Code: Select all

   echo !var:*\%target%=\%target%!>>out1.tmp
part did returned the full path on filespecs that failed. (those that did not contain the target name.)
But a additional

Code: Select all

findstr /B \%target% <out1.tmp >out2.tmp
fixed that.

[+(which ...)]
Last edited by MvGulik on 27 Jun 2012 16:15, edited 1 time in total.

MvGulik
Posts: 7
Joined: 08 Jun 2011 06:18

Re: Clipping filespec path

#7 Post by MvGulik » 27 Jun 2012 16:11

PS: Question about the "dir /a:-d" use.
Why the "/a:-d" and not just "/a-d" ?
Is "/a:-d" a alternative way for writing "/a /a-d" ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clipping filespec path

#8 Post by foxidrive » 27 Jun 2012 19:40

MvGulik wrote:PS: Question about the "dir /a:-d" use.
Why the "/a:-d" and not just "/a-d" ?
Is "/a:-d" a alternative way for writing "/a /a-d" ?


A switch like /a:-d is the syntax used in the help for DIR. It's official. However it doesn't need the :

/a:-d returns all files, and removes directories from the list.

Post Reply