【问题标题】:Batch Script to Rename Files Not Working (Sometimes)重命名文件的批处理脚本不起作用(有时)
【发布时间】:2013-03-11 16:02:56
【问题描述】:

我有一个批处理脚本,它循环遍历目录中的文件,如果它与四种模式之一匹配,则会重命名该文件。当我在我的测试环境中运行它时,它可以完美运行。但是,当我自豪地运行它时,它只会重命名它遇到的第一个文件。我从我的电脑上针对映射驱动器上的目录运行它们。

我认为问题可能与我正在搜索的包含“+”号的文件名之一有关,但是当我在测试中运行脚本时这不会造成问题。

我要搜索的目录的文件路径作为参数传递。它是唯一的参数。

非常感谢任何帮助。

我的代码如下:

@ECHO OFF
SetLocal EnableDelayedExpansion
REM ******************************************************************************************
REM 
REM            Name: get_current_foldername.bat
REM   Description: This script will rename generic loan review sample file names such as CRE.xls, 
REM                Family.xls, etc. to [docket]_[yyyymmdd]_[hhmmss]_[list type (cre, fam1-4, etc.)].xls.
REM                Ex. 13346_20121220_125930_cre.xls.  
REM                This script is used by Globalscape as part of the completed loan review sample upload.
REM    Parameters: There is one parameter passed.  
REM                The first parameter contains the path where the source file exists.
REM          Author: Joe Mannetta
REM            Date: 01/28/13
REM 
REM ******************************************************************************************
REM ******************************************************************************************
REM 
REM  Set CurrPath variable to parameter value.
REM  Set CurrDirName variable to the current directory name.  This excludes higher level folders.
REM 
REM ******************************************************************************************
SET CurrPath=%1%
FOR %%* in (%CurrPath%) DO (SET CurrDirName=%%~n*)
REM ECHO Current Path is: %CurrPath%
REM ECHO New DIR NAME is: %CurrDirName%
REM ******************************************************************************************
 CD %CurrPath% 
 REM ECHO CurrPath is %CurrPath%
 GOTO LOOP
 REM *****************************************************************************************
 REM LOOP Function
 REM Loops through files in directory and go to Rename function if the file name matches CRE.xls, FAMILY.xls, FAMILY5+.xls, or HELOCS.xls.
 REM *****************************************************************************************
 :LOOP
REM Get name of 1st file in list. Note this also returns the full file path.
 FOR /F "delims= tokens=1" %%f in ('dir /b *.xls') DO ( 
REM FOR /F "tokens=1" %%f IN ('dir /o-d /b %CurrPath%') DO ( 
  SET File=%%f
 ECHO File is: %%f
 IF %%f==CRE.xls GOTO RENAME
 IF %%f==FAMILY.xls GOTO RENAME
 IF %%f==FAMILY5+.xls GOTO RENAME
 IF %%f==HELOCS.xls GOTO RENAME
 REM ECHO %%f
 REM ECHO %ERRORLEVEL%
 GOTO END
REM ECHO Did NOT Make it to GOTO
 )
REM ******************************************************************************************
REM RENAME Function
REM Renames the file to [folder name]_[yyyymmdd]_[hhmmss]_[CRE|FAMILY|FAMILY5+|HELOCS].xls
REM  Date is reformatted to yyyymmdd.  Time is reformatted to hhmmss.  Some credit due to Jimmy Selix at http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/ for date and time formatting.
REM ******************************************************************************************
:RENAME
REM End loop so that File variable is set to the newest file.   
  REM Drop file path to isolate the file name.
  SET CurrFName=!File:%CurrPath%=!
  REM ECHO CurrFName is %CurrFName%
REM Set date to yyyymmdd format.
 SET dt=!date:~10,4!!date:~4,2!!date:~7,2!
REM Set time to hhmmss format. Include leading zeros for single digit times.
 SET mytime=%TIME: =0%
 SET hhmmss=!mytime:~0,2!!mytime:~3,2!!mytime:~6,2!
 SET NewFName=!CurrDirName!_!dt!_!hhmmss!_!CurrFName!
REM echo !CurrPath!\!CurrFName! !CurrPath!\!NewFName!
REM Set new file name and rename file.
 COPY "!CurrPath!\!CurrFName!" "!CurrPath!\!NewFName!"
 COPY "!CurrPath!\!NewFName!" "!CurrPath!\..\..\archive"
 DEL /Q !CurrPath!\!CurrFName!
 REM ECHO %ERRORLEVEL%
 GOTO LOOP
 REM *******************************************************************************************
 :END

【问题讨论】:

  • 有很多代码需要挖掘。你能把它缩小到导致问题的部分吗?如果我们只需要阅读十几行代码,您就有更多机会找到愿意提供帮助的人。经验法则:如果你的代码块需要滚动条,那可能是代码太多了。

标签: file for-loop batch-file special-characters rename


【解决方案1】:

啊啊啊!

您的主要问题是您在 FOR/F...%%f 循环中使用 GOTO。

  • 彻底删除GOTO End
  • 将每个 GOTO RENAME 更改为 CALL :RENAME(冒号很重要 - 它指定调用内部例程)
  • 在 FOR/f...%%f 循环的最后一个右括号后添加 GOTO :EOF 语句
  • GOTO :EOF 替换:RENAME 例程中的GOTO LOOP

一些注意事项:

  • Batch 不会将标签视为过程结束,例如 Pascal。它只是通过...充电(因此GOTO LOOP/REMs/:LOOP 是多余的)
  • EOF 预定义为“物理文件结尾”,因此不需要 :END 标签。到达文件末尾将终止批处理或导致 CALL 返回
  • SET var=%1% 是不正确的(但不是致命的)正确的形式是 SET var=%1
  • %%* 有效,但不是“官方” - 为什么不使用已记录的 26 个小写字母和 26 个大写字母之一?
  • RENAME 是一个糟糕的例程名称选择,因为 REN 命令是 RENAME 的替代命令(即它是一个批处理关键字)

【讨论】:

  • 彼得,感谢您的批评。我会做出你推荐的改变,并让你知道它是如何进行的。正如您可能知道的那样,我绝不是编写批处理文件的专家。
  • 这并没有解决问题,但它确实帮助我找出了问题所在。由于某种原因,文件列表没有被保留。脚本循环遍历文件列表,找到匹配项并重命名,但随后再次执行 FOR 循环的 dir 命令。由于重命名的文件位于列表的顶部,它只是不断循环执行例程并重新开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多