Use embedded file from .exe - windows

I made an exe that has embedded files in it like a portable 7zip (7za.exe) and I want to call to it in the batch script that I am compiling into an exe but when I do it just gives me "7za.exe" is not recognized as an internal or external command. If I left anything out just ask.
(Sorry if this is an easy fix I am just messing around with some basic code)
This is the code I am working with and exe is in releases tab.
https://github.com/iamtis/mass-extract

Let us look on batch file with some additional lines at top:
#echo off
echo Current working directory is: %CD%
echo Directory of batch file is: %~dp0
pause
echo Files in current working directory:
dir /A-D /B
pause
echo Files in directory of batch file:
dir /A-D /B "%~dp0"
pause
I suppose that the current working directory is not equal the directory of the batch file and the tools are in the directory of the batch file. I suppose the batch file directory is a subdirectory with random name in %TEMP%.
So what you most likely need is:
#echo off
set "ToolPath=%~dp0"
if not exist "%CD%\archive\*" md "%CD%\archive"
"%ToolPath%7za.exe" x "%CD%\*.zip" "%CD%\archive\"
"%ToolPath%7za.exe" x "%CD%\*.7z" "%CD%\archive\"
"%ToolPath%unrar.exe" x "%CD%\*.rar" "%CD%\archive\"
"%ToolPath%7za.exe" a -mx9 archive.7z "%CD%\archive\"
rd /S /Q "%CD%\archive"
set "ToolPath="

Related

How to recursively delete all folders of a folder tree unless they contain a file with certain file extension?

How to go though a directory tree and delete all directories unless they contain a file with a particular file extension?
I tried Robocopy thinking the folders were empty. But all the folders have hidden files. So I need something that will take every folder in a directory that does not have a .pdf for example in it and delete it.
The task is to delete all directories/folders not containing a PDF file and also not containing a subdirectory/subfolder containing a PDF file. Let us look on an example to better understand the directory/folder deletion task.
The directory C:\Temp contains following subfolders and files:
Folder 1
Subfolder A
File 1A.txt
Subfolder B
File 1B.txt
Subfolder C
File 1C.pdf
File 1.cmd
Folder 2
Subfolder A
Subfolder B
File 2B.pdf
Subfolder C
File 2C.pdf
File 2.jpg
Folder 3
Subfolder A
File 3A.log
Subfolder B
File 3.doc
Last Folder & End
Subfolder A
Last File A.xls
Subfolder B
Subfolder C
Last File C.pdf
A folder is formatted bold. A hidden folder is formatted bold and italic. A hidden file is formatted italic.
The wanted folders and files after running the batch file should be:
Folder 1
Subfolder C
File 1C.pdf
File 1.cmd
Folder 2
Subfolder B
File 2B.pdf
Subfolder C
File 2C.pdf
File 2.jpg
Last Folder & End
Subfolder C
Last File C.pdf
This result can be achieved by executing following batch file:
#echo off
goto MainCode
:ProcessFolder
for /F "delims=" %%I in ('dir "%~1" /AD /B 2^>nul') do call :ProcessFolder "%~1\%%I"
if exist "%~1\*.pdf" goto :EOF
for /F "delims=" %%I in ('dir "%~1" /AD /B 2^>nul') do goto :EOF
if /I "%~1\" == "%BatchFilePath%" goto :EOF
rd /Q /S "%~1"
goto :EOF
:MainCode
setlocal EnableExtensions DisableDelayedExpansion
set "BatchFilePath=%~dp0"
if exist "C:\Temp\" cd /D "C:\Temp" & call :ProcessFolder "C:\Temp"
endlocal
Doing something recursively on a directory tree requires having a subroutine/function/procedure which calls itself recursively. In the batch file above this is ProcessFolder.
Please read the answer on Where does GOTO :EOF return to? The command goto :EOF is used here to exit the subroutine ProcessFolder and works only as wanted with enabled command extensions. FOR and CALL as used here require also enabled command extensions.
The main code of the batch file first enables explicitly the command extensions required for this batch file and disables delayed environment variable expansion to process correct also folders with an exclamation mark in name. This is the default environment on Windows, but it is better here to explicitly set this environment because the batch file contains the command RD with the options /Q /S which can be really very harmful on execution from within wrong environment or directory.
The subroutine ProcessFolder is not at end of the batch file as usual with a goto :EOF above to avoid an unwanted fall through to the command lines of the subroutine after finishing the entire task. For safety reasons the subroutine is in the middle of the batch file. So if a user tries to execute the batch file on Windows 95/98 with no support for command extensions nothing bad happens because first goto MainCode is executed successfully as expected, but SETLOCAL command line, calling the subroutine and last also ENDLOCAL fail and so no directory was deleted by this batch file designed for Windows with cmd.exe as Windows command processor instead of command.com.
The main code sets also current directory to the directory to process. So C:\Temp itself is never deleted by this code because of Windows prevents the deletion of a directory which is the current directory of any running process or contains a file opened by a running process with file access permission set to prevent other processes to delete the file while being opened by the process.
Next is called subroutine ProcessFolder with argument C:\Temp to process this folder recursively.
Last the initial environment is restored which includes also initial current directory on starting the batch file if this directory still exists.
The command for /D is usually used to do something on all subdirectories of a directory. But this is not possible here because FOR always ignores directories and files with hidden attribute set. For that reason it is necessary to use command DIR to get a list of all subdirectories in current directory including directories with hidden attribute set.
The command line dir "%~1" /AD /B 2>nul is executed by FOR in a separate command process started with cmd.exe /C in background. This is one reason why this batch file is quite slow. The other reason is calling the subroutine again and again which cause internally in cmd.exe to save and restore environment again and again.
Please read the Microsoft article 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.
For each subdirectory in a directory the subroutine ProcessFolder calls itself. The first FOR loop in the subroutine is left if a directory does not contain one more subdirectory.
Then the subroutine checks in current directory if there is at least one *.pdf file. The IF condition used here is true even if the directory contains only a hidden PDF file. In this case the subroutine is exited without doing anything as this directory contains definitely a PDF file and therefore must be kept according to the requirements of the folder deletion task.
Next is checked if the current directory still contains at least one subdirectory as in this case the current directory must be also kept as one of its subdirectories contains at least one PDF file.
Last the subroutine checks if the current directory contains by chance the batch file as this directory must be also kept to finish the processing of the batch file.
Otherwise the current directory is deleted with all files on not containing a PDF file and no subdirectories and also not the currently running batch file as long as Windows does not prevent the deletion of the directory because of missing permissions or a sharing access violation.
Please note that the batch file does not delete other files in a directory which is not deleted as it can be seen also on the example.
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.
call /?
cd /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rd /?
setlocal /?

How to execute an application existing in each specific folder of a directory tree on a file in same folder?

