Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

variables - name files after template bat file - used CMD code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I replaced the user input with:

set infovariable=%~n0

which safes the filename of the bat file as a variable and uses it later to rename appropriate files with it. The bat file is named exactly what I want as a text. Job done!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...