0

我有一个像这样的文件目录:

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

最重要的是,每次我执行时 %PDEN% 似乎都会变大。我需要刷新那个变量吗?

把招工广告!

谢谢大家

4

1 回答 1

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 个文件的日志(最后一个日志可能包含更少)。您可以在此行中更改此数字:set /a logcounter=!filecounter!/25+1

于 2013-04-18T23:48:55.513 回答