Need some help with a BAT script (1 Viewer)

Register Today to see less ads! It's Free!
Status
Please reply by conversation.

cpalmer2k

SatelliteGuys Pro
Lifetime Supporter
Oct 18, 2013
892
439
United States
OK I know we have to have some old DOS users/programmers among us. Every year I'm tasked with the job of doing a photo slideshow for our school. Many years ago I found a batch file online that randomly renames every file in a directory. We've been using it for the slideshow for many years, but one annoying feature is it requires you to enter "OK" before it will proceed with the renaming. I want to remove the "OK" requirement and just have it execute and rename automatically but I cannot for the life of me get it to work. Anyone want to tell me what I'm doing wrong? I "DO" want to keep the part that creates the translation file for undoing though, as we usually put everything back after the slideshow.

Code:
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=__Translation.txt

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!"
            )
        )
    )
) ELSE (
    ECHO Undo mode.
    IF NOT EXIST %TranslationFile% (
        ECHO Missing translation file: %TranslationFile%
        PAUSE
        GOTO :EOF
    )
    FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
    DEL /F /Q %TranslationFile%
)
 
Register Today to see less ads! It's Free!

gms49ers

Die hard Rush & 49ers Fan
Pub Member / Supporter
May 9, 2008
3,550
73
Boise, ID
You'll want to remove from the code this section:

ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)


I personally prefer to put REM in front of each line rather than deleting them.
 

harshness

SatelliteGuys Master
May 5, 2007
16,825
2,879
Salem, OR
Code:
   ECHO Type "OK" to continue.
   SET /P Confirm=
   IF /I NOT {!Confirm!}=={OK} (
       ECHO.
       ECHO Aborting.
       GOTO :EOF
   )
Looking at the prompt, it is obvious this is the code that looks for you to type OK and then deals with your answer.

The SET command with the /Pause flag waits for you to type in the value of Confirm. The first two lines should have been combined: SET /P Confirm=[Type "OK" to continue] but that's probably how the script evolved from DOS.

The /I switch with the IF command makes the string comparison case insensitive. The braces are the string delimiters and the exclamation points are some dark art to avoid/enable stripping special characters.

The NOT part tells the parser to execute the code in parens if the answer isn't OK, ok, Ok or oK bypassing all the functional code (GOTO :EOF).

Removing this entire section of the code will accomplish uninterrupted execution of the batch script.
 
Register Today to see less ads! It's Free!

cpalmer2k

SatelliteGuys Pro
Lifetime Supporter
Oct 18, 2013
892
439
United States
You'll want to remove from the code this section:

ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)


I personally prefer to put REM in front of each line rather than deleting them.

Thank you! I had tried that and multiple other combinations but missed the last line with the ). That was my problem!
 
Register Today to see less ads! It's Free!
Status
Please reply by conversation.

Users who are viewing this thread

Users Who Are Viewing This Thread (Total: 0, Members: 0, Guests: 0)

Top