我有一个批处理脚本循环遍历目录中的文件,如果文件匹配四种模式中的一种,则会重命名该文件。当我在我的测试环境中运行它时,它运行得很好。但是,当我自豪地运行它时,它只重命名它遇到的第一个文件。我从我的电脑上对映射驱动器上的目录运行它们。
我认为问题可能与我正在搜索的包含“+”符号的文件名有关,但是当我在测试中运行脚本时,这不会造成问题。
我要搜索的目录的文件路径作为参数传递。这是唯一的参数。
非常感谢任何帮助。
我的代码如下:
@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
答案 0 :(得分:2)
Aaaaack!
您的主要问题是您在FOR / F ... %% f循环中使用GOTO。
GOTO End
。 GOTO RENAME
更改为CALL :RENAME
(冒号是重要的 - 它指定调用内部例程)GOTO :EOF
语句GOTO LOOP
GOTO :EOF
一些注意事项:
GOTO LOOP
/ REMs / :LOOP
是多余的)SET var=%1%
是不正确的(但不是致命的)正确的形式是SET var=%1