Page 1 of 1

Easy Question

Posted: 04 Jan 2019 18:49
by Audoguy
I am in the prelinary stages of creating a batchfile to down-sample a LOT of videos in source directory tree and replicating that directory tree with the new files at a lower bit rate. Right now I am just testing on a few directories using the copy command as the converter runs for hours. I am having one issue I cannot overcome though:

On a WIndows10 64-bit with a normal batch file I am trying to extract the current directory name (not the full path) using the following line nested inside a larger loop recursing directories (Full file below):

for %%f in (!currentDirectoryNoTrailingSlash!) do set currentDirectoryName=%%~nxf

It works fine unless there is a space in the current directory name???

currentDirectory "C:\TEMP\Test1\"
currentDirectoryNoTrailingSlash "C:\TEMP\Test1"
currentdirectoryname "Test1"


currentDirectory "C:\TEMP\Test 2\"
currentDirectoryNoTrailingSlash "C:\TEMP\Test 2"
currentdirectoryname "2"

The whole file is below. Any guidance welcome.



Whole test file:

Code: Select all

@echo off
setlocal enabledelayedexpansion enableextensions

REM Set the root path where files end up
set destinationPathRoot=C:\Temp\New

REM Get all the files in the directory and sub-directories
for /f "tokens=* delims=|" %%G in ('dir /s /a-d /b *.*') do if not %%~xG==.bat (
 REM Set the source file path
 set sourceFilePath=%%G
 REM Get the current directory and remove the trialing slash
 set currentDirectory=%%~dpG
 set currentDirectoryNoTrailingSlash=!currentDirectory:~0,-1!
 echo currentDirectory "!currentDirectory!"
 echo currentDirectoryNoTrailingSlash "!currentDirectoryNoTrailingSlash!"
 REM Get only the name of the current directory
 for %%f in (!currentDirectoryNoTrailingSlash!) do set currentDirectoryName=%%~nxf

 REM Create the path to the destantion path based on the current directory name
 set destinationPath=!destinationPathRoot!\!currentDirectoryName!
 echo currentdirectoryname "!currentDirectoryName!" 
 REM if the destination doesn't exist, create it
 if not exist !destinationPath! (
  REM mkdir !destinationPath!
 REM Set the destination file based on the destination path and the current file name
 set destinationFilePath=!destinationPath!\%%~nG.mp4

 copy "!sourceFilePath!" "!destinationFilePath!"


Re: Easy Question

Posted: 05 Jan 2019 09:29
by aGerman
As always - paths containing spaces need to be surrounded with quotation marks. And it's good practice to use quoted paths either way.

Code: Select all

for %%f in ("!currentDirectoryNoTrailingSlash!") do ...

Re: Easy Question

Posted: 07 Jan 2019 05:48
by Audoguy
Thanks aGerman!!!

If you are "a German", I enjoy many weeks each year in Munich for work:)

In the interest of giving back once I get the other bugs out of the script I will post it with this fix to this forum and others with some nice search tags for future googlers.

Take care,


Re: Easy Question

Posted: 07 Jan 2019 12:04
by aGerman
Munich is a nice town but about 4 hours for me to go by car. So it's only once in a few years that I visit it.

Another advice I can give you for your script is that you should quote assignments this way:

Code: Select all

SET "variable_name=variable_value"
Drawbacks that you would have with special characters like & < > | are gone.
Exclamation points in the values are still critical as long as delayed expansion is enabled. Try to work with the FOR variables only. E.g.

Code: Select all

@echo off &setlocal

for %%i in ("%~f0") do (
  echo full name "%%~i"
  echo path "%%~dpi"
  for /f "delims=" %%j in ("%%~dpi.") do echo path without trailing backspace "%%~fj"