Batch add string before filename

回眸只為那壹抹淺笑 提交于 2019-12-20 05:03:11

问题


I'm working on a Windows batch file and I need to change name of files in current direcotry.

I have these files:

file1.txt
file2.txt
file3.txt

and I need to add string "REG~" before each filename like this

REG~file1.txt
REG~file2.txt
REG~file3.txt

Thank you.


回答1:


@echo off    
SETLOCAL ENABLEDELAYEDEXPANSION    
SET old=file  
SET new=REG~file   
for /f "tokens=*" %%f in ('dir /b *.txt') do (    
    SET newname=%%f    
    SET newname=!newname:%old%=%new%!    
move "%%f" "!newname!"    
)

What this does is it loops over all .txt files in the folder where the batch file is located and replaces the file with December inside the filenames.




回答2:


Run this in a folder with .txt files in it. Use test files first.

@echo off
for %%a in (*.txt) do ren "%%a" "REG~%%a.tmp"
ren *.tmp *.



回答3:


For anyone who stumbles on to this, just one tweak to this code - add "txt" (or the appropriate file extension) to the very end:

@echo off
for %%a in (*.txt) do ren "%%a" "REG~%%a.tmp"
ren *.tmp *.txt


来源:https://stackoverflow.com/questions/21674341/batch-add-string-before-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!