1

Basic Question:

How can I take a recently created folder (done in the same batch), then do stuff in the folder, then rename the folder with a timestamp and repeat with a new file generating a new timestamp?

My Batch Code Outline and Expected vs. Actual Results:

I am creating a batch file that will loop through the steps below, for all files in a directory.

Step 1 (working): go through file in a directory and extract data, this data will then be outputted into a created folder that has to be named "output" (which is created during this step).

Step 2 (working): I have to go into this "output" folder and "do stuff" with the data (I already have a script that goes into this new filepath of the "output" and "does stuff")

Step 3 (not working): rename the folder "output" to "output_TimeStamp" (This is where my problem is, my loop takes the timestamp#1 of the first folder created, and tries to name all folders timestamp#1)

Step 4 (semi working): Go back to Step 1 to work on the next file (Loop till all files are finished in directory)

My Code (well at least one of my attempts)

::Loop to perform tasks on files in current directory
for /R %%f in (*.mp4) do (

::Extracts data from file and leaves it in a created output folder
start "" /w Sample.exe --clip "%%f" --verbose 2 --outDir output

::This goes in created output folder and does stuff to data
start "" /w C:\Users\user\Documents\winPython\WinPython-64bit-3.4.4.2\python-3.4.4.amd64\python.exe "%CD%\DoStuff.py" "%CD%\output\folder\folder2\Do.file" 

::This is supposed to rename the folder with a time stamp   
rename output output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%



::This is what my research came to, which increments a number in the timestamp but doesnt work
set N=0
set FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%
:loop
set /a N+=1
set FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%
if exist %FILENAME% goto :loop

echo You can safely use this name %FILENAME% to create a new file 


)

My Research

I have tried numerous things and used the links How do I increment a folder name using Windows batch? and cmd line rename file with date and time.
I feel like this should be a lot easier then writing out this question.

Community
  • 1
  • 1
altairya
  • 13
  • 3
  • 1. Do not use `goto` in a loop as it breaks the loop context; you could put the code containing `goto` and the label in a `call` subroutine. 2. Avoid `::` comments (which actually constitute invalid labels) in loops as they may lead to unexpected results; use `rem` instead. 3. Enable [delayed expansion](http://ss64.com/nt/delayedexpansion.html) for `date` and `time` variables, because otherwise they always expand to the values present when the entire loop is parsed. – aschipfl Jul 08 '16 at 18:52
  • @aschipfl thank you for your reply. I made your changes but I'm still facing some hurdles. I don't know exactly what you mean by using the [call] subroutine, as I am very new to using batch. I did make the other changes. If its easier, could you tell me how I can just add an incrementing number at the end of the final folder after the date – altairya Jul 08 '16 at 19:58
  • It would help if you would post your current code in the question and tell how it does not meet your expectation. Include exact error messages if they appear. – lit Jul 09 '16 at 16:17

1 Answers1

0

Not checking the logic of your script, I fixed the following issues I already mentioned in my comment:

  1. goto :Label breaks the context of a parenthesised block of code, your for loop constitutes such a block; so execution continues at :Label but in a way as it would not be in a block any more. You can work around that by placing the code portion containing goto and :Label in a subroutine and call it by the call command; this hides the current block context from goto.
  2. :: comments might lead to unexpected behaviour when being used within loops or other parenthesised blocks of code; they actually constitute invalid labels. You should use rem instead.
  3. Enable delayed expansion and use it for variables date, time and FILENAME, because otherwise they always expand to the values present when the entire loop is parsed.

So here is the modified code:

setlocal EnableDelayedExpansion

rem // Loop to perform tasks on files in current directory
for /R %%f in (*.mp4) do (

    rem // Extracts data from file and leaves it in a created output folder
    start "" /w "Sample.exe" --clip "%%f" --verbose 2 --outDir output

    rem // This goes in created output folder and does stuff to data
    start "" /w "C:\Users\user\Documents\winPython\WinPython-64bit-3.4.4.2\python-3.4.4.amd64\python.exe" "%CD%\DoStuff.py" "%CD%\output\folder\folder2\Do.file" 

    rem // This is supposed to rename the folder with a time stamp   
    rename "output" "output-!date:~4,2!-!date:~7,2!-!date:~10,4!_at_!time:~0,2!!time:~3,2!"

    call :SUB

    echo You can safely use this name !FILENAME! to create a new file 

)
endlocal
exit /B


:SUB
rem // This is what my research came to, which increments a number in the timestamp but doesnt work
set /a N=0
set "FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%"
:loop
set /a N+=1
set "FILENAME=output-%date:~4,2%-%date:~7,2%-%date:~10,4%_at_%time:~0,2%%time:~3,2%.%N%"
if exist "%FILENAME%" goto :loop
exit /B
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • thank you very much for your efforts. You fixed my problem by adding the explanation points in the "rename" line. I also appreciate the lesson on the cal :SUB, that is a very powerful technique that I will be using a lot now. – altairya Jul 11 '16 at 12:30