How to append filename to current directory in Batch file? - batch-file

I want to search a file in the current directory from which the batch is running, append the filename to the directory and include that entire directory as part of command that.
So.....
Directory:
C:\tempfiles\batch
Files in C:\tempfiles\batch
tmp1.txt
tmp2.txt
tmp3.txt
anyname.exe
I want the batch file, run from the directory, to find any .exe file and append it to the directory name, and use that new string as part of a command to copy the .exe file over to another directory. The command will eventually read like this (the FILETRANSFERSW.exe is the file transfer software that is also in the directory):
C:\tempfiled\batch> FILETRANSFERSW.exe "%CD%\tmp4.exe" X:\dest
The .exe file name will be changing so i need to dynamically add the new filename into the above command everytime i run the batch file. Any ideas??

If I read your problem correctly, is it sufficient to use the "for" keyword?
for %a in (*.exe) do FILETRANSFERSW.exe %a X:\dest
You can test the output with something innocuous like:
for %a in (*.exe) do echo [[%a]]
%a ends up iterating over *.exe in the current directory, returning the full file name for each one.

Related

Batch file to run cmd command

I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"

Batch File to zip files - apply to sub directories

I'm looking to get a batch file to apply to all sub directories.
I have a number of folders. Each folder will contain file pairs
xxx1.mp3 and xxx1.cdg,
xxx2.mp3 and xxx2.cdg,
etc.
I have a 7zip batch file that will look for file pairs and create a single zip file xxx1.zip, xxx2.zip and delete the (now) redundant cdg/mp3 files.
However, this will only work if the bat file is run in each individual folder. What I'm really looking for is a switch to add to the bat file that if I run in the root directory, will run through all sub directories also.
The code I currently run is:
FOR %F IN (*.cdg) DO "C:\Program Files\7-Zip\7Z.exe" a "%~nF.zip" "%~nF.cdg" "%~nF.mp3" -sdel
Any help?
FOR /R %%F IN (*.cdg) DO IF EXIST "%%~dpnF.mp3" pushd "%%~dpF"&ECHO("C:\Program Files\7-Zip\7Z.exe" a "%%~nF.zip" "%%~nF.cdg" "%%~nF.mp3" -sdel&POPD
Note that this is a batch line - to execute from the prompt, reduce each %% in metavariable to % (but it's a whole lot easier to put it into a batch by cut-and-paste;saves all those typos...)
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.
Using for /r will iterate through the filenames matching the supplied mask (it's possible to also add a starting directory - see for /?|more from the prompt.)
Since we know the selected full filename exists, first check that the paired file exists, then switch to the required directory, archive, and switch back.

How to delete subpath in batch file?

How can I edit path which i get from command line arguments and delete last subdirectory?
e.g. I run my batch file with argument:
C:\Users\Aga\Desktop\something
and I want to use only
"C:\Users\Aga\Desktop"
part in my batch file.
Add this as the very first line of your batch file:
#If Not "%~dp1"=="" #(Set "Parent=%~dp1"&Call Set "Parent=%%Parent:~,-1%%")
Then use %Parent% throughout your script as necessary, (%Parent% will be the next directory up the tree if the input was a directory or the container folder if the input was a file).

Start command cannot locate file from batch but can from Command Line

I am writing a startup script to open a .pdf file. When I execute the command in cmd window, the .pdf will open just fine, but when I execute the command from a .bat file, it says "Windows cannot find 'myfilename.pdf'. Make sure you typed the name correctly, and then try again."
The command I am using is
start myfilename.pdf C:\Temp
Not really sure what I should change other than perhaps insert change directory to C:\Temp before executing the start command?
When you run your command from the command line I assume you are in the same directory as the pdf file. That is why the file is found.
When you run a .bat file, the starting path is the path of the .bat file. If you have the .bat file in the same directory as the .pdf file, your command will work. If you have the .bat file in a different directory you can first change the current directory to the one that contains the .pdf file, or give the full path to the file like below:
start C:\LocationOfPdfFile\myfilename.pdf C:\Temp
If your batch file is not in the same directory as your file, it cannot be opened. If you specify the path it does not matter in what directory you are.
if exist C:\Temp\myfilename.pdf (
rem file exist an is being opened
start C:\Temp\myfilename.pdf
) else (
rem file doesn't exist
)

What is the current directory in a batch file?

I want to create a few batch files to automate a program.
My question is when I create the batch file, what is the current directory?
Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?
From within your batch file:
%cd% refers to the current working directory (variable)
%~dp0 refers to the full path to the batch file's directory (static)
%~dpnx0 and %~f0 both refer to the full path to the batch directory and file name (static).
See also: What does %~dp0 mean, and how does it work?
It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you'r in cmd, and your current directory is c:\dir3, you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3.
In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:
set mypath=%cd%
echo %mypath% (where %mypath% is the current directory that the batch file is sitting in)
So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:
start %mypath%\Myprog.exe
That would open Myprog from the current folder.
The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:
start "" "C:\AutomatePrograms\Myprog1.exe"
start "" "C:\AutomatePrograms\Myprog2.exe"
start "" "C:\AutomatePrograms\Myprog3.exe"
Say you were opening a file in your current directory. The command would be:
start %cd%\filename.filetype
I hope I answered your question.
It is the directory from where you run the command to execute your batch file.
As mentioned in the above answers you can add the below command to your script to verify:
> set current_dir=%cd%
> echo %current_dir%
%__CD__% , %CD% , %=C:%
There's also another dynamic variable %__CD__% which points to the current directory but unlike %CD% it has a backslash at the end.
This can be useful if you want to append files to the current directory. Also %CD% does not work under disabled extensions environment ,but %__CD__% always works.
With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven't accessed the drive on the current cmd session.
And %__APPDIR__% expands to the executable that runs the current script a.k.a. cmd.exe directory.
It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3, then run the batch, the current directory will be c:\dir3.
Just my 2 cents.
The following command fails if called from batch file (Windows 7) placed on a pendrive:
%SystemRoot%\System32\xcopy.exe /e /i "%cd%Ala" "C:\KS\Ala\"
But this does the job:
%SystemRoot%\System32\xcopy.exe /e /i "%~dp0Ala" "C:\KS\Ala\"
Your bat file should be in the directory that the bat file is/was in when you opened it. However if you want to put it into a different directory you can do so with cd [whatever directory]

Resources