-1

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

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

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

任何帮助是极大的赞赏。

我的代码如下:

@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
4

1 回答 1

2

啊啊啊!

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

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

几点注意事项:

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