0

[I have searched for a similar question but I did not find what I was looking for, so I am posting a new one.]

I want in my .bat file to SET a directory in a variable and I want to find all the files inside this directory (which all start with the same name) and rename them according to this pattern.

e.g file_location1.wml   -> file1.wml
    file_location2.wml   -> file2.wml
    file_location3.wml   -> file3.wml

Till now I am trying something like this:

    SET counter=1
    FOR /R %%f IN (C:\Documents\testFiles\*.*) DO (
        FOR /F "DELIMS=_ TOKENS=1,*" %%m IN ("%%~nxf") DO ( 
            REN "%%f" "%%m%counter%" 
            SET counter=counter+1
        }
    }

It does not work. What am I doing wrong? I specify that i want to iterate over the files and rename each one with the first token + the counter's value.

azal
  • 1,210
  • 6
  • 23
  • 43
  • The question is a little ambiguous (at least in terms of source and target names). `file_locationX` -> `fileX`: is there a connection between the 2 __X__ es? Is there a real (and general) example of location names and desired file names? A quick possible answer to your question (_what to do here?_) would be: `file!counter!.wml` (you'll have to also `setlocal enabledelayedexpansion` at the beginning of your _.bat_). Also __note__ `set params_dir` vs `%params_directory%` in the code snippet. – CristiFati Jun 13 '16 at 12:34
  • location is just a placeholder text. I just want to keep the name before the underscore but append the number at the end – azal Jun 13 '16 at 12:42
  • Possible duplicate of [Renaming a batch file and keeping part of the filename](http://stackoverflow.com/questions/27806085/renaming-a-batch-file-and-keeping-part-of-the-filename) – Ken White Jun 13 '16 at 12:54
  • no, it's not. I already checked it out. – azal Jun 13 '16 at 14:40
  • take a look to [delayed expansion](http://stackoverflow.com/a/30284028/2152082) (basically, what CristiFati tried to explain) – Stephan Jun 13 '16 at 14:55
  • i set the flag as said there and I use ! instead of $ in the code snippet above, but still nothing happens . – azal Jun 13 '16 at 15:00

0 Answers0