简体   繁体   English

如何制作批处理日志文件?

[英]How do I make a batch log file?

How do I make a log file from this code. 如何从此代码创建日志文件。 This has been answered a ton, but this file is huge and I want a simple log file. 这已经回答了很多,但这个文件很大,我想要一个简单的日志文件。 As you see I also tried to add a log file names RandomNamesLog.log. 如您所见,我还尝试添加日志文件名RandomNamesLog.log。 See if the output goes there. 看看输出是否存在。

I want all the code to be outputted into the log file, with error messages and confirm messages, like removing the @ECHO OFF value. 我希望将所有代码输出到日志文件中,包含错误消息和确认消息,例如删除@ECHO OFF值。 If you need more on what I want, please tell me. 如果你需要更多我想要的东西,请告诉我。

@ECHO OFF
SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#" %%a in 
('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set 
"DEL=%%a" )
ECHO Random Names
ECHO Written By: Trevor T
ECHO UltimateGuidesPro.blogspot.com
ECHO.
ECHO.

REM Randomly renames every file in a directory.

SETLOCAL EnableExtensions EnableDelayedExpansion

REM 0 = Rename the file randomly.
REM 1 = Prepend the existing file name with randomly generated string.
SET PrependOnly=0

REM 1 = Undo changes according to the translation file.
REM This will only work if the file "__Translation.txt" is in the same 
folder.
REM If you delete the translaction file, you will not be able to undo 
the changes!
SET Undo=0


REM --------------------------------------------------------------------------
REM Do not modify anything below this line unless you know what you are doing.
REM --------------------------------------------------------------------------

SET TranslationFile=_TranslatonData.txt
SET LogFile=_RandomNamesLog.log

IF NOT {%Undo%}=={1} (
REM Rename files
ECHO You are about to randomly rename every file in the following folder:
ECHO %~dp0
ECHO.
ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
    ECHO.
    ECHO Aborting.
    GOTO :EOF
)

ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%

FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
    IF NOT %%A==%~nx0 (
        IF NOT %%A==%TranslationFile% (
            SET Use=%%~xA
            IF {%PrependOnly%}=={1} SET Use=_%%A

            SET NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!
            ECHO %%A/!NewName!>> %TranslationFile%

            RENAME "%%A" "!NewName!"

   ECHO ----------------------------------------------------- >> %TranslationFile%
   ECHO Designed by Trevor T | UltimateGuidesPro.blogspot.com >> %TranslationFile%
   ECHO. >> %TranslationFile%
   ECHO WARNING: THIS FILE REQUIRED TO UNDO RANDOM NAMING! THE FILE CAN BE MOVED AS LONG AS IT IS IN THE FOLDER WHEN USING THE UNDO RANDOM NAMES FUNCTION! >> %TranslationFile%
 ECHO +---------+
 ECHO | Credits |  
 ECHO +---------+
 Echo.
 ECHO Original Random Names script created by
 call :colorEcho 0a "Jason Faulkner"
 call :colorEcho 0a "HowToGeek.com"
 ECHO ---------------------------------------
 ECHO Script created by
 call :colorEcho 0a "Trevor T"
 ECHO -------------------------------------------
 ECHO Color Text system created by
 call :colorEcho 0a "Visual Magic, user on Stack Exchange"
 ECHO Type "Exit" to quit
 GOTO :Exit
 :Exit
 SET /P Confirm=
IF /I NOT {!Confirm!}=={Exit} (
    ECHO.
    ECHO Please try again
    GOTO :Exit2
Exit 
 :Exit2
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
    ECHO.
    ECHO Please try again
    GOTO :Exit

 )
    )
)
) ELSE (
ECHO UNDO MODE
IF NOT EXIST %TranslationFile% (
    ECHO Missing translation file: %TranslationFile%
ECHO Please make sure %TranslationFile% is in the location of the random files. Otherwise it will give this error.
    PAUSE
    GOTO :EOF
)
ECHO Undoing...
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile% 
ECHO Completed Undo!
Pause
)
Exit
REM This is the color text code. Don't touch this, unless you know a better way to use color text.
:colorEcho 
echo off 
<nul set /p ".=%DEL%" > "%~2" 
findstr /v /a:%1 /R "^$" "%~2" nul 
del "%~2" > nul 2>&1i

