cmd将文本插入文件名的中间 [英] cmd insert text into the middle of the file name

查看:184
本文介绍了cmd将文本插入文件名的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文件从一个目录移动到新创建的目录。当前脚本将id变量插入到所有文件名的文件名的开头。
我想在所有文件名中插入模型名变量之后的id变量。

I am moving files from one directory to a newly created directory. Currently the script inserts the id variable to the start of the file name for all the file names. I would like to insert the id variable after model name variable in all the file names. How do I do that?

我在cmd文件中有以下代码。

I have the following code in my cmd file.

@echo off

rem set 12d variables
set modelname=dr network_
set id=10yr_

mkdir "%modelname%"

rem move to new dir created
move "%modelname%*.rpt" "%modelname%"

rem change working directory
cd "%modelname%"

rem for each file rename it and add the new prefix %id% to the file name 
for %%f in ("%modelname%*.rpt") do (

    rename "%%f" "%id%%%f"
)


推荐答案

一些小的改动,应该让你去。

A few small alterations, should get you going.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

rem set 12d variables
set modelname=dr network_
set id=10yr_

mkdir "%modelname%"

rem move to new dir created
move "%modelname%*.rpt" "%modelname%"

rem change working directory
cd "%modelname%"

rem Work out how long the modelname is
echo %modelname%>strlen.txt
for %%a in (strlen.txt) do set /a modelNameLength=%%~za - 2
del strlen.txt

rem for each file rename it and add the new text %id% to the middle of the file name 
for %%f in ("%modelname%*.rpt") do (
    set newfilename=%%f
    set newfilename=!newfilename:~0,%modelNameLength%!%id%!newfilename:~%modelNameLength%!
    ren "%%f" "!newfilename!"
)

这篇关于cmd将文本插入文件名的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