我有一个像这样的文件目录:
PDEN51505_1_1_N611729_15894285.XML
PDEN51505_1_2_N611729_15894286.XML
PDEN51505_2_1_N599021_15746279.XML
我需要为这些文件创建一个自定义日志文件,最终看起来像这样:
462121 14105618 PD EN 97240 1 97240 RF PDEN51505_1_1_N611729_15894285 XML C N N 0
462121 14105618 PD EN 97240 1 97240 RF PDEN51505_1_2_N611729_15894286 XML C N N 0
462121 14105618 PD EN 97240 1 97240 RF PDEN51505_2_1_N599021_15746279 XML C N N 0
我不是每次都修改自定义日志文件中的各个行,而是创建一个自动化过程来为我构建它。以下是我到目前为止的情况:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
MODE con: cols=125 lines=50
SET /P WHERE=Where are the XMLs located (full file path)? (default: %CD%)
IF "%WHERE%"=="" (
SET WHERE=%CD%\
) ELSE (
IF "!WHERE:~-1!"=="\" (
SET WHERE=!WHERE!
) ELSE (
SET WHERE=!WHERE!\
)
)
:: TRAVERSE TO DIRECTORY THE USER JUST INPUT
CD %WHERE%
:: CREATES AN TEMP OUTPUT FILE OF THE DIRECTORY (MINUS EXTENTIONS)
FOR /f "tokens=*" %%a in ('dir /b /a-d *.xml') DO ECHO %%~na>>output.txt
:: CLEARS OUT "PDEN" VARIABLES
FOR /f "delims==" %%i IN ('set PDEN 2^>nul') DO (SET %%i=)
:: CREATES A LISTING SEPERATED BY SPACES
FOR /f "tokens=*delims=_" %%a IN (output.txt) DO (
SET PDEN=!PDEN! %%a
)
:: ATTEMPTS TO GENERATE A LOG FILE
FOR /f "tokens=*delims=" %%i IN ('set PDEN 2^>nul') DO (
>%WHERE%PDEN_GEN_LOG.log ECHO 462121 14105618 PD EN 97240 1 97240 RF !PDEN! XML C N N 0
ECHO [INFO] Generated PDEN_GEN_LOG.log
)
:: PROMPTS USER IF THEY WANT TO SEE THEIR CREATION
ECHO FINISHED
ECHO A new control file has been saved to %WHERE%
SET /P FILESURE=Would you like to view the LOG file you created?(Y/[N])?
IF /I "%FILESURE%" NEQ "Y" GOTO REMOVE
notepad.exe %WHERE%PDEN_GEN_LOG.log
:: REMOVES THE OUTPUT.TXT FILE
:REMOVE
RM %WHERE%output.txt
exit
它创造了一切正确的创造。它将以前在output.txt中生成的列表放在一行上,所以现在我的日志文件看起来像这样:
462121 14105618 PD EN 97240 1 97240 RF PDEN51505_1_1_N611729_15894285 PDEN51505_1_2_N611729_15894286 PDEN51505_2_1_N599021_15746279 XML C N N 0
最重要的是,每次执行.bat时,%PDEN%的大小似乎都在增长。我需要刷新该变量吗?
需要帮助!
谢谢你们
答案 0 :(得分:1)
我的建议,试试这个:
@ECHO OFF & setlocal enabledelayedexpansion
MODE con: cols=125 lines=50
set "where=%cd%"
SET /P "WHERE=Where are the XMLs located (full file path)? (default: %CD%) "
IF "%WHERE%"=="" SET "WHERE=%CD%\"
IF not "%WHERE:~-1%"=="\" SET "WHERE=%WHERE%\"
:: TRAVERSE TO DIRECTORY THE USER JUST INPUT
CD "%WHERE%"
:: delete Logfiles
DEL PDEN_GEN_LOG_*.log >nul 2>&1
:: CREATES A LISTING
set /a filecounter=0
FOR /f "delims=" %%a in ('dir /b /a-d /on *.xml') DO (
set /a logcounter=!filecounter!/25+1
>>PDEN_GEN_LOG_!logcounter!.log ECHO 462121 14105618 PD EN 97240 1 97240 RF %%~na XML C N N 0
set /a filecounter+=1
)
ECHO [INFO] Generated PDEN_GEN_LOG_1.log to PDEN_GEN_LOG_%logcounter%.log
:: PROMPTS USER IF THEY WANT TO SEE THEIR CREATION(S)
ECHO FINISHED
ECHO New control file(s^) created in %WHERE%
SET /P "FILESURE=Would you like to view the LOG file(s^) you created?(Y/[N])? "
IF /I "%FILESURE%" NEQ "Y" exit
for /l %%i in (1,1,%logcounter%) do start "" "notepad.exe" PDEN_GEN_LOG_%%i.log
exit
这使得每25个文件的LOG(最后一个日志可能包含更少)。您可以在此行中更改此数字:set /a logcounter=!filecounter!/25+1
。