1

I need a batch file that renames my files in according to the folder name.

For example I have this folder:

E:\PROGET\01_Progetti\1_TRATTATIVE\IT.16.9291_Fabbricato ad Milano (MI) - Ing. Bianchi\03-CALCOLO\02-XLS\

Which contains

CB-Tech_92XX - .xls
Punz_92XX - .xls

I want to rename them to

CB-Tech_9291 - .xls
Punz_9291 - .xls

Is it possible?

EDIT:

I've found this code from a guy who asked for code and didn't get any complain Rename all files in a directory with a Windows batch script

I've changed it a little bit:

@echo off
setlocal enableDelayedExpansion
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=9XXX!"
)
@pause

Now I just have to understand how to get the path (done), extract only the numbers (not yet) and store in a variable (done).

To set a variable should be something like that

set "number=projectnumber"
SET mypath=%~dp0

ok now I've the path, need to extract only 4 characters after the IT.16.

Will edit later :)

EDIT 2:

@echo off
setlocal enableDelayedExpansion

SET mypath=%~dp0

set projectnumber=%mypath:~41,4%


for %%F in (*92XX*) do (
  set "name=%%F"
  ren "!name!" "!name:92XX=%projectnumber:~0%!"
)


@pause

YEAH! This works for this specific folder!!

Now I just need to understand how to search and extract the number inside the path to make it more general.

I'm looking for a function that returns the position of the first char of the string IT.16. Any advice?

Community
  • 1
  • 1
Dennis Grasso
  • 123
  • 1
  • 2
  • 11
  • Wow, you're so funny! – Dennis Grasso Oct 13 '16 at 16:08
  • I simply wanted to show how "specific" your question is; please read the help topic [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – aschipfl Oct 13 '16 at 17:27
  • You simply wanted to troll me. You don't understand a question of 2 lines? Don't answer. What's the purpose of downvote me? You could write "can you please be a little bit more specific?" good night sir and thanks for your contribution – Dennis Grasso Oct 13 '16 at 18:01
  • Please note that https://stackoverflow.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – DavidPostill Oct 13 '16 at 19:53
  • guys I've always include my code in all my other post. Please stop hate people without any reason. If you can't give me an advice, don't do it. I'm a newbie in batch programming, that's the reason I ask for a code that requires 1 row of code. Thanks anyway, you are very polite. Cheers – Dennis Grasso Oct 13 '16 at 21:39
  • http://stackoverflow.com/questions/7005951/batch-file-find-if-substring-is-in-string-not-in-a-file @aschipfl I'm looking there for getting a solution for the last part of my script.. Fortunately the guy who answered isn't you! Or all we could find is "YES, THERE IS A SOLUTION!" Cheers – Dennis Grasso Oct 14 '16 at 10:54
  • 1
    The questions you are referring to are about 5 years old, the rules for this site changed a lot since then. Anyway, you edited your post and showed your own efforts, so stay cool, everything is fine... Let us stick to the technical stuff now: instead of extracting the project number from the whole path string, I would do that in two steps: 1. `set "projectnumber=%mypath:*IT.16.=%"`, 2. `set "projectnumber=%projectnumber:~,4%"`; that way the path can be anything and the number will still be got correctly (as long as there is only one occurrence of `IT.16.`)... – aschipfl Oct 14 '16 at 11:42
  • Thank you very much! Now the code is complete! I'll try my best to write a good question next time :) Sorry for everything.. – Dennis Grasso Oct 14 '16 at 14:13

1 Answers1

0

This is the complete solution of this question:

@echo off
setlocal enableDelayedExpansion

SET mypath=%~dp0  
set "projectnumber=%mypath:*IT.16.=%"
set "projectnumber=%projectnumber:~,4%"

for %%F in (*92XX*) do (
  set "name=%%F"
  ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
Dennis Grasso
  • 123
  • 1
  • 2
  • 11