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.
Related
call :deleteSelf&exit /b
:deleteSelf
start "" /D "C:\Windows" /MIN cmd /c RD /S /Q "C:\Windows\test"&&exit /b
This is the code I use. Batch file running it sits within C:\Windows\test
The file is successfully deleted, along with any other files in the directory, but not the directory itself. Does anyone know some way to solve this issue? I'm rather stumped.
You will need, at least, to
leave the current batch file so it is not open
ensure your current active directory is not the one you want to remove
so, if you follow the already pointed dbenham's approach for leaving the current batch file you could use something like
((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0")
That is,
the (goto) will generate an error that will leave current batch file execution
we change the current active directory to the parent of the folder where the batch file is stored
it the active directory has been changed, we try to remove the folder that holds the batch file
Of course, if there is another process/file locking the folder you will not be able to remove it.
surely it's not as easy as adding the following line to your batch file:
cd c:\
rd c:\windows\test
I am working on a batch application where I have to get the current directory path via command prompt.
I can get the file location as:
C:\Users\Username\Downloads\Images\dance.png
or current directory as:
C:\Users\Username\Downloads\Images\stock_images\
I have to get the desired directory path as:
C:\Users\Username\Downloads\Images\
I have looked around on stackoverflow for the solution but could not find something useful.
So, how can I get parent directory of a file or subdirectory using batch script?
I've posted it a number of times, but can't locate it for the moment
#ECHO OFF
SETLOCAL
SET "name=C:\Users\Username\Downloads\Images\stock_images\dance.png"
FOR %%a IN ("%name%") DO FOR %%b IN ("%%~dpa.") DO ECHO Grandparent=%%~dpb&ECHO parent=%%~nxb
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="
I wrote this script to first install the msi and then copy my application to a temporary directory. But none is working. When the windows.bat file is executed it failed to find package\ and also dist directory
1) User downloaded and execute the windows.bat file which has following tree:
C:\Users\Username\Downloads\windows.bat
C:\Users\Username\Downloads\package\<.msi files>
C:\Users\Username\Downloads\dist\<application files>
2) windows.bat contain below:
msiexec /I "package\files.msi" /qb
set temp=%TEMP%
echo %temp%
xcopy dist %temp% /e /h /R
All fails, to run with windows.bat file. What am i doing wrong?
You need to add the following line to the beginning of your batch file:
cd c:\users\username\downloads
alternatively, you can do this:
cd /d %~p0
%~p0 will take argument #0 (the full path to the batch file) and extract the path from it. The /d option will make sure to also change the current drive if the given path contains a drive specification.
Ok, think I know what's wrong. msiexec.exe runs from the Windows System folder (e.g. C:\Windows\Systeme32) so when you pass it the name of the msi file to install, you need to include the full path to it.
So, using #MikeNakis' info about getting the current path within the batch file, try this (slight tweak to use %~dp0 for just the directory, so not including the batch file name too):
msiexec /I "%~dp0\package\files.msi" /qb
Ensure you are in the correct directory to start off with
CD /d c:\users\%USERNAME%\downloads
As the first line in your batch file
Is there a way to find a file on the C: of a computer , using a DOS command, without having to CD to the root of the C: drive? I would like to do it using the DIR and the FINDSTR command only.
Ultimately, my goal is to search for a file and then store the parent directory name in a ENV variable without changing directories and hopefully without creating a temp file.
Have a look at How to find a file in MS-DOS.
dir *bob*.* /s
See also List of DOS commands
This gets the complete file name with directory in a useable form.
dir C:\FILENAME.EXT /s /b
c:> findstr /s /i *.ext
REM finds in drive c: every file called *.ext containing (case insensitive)