Help With Hopefully Simple Batch Script

Soldato
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
Hi All,

Just looking for some help with a batch script to move some files around.

Basically I have a Camera that sends .264 files to a NAS drive, I then have a little computer that converts those into .mp4 so they are easily playable from multiple devices. The problem comes with the file structure that they are so for example currently they are like this:

Code:
Z:\CCTV\<Date>\Record\<filename>.mp4

All I would like to do after they have been converted into .mp4 is also move them into the parent directory so it shows like:
Code:
Z:\CCTV\<date>\<filename>.mp4

The reason I specifically refer to the .mp4 is because I want it to ignore .264 files as it sends them to the "Record" folder and that's where Handbrake finds the files to convert... and I can't change the Camera's preset file structure.

Not sure how I can adapt the script, I have this script

Code:
for /R Z:\CCTV\ %%F in (*.264) do (
    "..\HandBrakeCLI.exe" -i "%%~fF" -o "%%~dpF%%~nF.mp4"
    if exist "%%~dpF%%~nF.mp4" (
        del "%%~fF"
    )
)

Could this be adapted to move the files up? I have looked at so many different scripts and can't really get my head round this whole "%%~dpf" jazz, I understand that they link to variables like the drive name and file path but can't figure out how to adapt it to what I need.

The closest I got was:
Code:
forfiles /P Z:\CCTV /s /m *.mp4 /c "cmd /c move @file .."

The obvious problem with this was that every time the script was run it was moving the files up and up, which isn't what I want. I almost need it to run some sort of additional script like "If folder = record process, else ignore" If you catch my drift?

The script to search for the files to be converted is run every 10 minutes so that's I can't afford for it to be as the last. Any suggestions or solutions would be greatly appreciated!!
 
Soldato
Joined
3 Jun 2005
Posts
3,047
Location
The South
This isn't perfect and i'd definitely test it but it should do what you're after (might need to change to single %) -

Code:
for /f "delims=" %%D in ('dir /a:d /b Z:\CCTV') do forfiles /P %%~fD\Record /s /m *.mp4 /c "cmd /c move @file .."


Essentially, it's two loop - the outer, loops through the root directory (Z:\CCTV) using the dir command (dir /a:d /b Z:\CCTV) outputting the full path (%%~fD) of each <date> directory; the inner, does a 'file loop' through the 'Record' directory (%%~fD\Record; full path + 'Record') and moves any *.mp4 file up a level.
 
Soldato
OP
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
Thank you @visibleman I'll try and use that in my test folders and see how I get on.

And @Bug One I could rename them but I'm quite happy how they are named, I simply want to move them up one level so they are directly in the date named folder. This probably is doable from the output if I could figure out how to do it!

Also .264 is not natively playable on my pc or Android phone where I view them.
 
Soldato
OP
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
Code:
for /f "delims=" %%D in ('dir /a:d /b Z:\CCTV') do forfiles /P %%~fD\Record /s /m *.mp4 /c "cmd /c move @file .."

So when I try and run that it changes the directory to
Code:
C:\user\richa\<date>\record
And then exits saying error: directory name is invalid.
Does this have something to do with Z: being a mapped network drive?

Any further help would be appreciated
 
Soldato
Joined
3 Jun 2005
Posts
3,047
Location
The South
My mistake, i thought the dir command outputs the full path but it doesn't. There's no doubt a better way to do this but try the below (as before, using single % if using it from command line or double % in a batch script) -

Code:
for /f "delims=" %%D in ('dir /a:d /b Z:\CCTV') do forfiles /P Z:\CCTV\%%D\Record /s /m *.mp4 /c "cmd /c move @file .."


Essentially %%D is each <date> directory name and you're then formatting that to Z:\CCTV\<date>\Record.

And you can always break the loop down for testing, so in a command prompt (note the singular %) you can run the below to output a list of formatted <date> directories -

Code:
for /f "delims=" %D in ('dir /a:d /b Z:\CCTV') do echo Z:\CCTV\%D\Record


This is all based on the assumption that <date> refers to individual directories titled with the date, ie - Z:\CCTV\01-01-2020; Z:\CCTV\02-01-2020 etc.
 
Soldato
OP
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
That last one worked perfectly, thank you so much!

Needless to say I would never have got there as I only got the main conversion script working through trial and error and ripping other scripts together, but I was getting nowhere with this one.

I've set it to run at the end of the conversion script through task scheduler so gonna let it do its thing and keep my fingers crossed, but it worked perfectly with my test folders.
 
Soldato
OP
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
Maybe a slight added complication...

Task scheduler for some reason doesn't like the mapped network drive side of things so I have to use the UNC path name but the for function doesn't seem to like that.

Comes back with "ERROR: UNC path (\\machine\share) are not supported."
 
Soldato
Joined
3 Jun 2005
Posts
3,047
Location
The South
@Pulse The mapping probably doesn't exist outside of your user session (sort of rings bells) and there probably is a way of fixing it within Task Scheduler but ¯\_(ツ)_/¯.
So the only solution i can think of is to temporarily map a drive (Y: etc) to the UNC at the start of (batch) script and remove the mapping when done (end of script). It's a bit bodgy but i can't see why it shouldn't work.
 
Soldato
OP
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
I do remember trying something like that with one of the scripts that I did trial trying to get this working previously.

I'll find it again and give it a go.
 
Soldato
OP
Joined
25 Jul 2006
Posts
3,526
Location
Taunton
@visibleman got it working with net use commands. Ironically I also used the same net use to correct another script that I didn't even realise wasn't working correctly until I tested it and it didn't do anything.

Thank you for the script and the help!
 
Back
Top Bottom