Easy Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Audoguy
Posts: 2
Joined: 04 Jan 2019 09:44

Easy Question

#1 Post by Audoguy » 04 Jan 2019 18:49

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???

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

Fail:

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

The whole file is below. Any guidance welcome.

Thanks

Audo

Whole test file:
-------------------------------------------------------------------------------------------------------

Code: Select all

@echo off
cls
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!"

 )
)
endlocal
Last edited by aGerman on 05 Jan 2019 09:25, edited 1 time in total.
Reason: Please use code formatting!

aGerman
Expert
Posts: 3643
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easy Question

#2 Post by aGerman » 05 Jan 2019 09:29

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 ...
Steffen

Audoguy
Posts: 2
Joined: 04 Jan 2019 09:44

Re: Easy Question

#3 Post by Audoguy » 07 Jan 2019 05:48

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,

Audo

aGerman
Expert
Posts: 3643
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easy Question

#4 Post by aGerman » 07 Jan 2019 12:04

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"
)

pause
Steffen

Post Reply