Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch Script- padding a “0” into script as well as few bug fixes  (Read 2646 times)

0 Members and 1 Guest are viewing this topic.

lkenn

    Topic Starter


    Newbie

    • Experience: Experienced
    • OS: Windows 7
    Currently there are a few problems I am experiencing with the batch file I created.:

    1) i been trying to implement padding to the file versions from 02-09 and for it then to resume like 10, 11 etc... for example the file created could be called delivery_20-11-17_2.txt but i need it to have padding so it will be delivery_20-11-17_02.txt when it versions up. if it gets to version 10 it will need to stay like delivery_20-11-17_10.txt.

    2) When a new file is created it will save it like delivery_2017-11-09_.txt. For which there is an unnecessary underscore for the first version of the file at the end of it. i need that underscore to start once the files start versioning up from version 2 and not from the very first file created.

    3) In the section of the code where i have the option to name a file there is also a bug which i'm unsure on how to resolve. For example i create the file and i choose to update filename and name it "cat" it willl be saved as delivery_2017-11-09_cat_02.txt. However I want this cat file to be delivery_2017-11-09_cat.txt at first. then if i choose to update filename and name it cat again a version 2 will be created delivery_2017-11-09_cat_02.txt. which is ok.

    4) when i chose the option of "update version" a file would be saved like delivery_2017-11-09__2. a double underscore appears before the version number. I need there to be only 1 underscore their. I understand that this relate to a bit of question number 2.

    Here is the code below in full:

    Code: [Select]
    @echo off

    :prep
    cls
    set filename=
    set mydate=
    set newname=
    set version=1
    For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%b-%%a)
    set choice=

    set filename="../delivery_%mydate%_%newname%.txt"
    ::set filename="../delivery_%mydate%_%version%.txt"
    echo %filename%
    pause

    :_CHECKEXIST
    cls
    IF EXIST %Filename% (
        GOTO _MAINMENU
    ) Else (
        GOTO start
    )
    :_MAINMENU

    ECHO.
    ECHO Choose on of the the following options:
    ECHO 1.   Update filename
    ECHO 2.   Update version
    ECHO 3.   Quit
    ECHO.
    SET /P Choice="Enter your selection. [1,2,3]: "
    IF /I "%Choice%"=="1" GOTO addname
    IF /I "%Choice%"=="2" GOTO versionup
    IF /I "%Choice%"=="3" (
        GOTO _END
    ) ELSE (
        CLS
        GOTO _MAINMENU
    )
    :addname
    echo Enter addition to file name
    set /p newname=
    set filename="../delivery_%mydate%_%newname%.txt"
    echo File name is now %filename%.
    ::set choice3 =
    ::set /p choice3=(y / n)

    ::if '%choice3%'=='' ECHO "%choice3%" is not valid please try again

    ::if '%choice3%'=='y' goto versionup
    ::if '%choice3%'=='n' goto addname


    :versionup
    set /A version=version+1

    echo version is %version%
    ::FOR /F "tokens=* delims=0" %%A IN ("%Var%") DO SET Var=%%A
    set filename="../delivery_%mydate%_%newname%_%version%.txt"
    if EXIST "%filename%" goto :versionup 

    :start


    echo.

    echo Choose the following:
    echo 1. Directories
    echo 2. Files
    echo 3. quit
    echo.

    set /p choice=
    if '%choice%'=='1' goto directory
    if '%choice%'=='2' goto file
    if '%choice%'=='3' goto end
    cls
    ECHO "%choice%" is not valid please try again
    goto start


    :directory
    dir /ad /on /b > %filename%
    echo.
    goto checksuccess


    :file
    dir /a-d /on /b > %filename%
    echo.
    goto checksuccess


    : checksuccess
    if EXIST %filename% (
        echo %filename% written successfully
    ) else (
        echo file not written
    )


    :end
    echo.