我正在尝试为文件列表中的每个文件名添加前缀。如何确定已添加该前缀的文件?

时间:2016-04-28 02:16:37

标签: windows batch-file command-line

我试图通过在文件名中添加前缀来重命名目录中的文件。在这种情况下,我尝试添加的前缀是1-

@echo off
@setlocal enableextensions enabledelayedexpansion
cd %folder%

for  %%a in (*.txt) do (
set var tem=%%a
rem checking file name  contains 1- as prefix
if not x%tem:1-=%==x%tem% (
echo file is renamed all ready
)else(
ren "%%a" "1-%%a"
)
)

预期输出:不应重命名重命名的文件

1 个答案:

答案 0 :(得分:0)

@echo off
setlocal enableextensions enabledelayedexpansion
cd %folder%

for %%a in (*.txt) do (
   rem Set file name to "tem" variable
   set "tem=%%a"
   rem Checking if two first chars of file name are "1-"
   if "!tem:~0,2!" == "1-" (
      echo file is renamed allready
   ) else (
      ren "%%a" "1-%%a"
   )
)