2

I have numerous mp4 files inside a folder with can take the form below

  1. Dancing_2022-10-30_00-05-32_015_Paris.mp4
  2. Eating_in_the_Restaurant_2022-07-03_04-21-25_497_London.mp4

My goal is to create two folders and move the respective files I want the last underscore (_) delimited item (in this case Paris and London) extracted to create the first folders and then extract the date part (2022-10-30 and 2022-07-03) and create the subfolder. Then move the files accordingly. My biggest challenge is to extract the tokens. Usually does allows tokens to be extracted from left to right but this assumes the file names are consistent. However in my case the files names are not consistent. There is some consistency from right to left with regards to the placement of the _ delimited parts I want to extract. I have a draft batch script but the issue I have is that CMD does not allow delayed expanded variable to be passed to the inner For-Loop within the Outer For loop. Any clues to the solution. Thanks

This is my draft batch script.

@echo off
Setlocal EnableDelayedExpansion
    
for %%F in ("*.mp4") do (
    set file=%%~nF
    echo !file!
    set files="!file:_=" "!"
    echo file=!files!
    set cnt=-4
    for %%a in (!files!) do set /a cnt+=1
    echo !cnt!
    for /F "tokens=!cnt!* delims=_" %%a in ("!file!") do set output=%%b
    echo Output=%output%
    for /F "tokens=1-4 delims=_" %%a in ("!output!") do echo %%d
)
endlocal
pause
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Normad68
  • 19
  • 4
  • 1
    Does this answer your question? [Issue with \*For loop\* and \*delayed expansion\*](https://stackoverflow.com/questions/33973243/issue-with-for-loop-and-delayed-expansion) Specifically, you can only use `%variable%` in `for` loop options and not `!variable!` because of the order in which the interpreter processes things, so you'll have to create a subroutine and pass `!cnt!` to it. – SomethingDark Oct 31 '22 at 07:47
  • 1
    @Magoo SomethingDark is right, in FOR loop **options**, neither delayed expansion nor for-loop-meta variables work – jeb Oct 31 '22 at 08:25
  • @jeb: True, retracted – Magoo Oct 31 '22 at 08:27

1 Answers1

2
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

FOR %%e IN ("%sourcedir%\*.mp4") DO (
 SET "fname=%%~ne"
 SET "fname=!fname:_=,!"
 SET "fname=!fname: =#!"
 FOR %%y IN (!fname!) DO SET "fdate=!ftime!"&SET "ftime=!fmsec!"&SET "fmsec=!flocation!"&SET "flocation=%%y"
 ECHO MD "!flocation:#= !\!fdate!"
 ECHO MOVE "%%e" "!flocation:#= !\!fdate!\"
)
GOTO :EOF

Always verify against a test directory before applying to real data.

The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)

The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)

Relatively easy to follow. Foe each filename, take the name part. replace each _ with , and each Space with #

The result in fname would thus be a comma-separated list of elements, so process each one, cascading them through the variables.

Finally, replace the # with Space when creating the subdirectory and filename.

Note that the switch between Space and # is intended to take care of places like New York and Los Angeles

== Supplemental == in response to comment:

FOR %%e IN ("%sourcedir%\*.mp4") DO (
 SET "fname=%%~ne"
 SET "fname=!fname:_=,!"
 SET "fname=!fname: =#!"
 FOR %%y IN (!fname!) DO SET "fdate=!ftime!"&SET "ftime=!fmsec!"&SET "fmsec=!flocation!"&SET "flocation=%%y"
 SET "fname=!fname:,=_!"
 CALL SET "fname=%%fname:_!fdate!_!Ftime!_!fmsec!_!flocation!=%%
 ECHO MD "!flocation:#= !\!fname!\!fdate!"
 ECHO MOVE "%%e" "!flocation:#= !\!fname!\!fdate!\"
 ECHO MD "!flocation:#= !\!fname:_= !\!fdate!"
 ECHO MOVE "%%e" "!flocation:#= !\!fname:_= !\!fdate!\"
)

After locating the required parts, replace each , with _ in fname, then replace the part from the date onwards with nothing, yielding the activity in fname.

Two sets of reports generated now - one for activity with underscores, the second with spaces.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • This is ingenious! I would never have figured out a simpler way to do this. Thank you very much. You are very resourceful. I figured I missed something. Can you tweak the code to have the folder structure like this - Paris>Dancing>2022-10-30>mp4 file and London>Eating_in_the_Restaurant>2022-07>mp4 file. – Normad68 Oct 31 '22 at 19:48