Name by path of file

December 10, 2009 at 06:10:43
Specs: Windows XP
Im trying to copy and rename file by its original path:

%%v - holds the original path of the file (before i copied it to "tmptxt" folder, and its changing.

FOR /F "tokens=* delims=\&." %%A IN ("%%v") DO copy c:\tmptxt\*.txt p:\TXTFiles\%%X%%Y.txt

i would like to name the file this way:
will be:

but the path lenght might change, for example:
will be

the last two folder will alwayes be a number and then a letter.

but i can not find a way to reffer to the right token (in the example i used X and Y - but they will change each time)
any idea?

See More: Name by path of file

Report •

December 10, 2009 at 06:21:12
Does %%v contain the original path of the file, or the pathname?

I.e. is it "C:\Roads\Test\4963\a\" (path)
or "C:\Roads\Test\4963\a\1.txt" (pathname)

If it's path, does it end with a backslash?

Report •

December 10, 2009 at 06:25:16

it includes the path the file name and the extension.

Report •

December 10, 2009 at 07:30:51
Try this:

@echo off
setlocal EnableDelayedExpansion

rem For pathname "C:\a\b\c\...\y\z\1.txt", retrieve y and z.
rem Use %%~pA to get the path component of %%A, then use !VAR:~,-1! to remove
rem the trailing backslash. Start with the variable %%v containing the
rem pathname from which we need to extract the last two components.

for %%v in ("C:\a\bb\ccc\dddd\1.txt") do (
   set tmpPathname=%%~pv
   for %%w in ("!tmpPathname:~,-1!") do (
      set NamePart2=%%~nw
      set tmpPathname=%%~pw
      for %%w in ("!tmpPathname:~,-1!") do (
         set NamePart1=%%~nw
   echo The name parts relating to %%v are as follows:
   set NamePart

Report •

Related Solutions

December 11, 2009 at 05:56:16
Even better:

for %%v in ("C:\Roads\Test\4963\a\1.txt") do (
   for %%a in ("%%v\..") do (
      for %%b in ("%%~dpa.") do (
         echo %%~nxb-%%~nxa.txt

Thanks to Razor3.2 for pointing out this technique in this thread.

Report •

Ask Question