I have rewritten your script fixing all of the problems I could see and removing the pointless code to add colored text. 我已经重写了你的脚本,修复了我能看到的所有问题,并删除了无意义的代码来添加彩色文本。

Test it to see if it works as it should adding whatever you think I've missed. 测试它是否有效,因为它应该添加你认为我错过的任何东西。

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

ECHO Random Names
ECHO Written By: Trevor T
ECHO UltimateGuidesPro.blogspot.com
ECHO.

REM Randomly renames every file in a directory.

REM 0 = Rename the file randomly.
REM 1 = Prepend the existing file name with randomly generated string.
SET "PrependOnly=0"

REM 1 = Undo changes according to the translation file.
REM This will only work if the file "_TranslationData.txt" is in the same folder.
REM If you delete the translaction file, you will not be able to undo the changes!
SET "Undo=0"

REM --------------------------------------------------------------------------
REM Do not modify anything below this line unless you know what you are doing.
REM --------------------------------------------------------------------------

SET "TranslationFile=_TranslationData.txt"
IF "%Undo%"=="1" GOTO UNDOIT

ECHO You are about to randomly rename every file in the following folder:
ECHO %CD%
ECHO.
CHOICE /M "Do you want to continue "
IF NOT %ERRORLEVEL%==1 (
    ECHO.
    ECHO Aborting.
    TIMEOUT -1
    GOTO :EOF
)

ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
(
    ECHO ;Original Name/Random Name
    ECHO ;-------------------------
    FOR %%A IN (*) DO IF NOT "%%A"=="%~nx0" IF NOT "%%A"=="%TranslationFile%" (
        SET "Use=%%~xA"
        IF "%PrependOnly%"=="1" SET "Use=_%%A"
        SET "NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!"
        ECHO %%A/!NewName!
        REN "%%A" "!NewName!"
    )
    ECHO ;-----------------------------------------------------
    ECHO ;Designed by Trevor T ^| UltimateGuidesPro.blogspot.com
    ECHO.
    ECHO ;WARNING: THIS FILE REQUIRED TO UNDO RANDOM NAMING!
    ECHO ; THE FILE CAN BE MOVED AS LONG AS IT IS IN THE FOLDER WHEN USING THE UNDO RANDOM NAMES FUNCTION!
)>"%TranslationFile%"

ECHO +---------+
ECHO ^| Credits ^|  
ECHO +---------+
ECHO.
ECHO Original Random Names script created by Jason Faulkner @ HowToGeek.com
ECHO -------------------------------------------
ECHO Script created by Trevor T
ECHO -------------------------------------------
TIMEOUT -1
GOTO :EOF

:UNDOIT
ECHO UNDO MODE
IF NOT EXIST "%TranslationFile%" (
    ECHO Missing translation file: %TranslationFile%
    ECHO Please make sure %TranslationFile% is in the location of the random files. Otherwise it will give this error.
    TIMEOUT -1
    GOTO :EOF
)
ECHO Undoing...
FOR /F "USEBACKQ TOKENS=1-2 DELIMS=/" %%A IN ("%TranslationFile%") DO REN "%%B" "%%A"
DEL /F "%TranslationFile%" 
ECHO Completed Undo!
TIMEOUT -1
GOTO :EOF

Once you are happy with it edit your opening question explaining what it is you want to have in the log file and how you need it to look. 一旦您对它感到满意,请编辑您的开头问题,解释您希望在日志文件中包含的内容以及您需要它的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM