I'm trying to make that when I run the script and there's a .wem file in my "input" folder, it will directly convert it into my "output" folder. But I can't seem to figure this out.
#Echo off
Echo -------------------------------------------------------------------------------------
For %%f in ("%~dp0input\*.wem") do "./ww2ogg024/ww2ogg.exe" --pcb "./ww2ogg024/packed_codebooks_aoTuV_603.bin" "%%f"
For %%f in ("%~dp0outnput\*.ogg") do revorb.exe "%%f"
Echo -------------------------------------------------------------------------------------
pause
Does anyone know what am I doing wrong? I'm kind of a starter with this.
I have no knowledge of your executables, however, a quick search revealed their command line options:
ww2ogg input.wav [-o output.ogg] [--inline-codebooks] [--full-setup]
[--pcb packed_codebooks.bin]
revorb <input.ogg> [output.ogg]
From that information, I'd suggest that you try this sort of methodology, (Remarks included as explanation):
#Echo Off
Rem Define the location for ww2ogg.exe.
Set "WemToOggPath=C:\SomeLocation\ww2ogg024"
Rem Define the location for revorb.exe.
Set "MyRevorbPath=C:\SomeLocation"
Rem Exit if the required executables and input files are not available.
If Not Exist "%WemToOggPath%\ww2ogg.exe" Exit /B 1
If Not Exist "%MyRevorbPath%\revorb.exe" Exit /B 1
If Not Exist "%~dp0input\*.wem" Exit /B 1
Rem Create output directory if it does not already exist along side this script.
If Not Exist "%~dp0output\" MD "%~dp0output"
Echo -------------------------------------------------------------------------------------
Rem Loop through each .wem file located inside the a directory named input along side this script.
For %%I In ("%~dp0input\*.wem") Do (
Rem Run ww2ogg.exe against each .wem file outputting them to the same directory but with an .ogg extension.
"%WemToOggPath%\ww2ogg.exe" "%%I" -o "%%~dpnI.ogg" --pcb "%WemToOggPath%\packed_codebooks_aoTuV_603.bin"
Rem If the last ww2ogg.exe process was successful then.
If Not ErrorLevel 1 If Exist "%%~dpnI.ogg" (
Rem If there is still a .wem file delete it.
If Exist "%%I" Del "%%I"
Rem Run revorb.exe against the .ogg file outputting it to the output directory along side this script.
"%MyRevorbPath%\revorb.exe" "%%~dpnI.ogg" "%~dp0output\%%~nI.ogg"
Rem If the last revorb.exe process was successful and there is still a matching .ogg file inside the input directory delete it.
If Not ErrorLevel 1 If Exist "%%~dpnI.ogg" If Exist "%~dp0output\%%~nI.ogg" Del "%%~dpnI.ogg"
)
)
Echo -------------------------------------------------------------------------------------
Pause
As I don't know those utilities, I have tried to assume nothing, so if your conversion processes are not leaving the unconverted files behind, the script could probably be made smaller.
Please read through the Remarks to understand exactly what it does, before you run it, and most importantly, ensure that you modify C:\SomeLocation on lines 3 and 5 to those which hold your two executables. (Please do not leave trailing path separators on those directories). I would then suggest that you try the script from within a test input directory, and against some copied .wem files, before attempting it in your production environment.
Related
I have a folder full of zip files. Those zip files sometimes contain zip files, that sometimes contain zip files within them, and so on. I am trying to write a batch file that I can paste into the top folder containing all the zips, and when it runs it will unzip all the nested zip files, and within sub-directories, all the way down, and delete the zips once they have been successfully extracted. The full file paths need to be preserved. If there is an error and a file cannot be extracted then it should not be deleted and the file and file path need to be printed to a text file.
So far I have this:
#ECHO ON
SET source=%cd%
FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%source%\*.zip"') DO "C:\Program Files\7-Zip\7z.exe" x "%%~fF" -o"%%~pF\"
EXIT
Which I can drop into a folder and run, it will unzip the first level of zips but none of the nested zips inside. That's the first hurdle.
The next hurdle would be to delete the successfully extracted zips. And last, not to delete any zips that could not be extracted and print their name and/or path to a text file.
Any suggestions or chunks of code are appreciated. Or if there's a better way to do this entirely.
**** UPDATED ****
Mofi posted an answer that looks like it's working except for one piece:
When a ZIP is extracted, it needs to be extracted to a folder with the same name, so I can still follow the structure.
Starting Example:
[Top Level Folder Holding Zips] (folder)
--ExampleZip.zip
---FileInZip.txt
---FileinZip2.txt
--ExampleZip2.zip
---Folder1 (folder)
----ExampleZip3.zip
-----FileinZip3.txt
-----FileinZip4.txt
---ExampleZip4.zip
----FileinZip5.txt
----FileinZip6.txt
Needs to become this:
[Top Level Folder Holding Zips] (folder)
--ExampleZip (folder)
---FileInZip.txt
---FileinZip2.txt
--ExampleZip2 (folder)
---Folder1 (folder)
----ExampleZip3 (folder)
-----FileinZip3.txt
-----FileinZip4.txt
---ExampleZip4 (folder)
----FileinZip5.txt
----FileinZip6.txt
So the full structure is still visible.
I think the top answer in this question shows what I need to include: Extract zip contents into directory with same name as zip file, retain directory structure
This part:
SET "filename=%~1"
SET dirName=%filename:~0,-4%
7z x -o"%dirName%" "%filename%"
Needs to be smashed in there somewhere. Or it seems like there should be a switch for 7Zip that does it, since you can do this from the context menu with "Extract to *" I thought that's what the "extract with full paths" command does but that must have something to do with the -o switch, specifying output path? How do I specify the output path to be a folder with the same name as the input zip? Or merge the answer from that question I linked with Mofi's answer?
*** UPDATED AGAIN ***
I thought there was an issue with the batch file ignoring ZIP files with underscores in the name, but that was a coincidence and it was actually ignoring ZIP files without the Archive file attribute set.
Mofi suggested another fix for that which worked, but the batch file is not extracting nested zips that needed the Archive file attribute set.
This does kind of work, in that I can manually execute the batch file a few times and it will work it's way through everything in the folder, but the loop calculation does not seem to work, or is calculating/terminating before the batch file sets the Archive attribute for all zip files?
Here is the current version I'm working with:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ErrorOutput="
set "LoopCount=20"
rem The current directory is used on batch file being called without
rem a base folder path or with just one or more double quotes.
set "BaseFolder=%~1"
if defined BaseFolder set "BaseFolder=%BaseFolder:"=%"
if not defined BaseFolder set "BaseFolder=%CD%" & goto VerifyFolderPath
rem Make sure the folder path contains backslashes and not forward slashes
rem and does not contain wildcard characters or redirection operators or a
rem horizontal tab character after removing all double quotes.
set "BaseFolder=%BaseFolder:/=\%"
for /F "delims=*?|<> " %%I in ("%BaseFolder%") do if not "%BaseFolder%" == "%%I" (
echo ERROR: %~nx0 must be called with a valid folder path.
echo "%~1" is not a valid folder path.
set "ErrorOutput=1"
goto EndBatch
)
rem Get full folder path in case of the folder was specified with
rem a relative path. If the folder path references the root of a
rem drive like on using "C:\" or just "\", redefine the folder
rem path with full path for root of the (current) drive.
for %%I in ("%BaseFolder%") do set "BaseFolder=%%~fI"
:VerifyFolderPath
rem The base folder path must end with a backslash for verification.
if not "%BaseFolder:~-1%" == "\" set "BaseFolder=%BaseFolder%\"
rem Verify the existence of the folder. The code above processed also
rem folder paths of folders not existing at all and also invalid folder
rem paths containing for example a colon not (only) after drive letter.
if not exist "%BaseFolder%" (
echo ERROR: Folder "%BaseFolder%" does not exist.
set "ErrorOutput=1"
goto EndBatch
)
rem Make sure to process all ZIP files existing in base folder and all
rem its subfolders by setting archive file attribute on all ZIP files.
%SystemRoot%\System32\attrib.exe +A /S "%BaseFolder%*.zip"
rem Process all *.zip files found in base folder and all its subfolders
rem which have the archive file attribute set. *.zip files with archive
rem file attribute not set are ignored to avoid an endless running loop
rem if a ZIP archive file cannot be extracted successfully with reason(s)
rem output by 7-Zip or if the ZIP file cannot be deleted after successful
rem extraction of the archive. The archive extraction loop runs are limited
rem additionally by a loop counter as defined at top of the batch file for
rem 100% safety on prevention of an endless loop execution.
:ExtractArchives
set "ArchiveProcessed="
for /F "delims=" %%I in ('dir "%BaseFolder%*.zip" /AA-D /B /S 2^>nul') do (
set "ArchiveProcessed=1"
echo Extracting archive: "%%I"
"%ProgramFiles%\7-Zip\7z.exe" x -bd -bso0 -o"%%~dpnI\" -spd -y -- "%%I"
#pause
if errorlevel 255 set "ErrorOutput=1" & goto EndBatch
if errorlevel 1 (
set "ErrorOutput=1"
%SystemRoot%\System32\attrib.exe -A "%%I"
) else (
del /A /F "%%I"
if exist "%%I" (
echo ERROR: Failed to delete: "%%I"
set "ErrorOutput=1"
%SystemRoot%\System32\attrib.exe -A "%%I"
)
)
)
if not defined ArchiveProcessed goto EndBatch
set /A LoopCount-=1
if not LoopCount == 0 goto ExtractArchives
:EndBatch
if defined ErrorOutput echo/& pause
endlocal
echo[
echo[
echo If no errors are displayed above, everything extracted successfully. Remember to delete the batch file once you are done.
#pause
It is rare that there would be maybe 10 or 20 layers of nested zips, so a quick and dirty fix may be just somehow looping the whole batch file 10 or 20 times, unless that is a bad idea or there is a more elegant way to do it.
The task to recursively extract all ZIP archives including nested ZIP archives inside a ZIP archive can be achieved by running the ZIP archive file extraction process in a loop until no ZIP file exists anymore. But there must be at least two use cases taken into account to avoid an endless running archive extraction loop:
The extraction of a ZIP archive file fails for whatever reason. 7-Zip outputs information about the error reason(s). Such a ZIP file should not be processed a second time.
The deletion of a successfully extracted ZIP file fails for whatever reason. The ZIP file should not be processed once again.
The solution is processing only ZIP files with archive file attribute set as done automatically by Windows on creating, renaming or modifying a file and remove the archive file attribute on every ZIP file on which the extraction process or the deletion of the file failed to avoid processing the ZIP file again.
The archive file attribute is set on all *.zip files on directory tree to process before starting the archive files extraction process to make sure that really all existing *.zip files are processed at least once. The archive file attribute is also set on all *.zip files in output directory of a completely successfully processed ZIP archive file to make sure that even *.zip files inside a ZIP file with archive file attribute not set after extraction are processed also on next archive file extraction loop run.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ErrorOutput="
set "LoopCount=20"
rem The current directory is used on batch file being called without
rem a base folder path or with just one or more double quotes.
set "BaseFolder=%~1"
if defined BaseFolder set "BaseFolder=%BaseFolder:"=%"
if not defined BaseFolder set "BaseFolder=%CD%" & goto VerifyFolderPath
rem Make sure the folder path contains backslashes and not forward slashes
rem and does not contain wildcard characters or redirection operators or a
rem horizontal tab character after removing all double quotes.
set "BaseFolder=%BaseFolder:/=\%"
for /F "delims=*?|<> " %%I in ("%BaseFolder%") do if not "%BaseFolder%" == "%%I" (
echo ERROR: %~nx0 must be called with a valid folder path.
echo "%~1" is not a valid folder path.
set "ErrorOutput=1"
goto EndBatch
)
rem Get full folder path in case of the folder was specified with
rem a relative path. If the folder path references the root of a
rem drive like on using "C:\" or just "\", redefine the folder
rem path with full path for root of the (current) drive.
for %%I in ("%BaseFolder%") do set "BaseFolder=%%~fI"
:VerifyFolderPath
rem The base folder path must end with a backslash for verification.
if not "%BaseFolder:~-1%" == "\" set "BaseFolder=%BaseFolder%\"
rem Verify the existence of the folder. The code above processed also
rem folder paths of folders not existing at all and also invalid folder
rem paths containing for example a colon not (only) after drive letter.
if not exist "%BaseFolder%" (
echo ERROR: Folder "%BaseFolder%" does not exist.
set "ErrorOutput=1"
goto EndBatch
)
rem Make sure to process all ZIP files existing in base folder and all
rem its subfolders by setting archive file attribute on all ZIP files.
%SystemRoot%\System32\attrib.exe +A /S "%BaseFolder%*.zip" >nul
rem Process all *.zip files found in base folder and all its subfolders
rem which have the archive file attribute set. *.zip files with archive
rem file attribute not set are ignored to avoid an endless running loop
rem if a ZIP archive file cannot be extracted successfully with reason(s)
rem output by 7-Zip or if the ZIP file cannot be deleted after successful
rem extraction of the archive. The archive extraction loop runs are limited
rem additionally by a loop counter as defined at top of the batch file for
rem 100% safety on prevention of an endless loop execution.
:ExtractArchives
set "ArchiveProcessed="
for /F "delims=" %%I in ('dir "%BaseFolder%*.zip" /AA-D /B /S 2^>nul') do (
set "ArchiveProcessed=1"
echo Extracting archive: "%%I"
"%ProgramFiles%\7-Zip\7z.exe" x -bd -bso0 -o"%%~dpI" -spd -y -- "%%I"
if errorlevel 255 set "ErrorOutput=1" & goto EndBatch
if errorlevel 1 (
set "ErrorOutput=1"
%SystemRoot%\System32\attrib.exe -A "%%I"
) else (
%SystemRoot%\System32\attrib.exe +A /S "%%~dpnI\*.zip" >nul
del /A /F "%%I"
if exist "%%I" (
echo ERROR: Failed to delete: "%%I"
set "ErrorOutput=1"
%SystemRoot%\System32\attrib.exe -A "%%I"
)
)
)
if not defined ArchiveProcessed goto EndBatch
set /A LoopCount-=1
if not LoopCount == 0 goto ExtractArchives
:EndBatch
if defined ErrorOutput echo/& pause
endlocal
Note: There must be one horizontal tab character after "delims=*?|<> and " on line 16 of the batch file code and not a series of space characters as there will be after copying the code from browser window and pasting the code into a text editor window.
The batch file is commented with lines with command REM (remark). These comments should be read for understanding the code and then can be removed for a more efficient execution of the batch file by Windows command processor.
The 7-Zip switches used in code are explained by help of 7-Zip opened by double clicking on file 7-zip.chm or opening Help from within GUI window of started 7-Zip. On help tab Contents expand the list item Command Line Version and click on list item Switches to get displayed the help page Command Line Switches with all switches supported by currently used version of 7-Zip.
The batch file can be executed with a folder path as argument to process all ZIP files in this folder and all its subfolders. So it is possible to add to Send to context menu of Windows File Explorer a shortcut file which runs the batch file with the folder path passed by Windows File Explorer to the batch file as first argument. It would be also possible to registry the batch file as context menu option for Directory in Windows registry to be able to run the batch file easily from within any application supporting the Windows context menu handlers for a directory.
Edit after question edited: The command line running 7-Zip can be modified to:
"%ProgramFiles%\7-Zip\7z.exe" x -bd -bso0 -o"%%~dpnI\" -spe -spd -y -- "%%I"
Each ZIP file is extracted with this command line into a subfolder in folder of the ZIP file with name of the ZIP file because of replacing -o"%%~dpI" by -o"%%~dpnI\". The additional 7-Zip switch -spe avoids duplicating the folder name if the ZIP file contains at top level a folder with same name as the ZIP file. So if Example3.zip contains at top level the folder Example3, the files are extracted to folder Example3 and not to folder Example3\Example3 as it would occur without usage of option -spe.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
attrib /?
call /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
Using Groovy, or Ant
This would be a lot easier using Apache Ant or, better still, the Groovy AntBuilder.
e.g. this Groovy script will unzip all the top leval zip files then delete them:
new AntBuilder().with {
def sourceRoot = '.'
// Unzip all .zip files in / underneath sourceRoot
unzip( dest: 'some-folder' ) {
fileset( dir: sourceRoot ) {
include name: "**/*.zip"
}
}
// Unzip throws an exception on failure.
// Delete all .zip files in / underneath sourceRoot
delete {
fileset( dir: sourceRoot, includes: '**/*.zip' )
}
}
You'll need to keep scanning the destination folder for zips, and repeating the above process, until everythings unzipped. You may also find it useful to use a FileScanner.
AntBuilder throws an exception if anything fails, so you can avoid deleting archives that fail to unzip. AntBuilder will also log it's progress, using the standard Java logging mechanisms. You can tell it the level of detail you want, or supress it completely
The full AntBuilder documentation is here:
http://docs.groovy-lang.org/latest/html/documentation/ant-builder.html
Using a fileScanner
Example from the Groovy AntBuilder documentation:
// let's create a scanner of filesets
def scanner = ant.fileScanner {
fileset(dir:"src/test") {
include(name:"**/My*.groovy")
}
}
// now let's iterate over
def found = false
for (f in scanner) {
println("Found file $f")
found = true
assert f instanceof File
assert f.name.endsWith(".groovy")
}
assert found
Putting it together
It's not a huge leap to combine a filesScanner with an AntBuilder to get the job done. I suspect it will be a lot easier than doing it with a batch script.
Finally managed to write a batch file that can unzip nested zips, keeping the archive file structure intact!
logic is that, run recursively until all the zip files are unzipped. Number of iterations default is 5, and can be passed as cmd arg "extract.bat 3". may be changed to a while loop until hit file not found exception. And most importantly delete the archive file after extraction, so, we don't get into endless loop!
But follow the rules below
it uses 7z, make sure in the cmd window 7z can be run, that is in the path
zip file names cannot have spaces. make sure of that and ext is zip
copy the zip file to a directory where there are no other zip files
And only .zip ext, you may change that to rar or anything in the batch file
Here is the batch file
Rem Nested unzip - #sivakd
echo off
if "%1"=="" (set iter=5) else (set iter=%1)
echo Running %iter% iterations
for /l %%x in (1, 1, %iter%) do (
dir *.zip /s /b > ziplist.txt
for /F %%f in (ziplist.txt) do (
7z x %%f -o%%~dpnf -y & del /f %%f
)
del ziplist.txt
)
My Code
I have some straight forward code below that:
Checks if a file exists in my directory
Runs a for loop to get the first filename
Does stuff based on filename
Deletes the file
Checks if any other files exist in directory (if yes, repeat, if not, move on)
:MYLOOP
IF NOT EXIST "%mypath%\*.*" GOTO nofile
FOR %%F IN ("%mypath%\*.*") DO (
set filenameWithExt=%%~nxF
set filename=%%~nF
set filepath=%%~pF
)
do other filename specific tasks
del "%mypath%\%filenameWithExt%"
IF NOT EXIST "%mypath%\*.*" GOTO nofile
GOTO MYLOOP
:nofile
My Issue
I've used this code repeatedly and its worked like a charm, but on my most recent use it looks like its finding a 'ghost' file. When there are no FILES (there is a single archive FOLDER) in the directory, the if not exist check from step 1 above somehow is still passing. As a result, the set code in the for loop results in:
The system cannot find the file specified.
And it then appears as though it tries to delete my directory, saying:
\\mypath*, Are you sure (Y/N)?
I then have to manually terminate an otherwise automated batch.
My Question
Why is it passing the if not exist check, rather than skipping to :nofile?
How can I account for this 'ghost' file (or if it is detecting the archive folder -- how else can I ignore it)?
Windows kernel and therefore also Windows command interpreter interprets the wildcard pattern *.* like * which means any file or folder. On using wildcard pattern *.* it does not mean there must be a file (or folder) with a dot in name.
For that reason using the condition IF NOT EXIST "%mypath%\*.*" is the same as using IF NOT EXIST "%mypath%\*".
IF EXIST "%mypath%\*" is often used in batch files to verify that %mypath% specifies a folder and not a file because this condition checks if there is a folder %mypath%. The condition is true if that folder exists, independent on number of files and folders in that folder.
So the condition at top of your batch file does not check if there is not at least 1 file in folder %mypath%, it checks if this folder does not exist at all.
You could use the following batch code which avoids the usage of delayed expansion by using a subroutine.
#echo off
for /F "delims=" %%I in ('dir /A-D /B /ON "%mypath%\*" 2^>nul') do call :ProcessFile "%mypath%\%%I"
goto :EOF
:ProcessFile
set "FilenNmeWithExt=%~nx1"
set "FileName=%~n1"
set "FilePath=%~p1"
rem do other filename specific tasks
del "%~1"
goto :EOF
The command FOR executes the command line
dir /A-D /B /ON "%mypath%\*" 2>nul
in a separate command process in background and captures the output of DIR written to handle STDOUT.
DIR would output an error message to handle STDERR if the directory does not exist at all or does not contain any file. This error message is suppressed by redirecting it to device NUL using 2>nul. The redirection operator > must be escaped here with caret character ^ to be interpreted first by Windows command interpreter as literal character on parsing the entire FOR command line as otherwise a syntax error would be the result.
Option /A-D means that DIR should output all directory entries NOT having directory attribute set, i.e. just files, not folders. /B changes output of DIR to bare format which means only the file names without any additional data. /ON results in ordering the list by file name before DIR outputs the entire list. This option would not be really necessary here.
FOR processes now the captured output of DIR. So it does not matter that files from that directory are deleted while FOR is running. FOR processes the initial list as output by DIR.
For each file name output by DIR the subroutine ProcessFile is executed which is like calling another batch file with that name. Passed to the subroutine is the file name with its path. DIR outputs just the file name without path on not using additionally /S to get a list of all file names in specified directory and all its subdirectories.
The command goto :EOF after the FOR loop is required to avoid a fall through to the subroutine once all file names output by DIR have been processed.
The command goto :EOF after the subroutine would not be required if the line above is the last line of the batch file. But it is in general good practice to end a subroutine always with goto :EOF in case of ever adding more command lines like another subroutine below. For Windows command interpreter it does not matter where the subroutine starting with its label is located in the file.
The if exist test looks for anything in the directory.
I'd restructure you code:
:MYLOOP
set "found1="
FOR %%F IN ("%mypath%\*.*") DO (
set filenameWithExt=%%~nxF
set filename=%%~nF
set filepath=%%~pF
set "found1=Y"
)
if not defined found1 goto nofile
do other filename specific tasks
del "%mypath%\%filenameWithExt%"
GOTO MYLOOP
:nofile
If the for finds no files, found1 will remain undefined so we go to the :nofile label, else we have a file to process. Having deleted the file, just go back to the beginning, clear the flag and repeat...
I need to compare two folders using Batch commands. Both the folders have subdirectories and files in them. The comparison must check every file. If there is any difference, for instance if we are comparing folder1 and folder2, and file1 is present in folder1 but it is not present in folder2, then it must return true (so that I can do some other operations), else false.
I am actually doing a copy from one folder to another. Once it's done I need to validate if all the files are copied.
Note: I cannot use any third party tools.
#echo off
REM Writing the folder tree to a file for each path
pushd "Folder 1"
for /f "delims=" %%c in ('tree /f') do >>"%~dp0folder1.txt" echo "%%c"
popd
pushd "Folder 2"
for /f "delims=" %%d in ('tree /f') do >>"%~dp0folder2.txt" echo "%%d"
popd
REM removing the first three lines from each of the two files
more +3 "folder1.txt" >"folder1.txt.new"
move /y "folder1.txt.new" "folder1.txt" >nul
more +3 "folder2.txt" >"folder2.txt.new"
move /y "folder2.txt.new" "folder2.txt" >nul
REM comparing files
fc /b folder1.txt folder2.txt>nul && echo same || echo different
REM cleaning up
del folder1.txt
del folder2.txt
Should work as long as the folders are on the same drive. Changes the directory to the first folder and prints the output of the command tree to one file and same for the other folder.
Then compares a /binary file comparison and outputs same if the outputs of the command and with that the folders are the same. If not it outputs different.
Deletes two help-files after.
NOTE: If you already have files with that names, change it!
Feel free to ask if something is unclear :)
Edit: Remove first 3 lines to prevent comparison faliures based on Volume-Number/Drive-Letter. Credit for the way to remove goes to dbenhams answer to another question.
I want to be able to use the "Send to" function (When right clicking a file) with this batch file.
It needs to create a folder, with the name of the file, for each of the selected files, in the same directory as the file itself. (No moving of the file needed)
The following code has helped, but this creates folders for all files in the directory and places it in the directory of the batch file.
#echo off
pushd %~dp0
for /f "delims=" %%a in ('dir /b') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
)
)
popd
I believe using the following function will be needed for the directory of the files but not sure about how to call it.
%CD%
I am rather new to batch files so any extra explanation would be helpful, but not necessary.
Even if it can only run on one file at a time, that will be great since it needs to be no a chosen file basis.
Here goes to learning on the go and thanks for any help!
This should do what you are looking for. Give this script a try in your Send To menu:
#ECHO OFF
SETLOCAL
:ProcessFile
REM Check if there are any files to process.
IF "%~1"=="" GOTO :EOF
REM Process the current file.
SET NewDir="%~dpn1\"
REM Create the directory if it doesn't already exist.
IF NOT EXIST %NewDir% MKDIR %NewDir%
REM Move to the next selected file.
SHIFT /1
REM Recurse.
GOTO ProcessFile
ENDLOCAL
I am trying to create a batch file, or other script, to take the contents of one folder to a folder containing its name in another directory. For example:
ShowName.Episode.Title.mkv should be moved to \movies\showname. if \movies\showname\ doesn't exist, the script would create it.
There are, on average, 10-15 files at a time that would need moved.
Any ideas?
Thanks
You can conditionally create the folder with:
if not exist \movies\showname mkdir \movies\showname
To move a file into it:
move ShowName.Episode.Title.mkv \movies\showname
To get more information about these commands, open a command prompt and type:
help if
and
help move
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /f "tokens=1-4delims=." %%a IN (
'dir /b /a-d "%sourcedir%\*.*.*.mkv" '
) DO (
MD "%destdir%\%%a" 2>NUL
MOVE "%sourcedir%\%%a.%%b.%%c.%%d" "%destdir%\%%a\"
)
GOTO :EOF
This should do your moves. You'd have to change the directory names, of course - no idea where your source directory is, but destination becomes \movies in your case.
May be an idea to try ECHO MOVE first, just to make sure that the move is as-required.
The 2>nul on the MD suppresses the error messages saying the directory already exists.
Adding >nul to the end of the MOVE line will suppress the file moved message.