Need to use variables to output a file to a specified directory while preserving sub folder using batch file

110 views Asked by At

I am hoping someone way smarter than me can help me modify this batch file to output or move the completed mp4 file to E:\downloads\converted\ however it is very important that I preserve the directory structure. For example if I have "E:\downloads\complete\Some Subfolder\Some-File.mkv" I need handbrake to process the file and convert it to an mp4 and then spit the file out as "E:\downloads\converted\Some Subfolder\Some-File.mp4"

I am attempting to run this on Windows 10. The conversion works correctly but I can't get the files and directories to move to the converted directory

This is what I have currently

for /R E:\downloads\complete\ %%F in (*.mkv,*.avi,*.mp4) do (
    "D:\HandBrakeCLI\HandBrakeCLI.exe" --preset-import-file "D:\HandBrakeCLI\VCEmp4HVEC.json" -Z "VCEmp4HVEC" -i "%%~fF" -o "%%~dpF%%~nF.mp4"
    if exist "%%~dpF%%~nF.mp4" (
        del "%%~fF"
        move "%%~dpF%%~nF.mp4" E:\downloads\converted\"%%~dpF%%~nF.mp4"
    )
)

This doesn't work because I don't understand enough about how those variables work to get it to do what I want despite my googling. I suspect I probably don't even need to use the move command and could probably just do it with the proper variables in the handbrake output but I am running myself in circles to figure it out. I also want the original non converted file to be deleted once it is done being processed.

tried the code above expecting it to move the sub folder and files to the specified directory but it fails

1

There are 1 answers

2
protoncracker On BEST ANSWER

If I understood it correctly, you want to preserve the folder structure when moving files from one directory to another after the HandBrake conversion. To achieve this, you can manipulate the destination path string by replacing the relevant part of it. Let's refine your code:


@echo off
setlocal enabledelayedexpansion

for /R E:\downloads\complete\ %%F in (*.mkv,*.avi,*.mp4) do (
    "D:\HandBrakeCLI\HandBrakeCLI.exe" --preset-import-file "D:\HandBrakeCLI\VCEmp4HVEC.json" -Z "VCEmp4HVEC" -i "%%~fF" -o "%%~dpF%%~nF.mp4"
    if exist "%%~dpF%%~nF.mp4" (
        del "%%~fF"
        set "destPath=%%~dpF"
        set "destPath=!destPath:E:\downloads\complete\=E:\downloads\converted\!"
        if not exist "!destPath!" (
            mkdir "!destPath!"
        )
        move "%%~dpF%%~nF.mp4" "!destPath!%%~nF.mp4"
    )
)

endlocal
  1. I added setlocal enabledelayedexpansion at the beginning so that we can manipulate string variables within the loop.
  2. set "destPath=%%~dpF" captures the directory of each file.
  3. set "destPath=!destPath:E:\downloads\complete\=E:\downloads\converted\!" replaces the relevant part of the directory string, effectively changing the destination directory.
  4. The mkdir "!destPath!" command creates the directory if it doesn't already exist.
  5. Finally, move "%%~dpF%%~nF.mp4" "!destPath!%%~nF.mp4" moves the converted file to the new directory while retaining the folder structure.

Why?

  • Lack of String Manipulation: The original code did not include a mechanism to alter the destination directory from E:\downloads\complete\ to E:\downloads\converted\. This is crucial for maintaining the folder structure.
  • No Directory Creation: The original code did not account for the need to create new directories in the destination path, which is essential for mirroring the folder structure.
  • Variable Expansion: Delayed variable expansion was not used, which is necessary for string manipulation within the loop.
  • File Deletion: While the original script aimed to delete the source file after conversion, it did not ensure that the file was successfully moved to the new location. The refined code incorporates this check.

This should accomplish the desired behavior you're looking for. Some minor changes might be needed, but this may give you an overall applicability.