2017-04-14 85 views
1

如何從此代碼創建日誌文件。這已被回答了一噸,但這個文件是巨大的,我想要一個簡單的日誌文件。正如你所看到的,我也嘗試添加一個日誌文件名稱RandomNamesLog.log。看看輸出是否到達那裏。如何製作批處理日誌文件?

我希望將所有代碼輸出到日誌文件中,並帶有錯誤消息和確認消息,如刪除@ECHO OFF值。 如果你需要更多我想要的東西,請告訴我。

@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 

回答

0

我已經重寫了您的腳本,修復了我可以看到的所有問題,並刪除了無意義的代碼以添加彩色文本。

測試它,看它是否工作,因爲它應該添加你認爲我錯過了的任何東西。

@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 

一旦你滿意的話編輯您開放問題解釋你想要的是有日誌文件,以及如何需要看。

+0

謝謝@compo!這真的有幫助 – AwesomeTech10

相關問題