This question already has answers here:
Get directory name from path of %CD%
(4 answers)
Closed 5 years ago.
I have a simple batch file that I use to archive files in tar/gzip format. I have placed the batch file in system32 so that I can access it from anywhere.
I open the command window using "shift + right click" in a particular folder where I want the contents of the folder to be archived and enter the name of the batch file (targz.bat). Batch file does the archiving/compressing.
The problem is I use absolute paths. I need a way to get the current directory and the name of the current folder. I can get the current directory with %cd%, but how do I get the folder name?
For example:
set currentdir=%cd% "C:\xampp\htdocs\wordpress"
set currentfoldername= should be just "wordpress"
Actual code:
#echo off
cd "C:\Program Files\7-Zip"
7z a -ttar "C:\xampp\htdocs\wordpress\archive.tar" "C:\xampp\htdocs\wordpress\*"
7z a -tgzip "C:\xampp\htdocs\wordpress\archive.tar.gz" "C:\xampp\htdocs\wordpress\archive.tar"
del "C:\xampp\htdocs\wordpress\archive.tar"
exit
Desired:
#echo off
set currentdir=%cd%
set currentfoldername=
cd "C:\Program Files\7-Zip"
7z a -ttar "%currentdir%\%currentfoldername%.tar" "%currentdir%\*"
7z a -tgzip "%currentdir%\%currentfoldername%.tar.gz" "%currentdir%\%currentfoldername%.tar"
del "%currentdir%\%currentfoldername%.tar"
exit
Using the ~n modifier you can easely get the last element of a path :
for %%a in (%cd%) do set "currentfoldername=%~na"
Related
I am trying to create a batch file that I can place in a folder and have it copy files the directory structure and the files from a networked location. I know this can be achieved using xcopy the issue I am having is it is not placing it in the folder where the batch file was copied. How can this be achieved so that I don't have to rewrite the file for every new folder I want this copied to? Here is what I have so far.
#echo off
Title xcopybatch.bat
REM Copy Directory Structure using xcopy Windows native command
REM -------------------------- Enter Source Directory ---------------------------
set "dir1=K:\GIS\Dallas Area 500-100 Year BFE Maps\_Misc Flood Info\"
REM -------------------------- Enter Dest Directory -----------------------------
set "dir2=K:."
REM ----------------------------------------------------------------------------
xcopy "%dir1%" "%dir2%" /e
pause
REM --------------------------------- Exit --------------------------------------
:end
EXIT /B 0
Based on your comments, you want to dump this script into a folder then run it, it will copy files from your source location, then self destruct (delete itself)?
By the way, if you're clarifying the parameters of a question, you should edit the question to reflect that.
with Xcopy, ommiting the target directory will assume you want the current working directory, but if you want to be sure, just implicitly specify %~dp0 as the target
xcopy "%dir1%" "%~dp0" /e
To have it delete itself, add this line at the very end
DEL "%~f0"
Just as %~dp0 give you the batch file's directory, %~f0 is the file name of the batch file. Because the CLI parses the entire file, it will run from memory, then delete itself.
This question already has answers here:
ROBOCOPY - Copy folders content to a single folder
(6 answers)
Closed 4 years ago.
I'm trying to use a .bat file to go into a folder, and take all photos within it and its subfolders, and place them all into another directory. I know how to copy the folder exactly, with all subfolders remaining in place when copied with
#ECHO OFF
XCOPY E:\FromFolderNameX C:\toFolderNameY /m /y
but I only want all of the photos to be in one folder in the end, no subfolders. Can this be done with a batch file?
I am assuming that you want to copy (not move) the photos from the subtree starting at E:\FromFolderNameX into the directory C:\toFolderNameY.
I am assuming that by "photos" you mean .jpg files.
The one-line interactive command is
for /r E:\FromFolderNameX %p in (*.jpg) do copy /y "%~p" C:\ToFolderNameY
If instead of JPG files you want to copy all files, just replace *.jpg with *.
If instead of an interactive one liner you want a batch file, the core of the batch file would be
for /r "%~1" %%p in (*.jpg) do copy "%%~p" "%~2"
(%1 is the first positional argument = the top of the subtree from where you want to copy the files. %2 is the second positional argument = the destination directory.)
In production, the batch file would probably check that the directories %1 and %2 exist and are really directories; and it should probably accept an optional third argument giving the pattern of the files to be copied.
Enter for /? to read more about how for /r works.
okay, so I have written a little script of code :
for /R %%f in (*.acd) do C:\Users\jelle\Downloads\quickbms\quickbms.exe D:\assetto_corsa_acd.bms %%f "\data"
but the problem is that the file data.acd will be extracted using quickbms.exe to a data folder in the main folder my batch file is located in stead of in the /data folder located in the folder where the data.acd file is, so x:\carname(folder_with_data.acd_in_it)\data
but i can't get that to work,
so my question is how do i get quickbms to extract the acd file to the folder where the acd file is located instead of where the batch file is located?
Also note that quickbms is in my path
Thanks in advance.
Your posted script appears to be running two lines instead of only one.
Based on the following usage:
quickbms.exe [options] <script.BMS> <input_archive/folder> [output_folder]
I would suggest something like this:
#Echo Off
Rem Full path to QuickBMS
Set "eBMS=%UserProfile%\Downloads\quickbms\quickbms.exe"
Rem Full path to BMS script
Set "sBMS=D:\assetto_corsa_acd.BMS"
Rem BMS Options
Set "oBMS=-o -d"
Rem The source directory path to recurse (. is current, %~1 is arg[0])
Set "sDIR=."
Rem The sourcefile extension
Set "sEXT=.acd"
Rem The output directory path (.\ relevant to current, %~2 is arg[1])
Set "tOUT=.\data"
For /R "%sDIR%" %%A In (*%sEXT%) Do "%eBMS%" %oBMS% "%sBMS%" "%%A" "%tOUT%"
Fill in the Set command lines according to the Remarks and give it a try reporting back any issues as necessary.
This question already has answers here:
How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?
(6 answers)
Closed 9 months ago.
I need to write a batch script that will allow me to unzip a .zip file into a new folder that has the same name as the zip file. I cannot assume that I have some program to unzip the file; it has to work as if the computer is brand new.
For example, if I have a group of zip files named a1, b2, c3, and d4, I want their contents to go into 4 different folders labeled a1, b2, c3, d4. It doesn't need to be concise, it just has to work.
Read excellent topic How can I compress (zip) and uncompress (unzip) files and folders with batch file without using any external tools? written by npocmaka.
I suggest to use the second solution: Using Shell.Application
The task can be done with zipjs.bat also written by npocmaka with a batch file with just a few lines:
#echo off
for %%I in (*.zip) do call "%~dp0zipjs.bat" unzip -source "%%~fI" -destination "%%~dpnI\" -force yes
for /F "delims=" %%D in ('dir /AD /B "%TEMP%\*.zip" 2^>nul') do (
%SystemRoot%\System32\attrib.exe -h "%TEMP%\%%~D"
rd /S /Q "%TEMP%\%%~D" 2>nul
)
The hybrid batch file zipjs.bat must be in same folder as the batch file with the two lines above.
Running in a command prompt window call /? explains %~dp0 which means drive and path of argument 0 of batch file without surrounding quotes ending always with a backslash. Argument 0 of a called batch file is the name of the batch file, see also What does %~dp0 mean, and how does it work?
Help of command for displayed on running in a command prompt window for /? explains
%%~fI - full file/folder name without surrounding quotes, and
%%~dpnI - drive, path and name of a file/folder without surrounding quotes and without extension
for loop variables like call /? does for batch parameters/arguments.
The second for loop finds all subdirectories in directory for temporary files with .zip in directory name, removes the hidden attribute and deletes those subdirectories. This is necessary as zipjs.bat first extracts each file into a newly created subdirectory in directory for temporary files and leaves them there after successful extraction.
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]