Reputation: 395
This question is building off the solution posted here.
The original folder has names like:
1FT_DINRM_TASCAM_pt5GAL_TCL_clp.wav
1FT_DINRM_TASCAM_pt5GAL_TCL_lclp.wav
1FT_DINRM_TASCAM_pt5GAL_TCL.wav
I want to create 3 separate folders to hold the files with the different ending into individual folders like this:
Files with xxx_clp to go into a folder, "Modified_originalClip"
Files with xxx_lclp to go into a folder, "Modified_originalLightClip" and
Files with none of the above endings, to go into a folder, "Modified_originalNoClip"
This is the code I came up with that is located in the same batch file:
@%SystemRoot%\System32\robocopy.exe "G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified" "G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified_originalNoClip" *L.wav /NDL /NFL /NJH /NJS /R:3 /W:10
@%SystemRoot%\System32\robocopy.exe "G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified" "G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified_originalClip" *_clp.wav /NDL /NFL /NJH /NJS /R:3 /W:10
@%SystemRoot%\System32\robocopy.exe "G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified" "G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified_originalLightClip" *_lclp.wav /NDL /NFL /NJH /NJS /R:3 /W:10
Is it possible to make the path dynamic instead of hard coded? That is, to carry out this operation from where the batch file is located. If the batch file is located as follows:
| Original
| Modified_originalClip
|1FT_DINRM_TASCAM_pt5GAL_TCL_clp.wav
| Modified_originalLightClip
|1FT_DINRM_TASCAM_pt5GAL_TCL_lclp.wav
| Modified_originalNoClip
|1FT_DINRM_TASCAM_pt5GAL_TCL.wav
| filetofolder.bat
Any help will be greatly appreciated!
Upvotes: 1
Views: 1253
Reputation: 49096
I recommend to use the following batch file which can be stored in the folders:
G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass
G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified
The batch file code is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
if exist "%~dp0*.wav" (
for %%I in ("%~dp0.") do (
set "SourceFolder=%%~fI"
set "TargetFolder=%%~fI_original"
)
) else if exist "%~dp0Modified\*.wav" (
set "SourceFolder=%~dp0Modified"
set "TargetFolder=%~dp0Modified_original"
) else (
echo(
echo ERROR: Cannot find a *.wav file in the folders:
echo "%~dp0"
echo "%~dp0Modified\"
echo(
pause
goto EndBatch
)
if exist "%SourceFolder%\*_lclp.wav" %SystemRoot%\System32\robocopy.exe "%SourceFolder%" "%TargetFolder%LightClip" *_lclp.wav /NDL /NFL /NJH /NJS /R:3 /W:10
if exist "%SourceFolder%\*_clp.wav" %SystemRoot%\System32\robocopy.exe "%SourceFolder%" "%TargetFolder%Clip" *_clp.wav /NDL /NFL /NJH /NJS /R:3 /W:10
if exist "%SourceFolder%\*L.wav" %SystemRoot%\System32\robocopy.exe "%SourceFolder%" "%TargetFolder%NoClip" *L.wav /NDL /NFL /NJH /NJS /R:3 /W:10
:EndBatch
endlocal
The expression %~dp0
references the drive and path of argument 0 which expands always to full path of currently processed batch file ending with a backslash. So for the two examples above %~dp0
is one of the following two folder paths:
G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\
G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified\
The batch file checks first if there is at least one .wav
directory entry (hopefully a file and not a directory of which name ends with .wav
) in the directory of the batch file. This is the case for batch file being stored in the directory G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass\Modified
.
ROBOCOPY has like REG an unusual parsing of argument strings. These two Windows commands interpret a backslash \
left to one more backslash \
or a double quote "
as an escape character for the backslash respectively the double quote while a backslash left to any other character is interpreted as literal character backslash. This special argument parsing makes sense for the command REG as it is often necessary to define a data string containing double quotes, but it does not really make sense in my opinion for command ROBOCOPY as it is impossible that a folder path or a file name contains a double quote character. However, that special argument parsing means that neither the source folder path assigned to environment variable SourceFolder
nor the target folder path assigned to environment variable TargetFolder
should end with a backslash as this backslash would be interpreted as escape character for "
and ROBOCOPY would interpret everything right to "
terminating the source/target folder path up to next "
without a backslash left to it as part of the source respectively target folder path although a folder path cannot contain a double quote character.
For that reason the command FOR is used to get the full path of the batch file without a backslash appended assigned to environment variable SourceFolder
. The dot after %~dp0
means current directory. The usage of %%~fI
forces command FOR to access the file system and this results in determining the absolute path of the folder path with relative path part .
for current directory at end of the folder path.
The target folder path assigned to environment variable TargetFolder
is in this case the batch file path without backslash at end, but with _original
appended. So the batch file could be stored also in a folder with path C:\Temp\MyWaves
containing one or more .wav
files in which case the target folder path would be C:\Temp\MyWaves_original
.
But if the folder containing the batch file does not contain any .wav
file (directory entry), the batch file checks next if there is in batch file folder a subfolder Modified
containing at least one .wav
file. That condition is true if the batch file is stored in folder G:\DataSet\PlasticBagPops\Personal Data\NSULabGlass
.
If there is no .wav
file in folder of the batch file and also not in a subfolder Modified
of the batch file folder, the batch file outputs an appropriate error message, pause the batch file execution until the user pressed a key and next jumps to a label resulting in exiting the batch file processing without doing anything at all.
The next three IF conditions check first if there is at least one file matching the wildcard pattern in source folder before running the command ROBOCOPY to move the files matching the wildcard pattern.
The order of the three IF conditions with ROBOCOPY execution on condition being true could be very important depending on the wildcard patterns.
ROBOCOPY creates the entire target folder tree automatically on not already existing. So it is not necessary to explicitly create the three target folders using either MD or MKDIR.
The batch file ends with restoring the execution environment defined outside of the batch file.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully. https://www.howtogeek.com/235101/10-ways-to-open-the-command-prompt-in-windows-10/
call /?
... explains how batch file arguments can be referenced from within a batch file as used here to reference with %~dp0
the full path of the batch file.echo /?
endlocal /?
for /?
if /?
pause /?
robocopy /?
set /?
setlocal /?
Upvotes: 1