I have a *.bat file in various folders which renames certain files so project number, project name and initials are included in the filename. So far running the file will ask the user to enter project number, project name and initials which are saved as variables (no, project and initial). Each original filename is kept but in brackets will be the required projects specific data). So far it works but what I would like is set up a batch file where the name sets the template for the project specific data e.g.filename of bat file could be:
Rename <XY00.000> (Projectname),XYZ.bat
If in CMD code you could retrieve the filename of the executable bat file (should be only 1 in the folder) and then with delimiters you would splice the filename and store the relevant strings in different variables e.g. what is inside <> will be variable "no", whats inside () will be variable "project" and what is past the , is "initial". If I have those variables the file renaming is as in my current procedure. Please help as I'm not familiar with CMD code or Powershell (which might be alternative option) and it took me long to assemble the existing code. Basically I want to change the input part (see set /p) to something which does:
- get filename of bat file
- extract 3 text strings from the filename (use of delimiters like ( < ,)
- store the 3 text strings as parameters (NO, PROJECT , INITIAL)
- the rest of the existing code should then work to rename the files in the folder
Thanks.
Existing code as example below:
cls
echo off
pushd "%~p0" 2> nul
pushd "\%~p0" 2> nul
echo
attrib -r *.* /s
set /p no= Enter Project Reference in style XY00.000:
set /p project= Enter Project name (without any underscores):
set /p initial= Enter your Initials:
for /f "tokens=1* delims=(" %%i in ('dir /b *.xlsm') do ren "%%i(%%j" "%%i(%no% %project%_%initial%).xlsm"
for /f "tokens=1,2* delims=()" %%i in ('dir /b *.docm') do ren "%%i(%%j)%%k" "%%i(%no% %project%_%initial%)%%k"
question from:
https://stackoverflow.com/questions/66050234/name-files-after-template-bat-file-used-cmd-code 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…