I have some folders with different names. Each folder has a specific structure as listed below:
Folder1
Contents
x64
Folder1.aaxplugin
TransVST_Fixer.exe
Folder 2
Contents
x64
Folder 2.aaxplugin
TransVST_Fixer.exe
There are two files within each subfolder x64. One file has the same name as the folder two folder levels above. The other file is an .exe file whose name is the same in all folders.
Now I need to run file with file extension aaxplugin on each specific .exe file. It would be obviously very time consuming opening each and every single folder and drag & drop each file on .exe to run it on this file.
That's why I am trying to create a batch script to save some time.
I looked for solutions here on Stack Overflow. The only thing I have found so far was a user saying this: When I perform a drag & drop, the process 'fileprocessor.exe' is executed. When I try to launch this exe, though, CMD returns error ('not recognized or not batch file' stuff).
How can I do this?
UPDATE 12/22/2015
I used first a batch file with following line to copy the executable into x64 subfolder of Folder1.
for /d %%a in ("C:\Users\Davide\Desktop\test\Folder1\*") do ( copy "C:\Program Files\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%a\x64\" 2> nul )
After asking here, I tried the following script:
for /f "delims=" %%F in ('dir /b /s x64\*.aaxplugin') do "%%~dpFTransVST_Fixer.exe" "%%F"
Unfortunately, the output is as following
C:\Users\Davide\Desktop>for /F "delims=" %F in ('dir /b /s x64\*.aaxplugin') do "%~dpFTransVST_Fixer.exe" "%F"
The system cannot find the file specified.
Try the following batch code:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\Desktop\test" %%F in (*.aaxplugin) do (
set "FilePath=%%~dpF"
if not "!FilePath:\x64\=!" == "!FilePath!" "%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%F"
)
endlocal
The command FOR with option/R searches recursive in all directories of directory %USERPROFILE%\Desktop\test being expanded on your machine to C:\Users\Davide\Desktop for files with file extension aaxplugin. The loop variable F contains on each loop run the name of the found file with full path without surrounding double quotes.
The drive and path of each found file is assigned to environment variable FilePath.
Next a case-sensitive string comparison is done between file path with all occurrences of string \x64\ case-insensitive removed with unmodified file path.
Referencing value of environment variable FilePath must be done here using delayed expansion because being defined and evaluated within a block defined with ( ... ). Otherwise command processor would expand %FilePath% already on parsing the entire block resulting in a syntax error on execution because string substitution is not possible as no environment variable FilePath defined above body block of FOR loop.
The strings are not equal if path of file contains a folder with name x64. This means on provided folder structure that the file is in folder x64 and not somewhere else and therefore the application is executed next from its original location to fix the found *.aaxplugin file.
The line with IF is for the folder structure example:
if not "C:\Users\Davide\Desktop\test\Folder1\Contents" == "C:\Users\Davide\Desktop\test\Folder1\Contents\x64\"
if not "C:\Users\Davide\Desktop\test\Folder 2\Contents" == "C:\Users\Davide\Desktop\test\Folder 2\Contents\x64\"
So for both *.aaxplugin files the condition is true because the compared strings are not identical
Also possible would be:
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%F in ('dir /A-D /B /S "%USERPROFILE%\test\*.aaxplugin" 2^>nul') do (
set "FilePath=%%~dpF"
if not "!FilePath:\x64\=!" == "!FilePath!" "%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%F"
)
endlocal
But command DIR is not really necessary as it can be seen on first provided code.
But if the application TransVST_Fixer.exe for some unknown reason does its job right only with directory of file being also the current directory, the following batch code could be used instead of first code using the commands pushd and popd:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\test" %%F in (*.aaxplugin) do (
set "FilePath=%%~dpF"
echo !FilePath!
if /I "!FilePath:~-5!" == "\x64\" (
pushd "%%~dpF"
"%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%~nxF"
popd
)
)
endlocal
There is one more difference in comparison to first code. Now the last 5 characters of path of file are compared case-insensitive with the string \x64\. Therefore the file must be really inside a folder with name x64 or X64. A folder with name x64 or X64 anywhere else in path of file does not result anymore in a true state for the condition as in first two batch codes.
But if for some unknown reason it is really necessary to run the application in same folder as the found *.aaxplugin and the directory of the file must be the current directory, the following batch code could be used:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\test" %%# in (*.aaxplugin) do (
set "FilePath=%%~dp#"
if /I "!FilePath:~-5!" == "\x64\" (
pushd "%%~dp#"
"%%~dp#TransVST_Fixer.exe" "%%~nx#"
popd
)
)
endlocal
The path of the file referenced with %%~dpF always ends with a backslash which is the reason why there is no backslash left of TransVST_Fixer.exe (although command processor could handle also file with with two backslashes in path).
In batch code above character # is used as loop variable because %%~dp#TransVST_Fixer.exe is easier to read in comparison to %%~dpFTransVST_Fixer.exe. It is more clear for a human with using # as loop variable where the reference to loop variable ends and where name of application begins. For the command processor it would not make a difference if loop variable is # or upper case F.
A lower case f would work here also as loop variable, but is in general problematic as explained on Modify variable within loop of batch script.
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.
dir /?
echo /?
endlocal /?
for /?
if /?
popd /?
pushd /?
set /?
setlocal /?
Your question isn't quite clear, but it seems, something like this should work:
for /f "delims=" %%f in ('dir /b /s X64\*.ext') do "%%~dpfMyExe.exe" "%%f"
Maybe you have to change directory to each folder (depends on your .exe):
for /f "delims=" %%d in ('dir /B /ad') do (
pushd "%%d"
for /f "delims=" %%f in ('dir /b "contents\x64\*.ext"') do (
cd Contents\x64
MyExe.exe "%%f"
)
popd
)
Assuming:
The Directory structure is fixed and the files are indeed in a subfolder contents\X64\.
MyExe.exe is the same (name) in every folder.
There is only one file *.ext in every folder.
I'll give you the script I created for doing so, hope it works for you
for /d %%d IN (./*) do (cd "%%d/Contents/x64" & "../../../TransVST_Fixer.exe" "%%d" & cd "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins")
Please note that I placed the fixer inside the root folder so I just have to copy it once. You have to place it inside your root folder and execute it. What it does:
iterate over each folder
for each one it enters /Contents/x64, executes the fixer (wich is 3 levels above) and after that returns to the original folder.
If you have your plugins in a different folder, you just have to change this part replacing the path for the one you have your plugins in.
cd "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins"
REMEMBER to place the script on that folder. For this example I place my script on the folder "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins" and run it (as a .bat).
PS: the fixer will place the fixed plugins in "C:\Users\Public\modified" (just read the screen while executing, it gives you the new files path. If you want to move them to the right path, you can execute this from the new files path ("C:\Users\Public\modified")
for %%d IN (*.aaxplugin) do (mkdir "%%d_temp/Contents\x64" & move "%%d" "%%d_temp/Contents\x64/%%d" & rename "%%d_temp" "%%d")
with that, I iterate over every plugin and create a folder with the same name (I create _temp because of name colision, after moving the file I rename it to the correct one), also with the subfolder "/Contents/x64", and move the plugin inside. Once donde, you can just take the resulting folders and place them in their correct path.
Hope it works, for me it works like a charm.

batch file to list all txt files from other directories

I am in the process of fetching all the .txt files from one directory to another (my current).
My current directory is
C:\USERS\MRAH
where i have the batch file and i have the code to fetch all .TXT files from the directory
dir E:\S_RUNS\12 month_STAR\S_2013\tst\*.txt /b >> INPUT_FILE_LIST.TXT
I am not able to fetch all the .TXT file which are in the E:\ DIREC into INPUT_FILE_LIST.TXT file on C:\USERS\MRAH
Can anyone let me know as to what should be code to fetch all the .txt file from one directory to another...
Thanks!
I'm not completely sure this will work on multiple directories but you could try it.
Cd E:\[path]
for /d %%a in (*) do (if %~xa == .txt echo %%a >> input_list.tmp)
for /f %%a in (input_list.tmp) do (copy %%a C:\USERS\MRAH)
note the batch file needs to be run from the E:[path]
also note you save it as a .tmp file to prevent it from logging itself
also instead of making an input_list file do it directly:
for /d %%a in (*) do (if %~xa == .txt copy %%a C:\users\MRAH)
Tell me if this doesn't work
Yours, Mona
Assume your current working directory is c:\testDir and you wanna copy all txt files from c:\source to d:\dest then use following content in a batch file
copy c:\source*.txt d:\dest

How to copy a location to a batch file from a file that is dragged and dropped to open the batch file

So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause

Batch file: for parameters

I am having trouble understanding what does following windows batch file do, can somebody explain:
for /f %%i in ("%0") do set curpath=%%~dpi
cd /d %curpath%
/*Some other code...*/
cd /d %curpath%
%0 is the full path to the .bat file itself (if run from another directory) and ~dpi is a modifier to extract the drive and directory from a path omitting the file name, so this snippet sets the current drive & directory to the one in which the batch file lives.
I can't see the reason for using a FOR, %~dp0 does the same thing in one go.

Resources