bzip2 - Bzipping all files inside folders (Windows) - batch-file

I have a bzipping tool on my computer, but it only bzips files that are inside the "compress" directory. How would I make it so files inside all directories inside the compress directory are zipped?
Example
compress/image.png goes to compress/image.png.bz2
however
compress/folder/image.png stays as compress/folder/image.png
My batch file is as follows:
#echo off
title bzip
echo bzip
echo All files within /compress will be compressed as a .bz2
echo.
echo Compressing file(s)...
bzip2.exe -z compress/*.*
echo.
echo Compression Completed!
pause
I hope somebody can help me!
Edit:
When running the process with directories inside the compress directory, it says "permission denied".

Use for /r compress %%i in (*) do bzip2.exe "%%i" in your batch file instead of the call to bzip2.exe directly. bzip2 almost certainly doesn't know how to recurse through subfolders -- standard wildcard globbing libs on Windows generally don't.
Run for /? from a Command Prompt to see more about the syntax of the for command. If you want to test the command from a prompt instead of a batch file, use 1 percent sign for the variable instead of 2.

Related

set /p failing to read sftp mapped file

I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)

How to get the current path of my batch file? [duplicate]

I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.

How to run a batch file on all sub directories within a directory?

I have a batch file (ReduceFLACPadding.bat) to reduce padding in all FLAC files within a directory using metaflac.exe
These FLAC files are stored in subdirectories (one per album) within the directory E:\FLAC Library
At the moment I am processing my FLAC files one album at a time, moving the batch file to the targeted subdirectory each time. (The batch file is set to process all FLAC files within a directory)
My question is; is there a way to run this batch file on all the *.FLAC files in all the subdirectories of E:\FLAC in one go?
Please let me know if you need any more information
Windows 7
I would change the command that finds the *.flac files to find it recursively rather than running the batch file itself on every directory.
for /r "e:\flac" %%a in (*.flac) do echo metaflac "%%~fa" "%%~da" "%%~pa" "%%~na" "%%~xa" "%%~nxa"
should provide at least a clue. Without an example metaflac command, a more precise response would take excessive research.
From the prompt, try for /? for details. I've just shown with an echo how to construct some possible source/destination components - the rest is a matter of judicious adhesion of strings. Note the use of quotes however to properly cater for spaces in file/directorynames

Batch file that unzips and combines contents

I am trying to unzip some folders and then recombine them, however when I run the batch file it get the following error:
'unzip' is not recognized as an internal or external command,
operable program or batch file.
I am guessing I need to point the batch file to where 7-zip is correct? Any insight would be appreciated thanks!
See below for code:
#echo off
cls
:start
:unzip
echo.
echo Unzipping files
echo.
unzip ELMDB-85308-PI003_OVA_2of5.zip
unzip ELMDB-85308-PI003_OVA_3of5.zip
unzip ELMDB-85308-PI003_OVA_4of5.zip
unzip ELMDB-85308-PI003_OVA_5of5.zip
echo.
:combining
echo.
echo Combining parts
echo.
copy /b ELMDB-85308-PI003_OVA_1of4+ELMDB-85308-PI003_OVA_2of4+ELMDB-85308-PI003_OVA_3of4+ELMDB-85308-PI003_OVA_4of4 ELMDB-85308-PI003.OVA
:done
echo.
echo Done!
echo.
pause
:end
Instead of unzip as a command, try 7z -e.
If you copy 7z.exe to some directory that is on your PATH, then you don't need to specify the directory.
You can see your PATH by executing
PATH
from the prompt. This displays a ;-separated list of directories that are searched after the current directory) for executables.
If 7z.exe is not on your PATH, then you'd need
"c:\wherever\it is\7z" -e ....
(more explaining)
If 7z is in your path, then
7Z -e ELMDB-85308-PI003_OVA_2of5.zip
will unzip ELMDB-85308-PI003_OVA_2of5.zip
And you'd then need to add a line for each of the other files (which you have done).
If 7z.exe is not on your path then you need
"C:\Program Files\7-Zip\7z" -e ELMDB-85308-PI003_OVA_2of5.zip
and reproduce that for each of your files.
You can tell that 7z.exe is in your path by executing 7z from the prompt. You will get either a report stating that it's not recognised (ie it's not on your path) or you'll get a commands-and-switches help report (which means it is on your path)
Since the command 7z did not work, then it's not on your path, so you need the other form.
If you were to copy /b C:\Program Files\7-Zip\7z.exe"c:\windows\system32\" then in all probability you'd have 7z.exe on your path and hence you could use the short form.

Variables in Batch FTP script

In C you can use %username% as a variable for the current user's name for directory listings and such: c:\documents and settings\%username%\
Is there something like this for a batch script?
Using just %username% doesn't seem to help.
I wrote a script that accesses my FTP server so I can load files to the server.
I want my friends to be able to use this script, but I don't want to have to write several different scripts.
Here is what I have so far:
#echo off
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
lcd "c:\documents and settings\%username%\my documents\FTP"
mput *txt
pause
bye
There's gotta be a way
This can be done if you change the batch file so that it creates a script file every time the batch file runs. You can do this by using the echo command to write the script lines to script file, which you can then pass to the ftp command. The reason this works is that echo will expand the %username% variable before writing it to the script file:
#echo off
del script.txt
echo open FTP.server.com>>script.txt
.
[echo rest of script lines to file]
.
echo lcd "c:\documents and settings\%username%\my documents\FTP">>script.txt
echo echo mput *txt>>script.txt
#ftp -s:script.txt
I believe i found a better way, although it's a bit more code.
set "rootdir=%userprofile%\my documents"
set "destdir=c:\
for /f "delims=" %%a in ('dir /b /s "%rootdir%*.txt"') do copy "%%~a" "%destdir%"
And then the usual FTP stuff, including lcd c:\
Ive tested this and it works, although I would like to find a simpler way.
I tried using xcopy but for some reason it doesn't work on my system, the cmd screen just hangs.
Also tried just using copy, but that gave me "can't find file" errors.
Instead of using lcd, a better idea might be to change the working directory in the outer batch file.
#echo off
#pushd "c:\documents and settings\%username%\my documents\FTP"
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
mput *txt
#pause
The only problem with this solution, is that the script itself is no longer in the working directory, and so you need to add a path for that. (Or, put it in the FTP folder ;)
Also, minor pedantry, but this is not actually a correct way to find My documents. In particular, on Vista or Windows 7, User profiles are stored in C:\Users. And, it's possible for users to move My Documents (on my computer, My Documents is located in D:\Mike's Documents)
However, there doesn't appear to be an environment variable that points directly at My Documents, so you will have to make do with this:
"%userprofile%\my documents\FTP"
If the people running this script are running XP and haven't moved their My Documents, then this doesn't really matter.

Resources