Nested commands in a batch file

I am trying to create a simple batch file that will individually "rar" compress each file in the source directory. The naming of rar files should be based on the date / time the file was created.

Can't get names to reflect the exact date / time. Currently, all generated rar files have the same name. Think the problem is with the use of nested FOR commands.

Newbie here. Appreciate help if possible.

setlocal
set _source=H:\Test
set _dest=J:\output

for /f "delims=/ tokens=1-3" %%a in ("%DATE:~4%") do (
        for /f "delims=:. tokens=1-4" %%m in ("%TIME: =0%") do (
            set _FILENAME=basename%%c%%b%%a%%m%%n%%o%%p
        )
    )

ECHO %_FILENAME%

for %%I in (%_source%\*.*) DO (

md "%_dest%\%%~nI"
"rar.exe" A -v50M -m0 -k -y -ep1 -r -t -hp[aabbcc] "%_dest%\%%~nI\%_FILENAME%.rar" "%_source%\%%~nxI"
phpar2.exe c -s640000 -r10 -l "%_dest%\%%~nI\%_FILENAME%.par2" "%_dest%\%%~nI\%_FILENAME%*.rar"

)

      

+3


source to share


1 answer


@echo off
setlocal
set "_source=H:\Test"
set "_dest=J:\output"
setlocal enableDelayedExpansion

for %%I in (%_source%\*.*) DO (
    for /f "delims=/ tokens=1-3" %%a in ("%DATE:~4%") do (
        for /f "delims=:. tokens=1-4" %%m in ("!TIME: =0!") do (
            set _FILENAME=basename%%c%%b%%a%%m%%n%%o%%p
            ECHO !_FILENAME!
        )
    )
    md "%_dest%\%%~nI"
    "rar.exe" A -v50M -m0 -k -y -ep1 -r -t -hp[aabbcc] "%_dest%\%%~nI\!_FILENAME!.rar" "%_source%\%%~nxI"
     phpar2.exe c -s640000 -r10 -l "%_dest%\%%~nI\!_FILENAME!.par2" "%_dest%\%%~nI\!_FILENAME!*.rar"
)

endlocaL
endlocal

      



Is this what you want? You can nest three loops, but you need delayedExpansion to use _FILENAME .

+1


source







All Articles