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"
I need to delete patata.pat files from about 2000 *.rar files, with most of them being inside of \unknown folder\patata\ or \patata\ folders in those rar files, but could be in even deeper directories.
I've tried with no luck
for %%f in (*.rar) do "%ProgramFiles%\WinRAR\RAR.exe" d "%%f" patata.pat
and
for %%f in (*.rar) do "%ProgramFiles%\WinRAR\RAR.exe" d "%%f" %~dp0\patata\patata.pat
I guessed that the last wouldn't work.
How to delete a specific file in an unknown directory from RAR files?
This task can be done with a batch file with the following single command line:
#for /F "eol=| delims=" %%I in ('dir "%~dp0*.rar" /A-D /B 2^>nul') do #"%ProgramFiles%\WinRAR\Rar.exe" d -idcd "%~dp0%%I" "patata.pat" "*\patata.pat"
This command line works even on FAT16, FAT32 and exFAT drives without processing a *.rar file more than once.
It would be also possible to use the following command line in the batch file on execution on an NTFS partition.
#for %%I in ("%~dp0*.rar") do #"%ProgramFiles%\WinRAR\Rar.exe" d -idcd "%%I" "patata.pat" "*\patata.pat"
This command line processing all *.rar files in directory of the batch file does not work correct on FAT16, FAT32 and exFAT drives because of the list of directory entries and the order of the *.rar directory entries changes on each execution of Rar.exe in case of a RAR file is modified because of deleting patata.pat from root of the archive or any directory inside the archive or the entire RAR file if not containing anything else than patata.pat. So RAR files are processed more than once and some RAR files could be skipped by mistake on using this command line on drives with FAT16, FAT32 or exFAT file system.
The difference is that the first command line results in executing by FOR in background with %ComSpec% /c with the DIR command line appended as additional arguments one more command process which executes the DIR command to output all RAR file names with just file name and file extension to STDOUT of the background command process. This output is captured by FOR and next processed line by line. So the changes on the directory entries list caused by Rar.exe do not matter because of a list of file names in memory is processed by FOR instead of the list of directory entries changing during the loop iterations.
The trick is to specify patata.pat once without any path to delete this file in root of a RAR file and once more with *\patata.pat to delete it from RAR file with any path stored in the RAR file. *\ for any path is something special explained in Rar.txt which is the manual of WinRAR console version Rar.exe.
The RAR command d to delete a directory or file from a RAR file and the switch -idcd to suppress the copyright and Done messages are documented also in text file Rar.txt.
The directory path of the batch file referenced with %~dp0 always expands to a string ending with \. For that reason %~dp0 should be never concatenated with an additional backslash with a folder or file name as this results in having finally on execution two backslashes in series and Windows kernel must remove the additional backslash before passing the full qualified file/folder name to the file system.
I need to write a batch file that will search a folder for all dll's, take their name do some processing with such. So for example a folder containing
File1.dll
File2.dll
File3.dll
within C:\temp
should return
File1
File2
File3
I would like to do some further processing then with these filenames.
Any idea how I might do such?
for /r %i in (*.dll) do #echo %~ni
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]
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.