In my batch script I need to pass the current directory path to a program, escaped.
So, when my batch script is running in C:\Program Files\ it has to pass C:\\Program Files\\ to the called program.
How can I do that? Many thanks.
Background: The called program expects a replacement string for a regex operation, thus it will expect a group parameter when using \ only. The data that will be targeted by the regex operation is used by a software that won't accept relative paths or environment variables.
My first guess is %~dp0 the current path where the batch are located
But there is also %CD%, the current working directory.
try something like:
#echo off
setlocal
set "x=%CD%"
set "x=%x:\=\\%"
echo %x%
as mentioned by #joey you can directly call the current directory like this %CD:\=\\%
Related
When calling a batch script inside Jenkins file it doesn't catch/read the value after asterik *.
Below are the few lines of my batch code:
SET HOUSE=GREENVILLA
SET NUMBER=10
SET HOUSE_NAME=%HOUSE%-%NUMBER%.*.y
I only see that HOUSE_NAME is set to GREENVILLA-10.*.y instead of GREENVILLA-10.44.6.y.
The actual path is \\servername\c\house\name containing the particular file greenvilla-10.44.6.y. I expect greenvilla-10.44.6.y being assigned to environment variable HOUSE_NAME.
Batch file coding is a new thing for me. I'm not sure about missing some basic things here.
The Windows command processor cmd.exe processing a batch file does not replace argument strings containing a wildcard by all files/folders found in file system matching the wildcard pattern as Linux shell interpreters do, except the argument string containing the wildcard is enclosed in '. There must be used code in batch file to get file/folder names matching a wildcard pattern.
set "HOUSE=GREENVILLA"
set "NUMBER=10"
set "HOUSE_NAME="
for %%I in ("\\servername\c\house\name\%HOUSE%-%NUMBER%.*.y") do set "HOUSE_NAME=%%I"
if defined HOUSE_NAME echo "%HOUSE_NAME%"
The last found non-hidden file in directory \\servername\c\house\name matching the wildcard pattern GREENVILLA-10.*.y is assigned to environment variable HOUSE_NAME with full path because of having specified the wildcard pattern with a folder path.
What is the last found file matching the wildcard pattern is determined by the file system. NTFS returns a list of files matching a pattern different to FAT16, FAT32 and exFAT drives. However, I suppose the directory contains always only one file matching the wildcard pattern.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
if /?
set /?
solution was I have to replace/remove any extra characters(space,*), when passing an input. I had a special character(") passed in with the number,where batch thought thats the end of the path and didn't give me the other values.
used Inputval = Inputval.replaceAll("\\W", "") before passing the value to the batch script in jenkinsfile.
Basically, i would like to use the type command, but I can't provide the actual path.
Currently my attempt was
type "./TESTS/Test1.txt"
but I'm assuming that since it's a relative path, it can't work.
I've run into the same issue with copy and xcopy.
I have been unable to solve this issue or find anything online.
Is there way to do this?
EDIT:
To clarify, I am trying to get my .bat file, to read the contents of a .txt file located in a subfolder (meaning the subfolder and the .bat file are in the same folder), and print it to the console.
Since you've now edited your question but seemingly not provided feedback on my earlier comment, here it is as an answer.
Windows and it's command interpreter, cmd.exe uses the backslash \ as its path separator.
Although many commands seem to accept the forward slash interchangeably, Type isn't one of those.
Additionally .\ is relative only to the current working directory, and in cmd.exe is unnecessary, though valid.
The following should therefore work as you intended:
Type TESTS\Test1.txt
Type "TESTS\Test1.txt"
Type .\TESTS\Test1.txt
Type ".\TESTS\Test1.txt"
If the location you are using is being received in the batch file with the forward slashes, you could set it to a variable, then expand that variable substituting the forward slashes for backward slashes:
Set "Variable=./TESTS/Test1.txt"
Type "%Variable:/=\%"
It may be necessary, depending upon the code we cannot see, to navigate to the batch file directory first, since it may not necessarily be the current working directory at the time of the invokation of those commands.
To do that use:
CD /D "%~dp0"
%~dp0 provides the folder, where your batchfile resides (including the last \) (does of course only work inside a batch file). So:
type "%~dp0Test\test1.txt"
is exactly what you want: <folder_where_my_batchfile_is\><subfolder_Test>\<File_test1.txt>
independent of your "working folder" (where the batchfile might have cd or pushd to).
Wouldn't it basically work by using %CD%? Like, TYPE "%CD%/Subfolder/Test1.txt"? %CD% is the windows variable for "Current Directory" and should be set to whatever directory the batch file is working in and since you're trying to access a folder within the same directory this should work. You're question is quite unclear, however, and I hope I'm not misinterpreting.
I was making a batch file to take dragged-and-dropped folders for program input. Everything was working fine until I passed a folder, which for the sake of this post, called foo&bar.
Checking what %1 contained inside the batch file looked like C:\path\to\foo or C:\path\to\foo\foo. If the file path were in quotes it would work, so the only working code that slightly takes this into effect is :
set arg1=%1
cd %arg1%*
set arg1="%CD%"
Which changes directory to the passed argument using wildcards. However this only works once for if there is another folder with un-escaped characters inside the parent folder, passing the child folder would result in the parent folders' value.
I tried the answer of this post, which suggests to output the argument using a remark and redirection statement during an #echo on sequence. However no progress occurred in rectifying the problem. Any suggestions?
To recap, I am looking for ways to pass folders with un-escaped characters as arguments to a batch file. The implementation should preferably be in a batch file, but answers using VBScript are welcome. However the starting program must be in batch as this is the only program of the 3 that accepts files as arguments.
To test this, create a batch file with following code:
#echo off
set "arg1=%~1"
echo "the passed path was %arg1%"
pause
Then create folders called foobar and foo&bar. Drag them onto the batch file to see their output. foo&bar will only return C:\path\to\foo.
OK, so the problem is that Explorer is passing this as the command line to cmd.exe:
C:\Windows\system32\cmd.exe /c ""C:\path\test.bat" C:\path\foo&bar"
The outermost quotes get stripped, and the command becomes
"C:\working\so46635563\test.bat" C:\path\foo&bar
which cmd.exe interprets similarly to
("C:\working\so46635563\test.bat" C:\path\foo) & bar
i.e., bar is considered to be a separate command, to be run after the batch file.
The best solution would be to drag-and-drop not directly onto the batch file but onto, say, a vbscript or a Powershell script or a plain old executable. That script could then run the batch file, either quoting the argument appropriately or putting the directory path into an environment variable rather than on the command line.
Alternatively, you can retrieve the original command string from %CMDCMDLINE% like this:
setlocal EnableDelayedExpansion
set "dirname=!CMDCMDLINE!"
set "dirname=%dirname:&=?%"
set "dirname=%dirname:" =*%"
set "dirname=%dirname:"=*%"
set "dirname=%dirname: =/%"
for /F "tokens=3 delims=*" %%i in ("%dirname%") do set dirname=%%i
set "dirname=%dirname:/= %"
set "dirname=%dirname:?=&%"
set dirname
pause
exit
Note the exit at the end; that is necessary so that cmd.exe doesn't try to run bar when it reaches the end of the script. Otherwise, if the part of the directory name after the & happens to be a valid command, it could cause trouble.
NB: I'm not sure how robust this script is.
I've tested it with the most obvious combinations, but YMMV. [It might be more sensible to use delayed expansion exclusively, I'm not sure. It doesn't seem to be necessary except in the first set command. Jeb's answer here might be a better choice if you're going this route.]
For the curious, the script works like this:
Load the original command line into dirname [necessary for the reason pointed out by jeb]
Replace all the & characters with ?
Replace all the quote marks with *
If a quote mark is followed by a space, suppress the space.
NB: it is necessary to suppress the space to deal with both the case where the path contains a space (in which case Explorer adds quote marks around it) and the case where it doesn't.
Replace all remaining spaces with /
NB: ? * and / are illegal in file names, so these replacements are safe.
At this point the string looks like this:
C:\Windows\system32\cmd.exe//c/**C:\path\test.bat**C:\path\foo?bar**
So we just need to pull out the third asterisk-delimited element, turn any forward slashes back into spaces and any question marks back into ampersands, and we're done. Phew!
I have been searching for a way to add a %var% to the system PATH in Windows 7. I am currently doing the following:
setx PATH "%PATH%;%PATH1%;%PATH2%;" /m
This works as expected. It adds whatever is in those system variables to the Path. However, I want to add the actual string '%PATH1%' to the system Path so if I make changes to the %PATH1% variable, it gets reflected in the PATH.
How do I do this?
EDIT
Currently, in Windows, I have these system variables:
Path1 = c:\path
Path2 = c:\another\path
I am using a batch process to create these system variables. I want to add these new variables to the PATH like this:
PATH = <other paths>;%PATH1%;%PATH2%
Currently, it shows up as:
PATH = <other paths>;c:\path; c:\another\path
My reasoning is that I want to edit the system variable and have the PATH updated at the same time.
You can add %var% literally, but that will not work as you expect.
Then cmd.exe tries to find files in a directory called %var% which normally will not exist on your system.
It will not expand %var% inside your path variable.
If you try it (on the console) and a batch exists in C:\temp with the name "myTest.bat"
set path=%path%;%^var%
set path
set var=C:\temp
set path
myTest
The output will be
C:\windows;....;%var%
C:\windows;....;%var%
Can't find internal or external command "mytest"
I'm reading a batch file, but I do not understand it, can someone help to explain?
As I understand %0 is is the name of batch file, can we iterate on it? or is it a convenient way to represent a folder?
I can not find the variable %BatchPath% in the file, where do you think it's defined?
And it seems APATH is defined in the two loops?
for %%x in (%0) do set APATH=%%~dpsx
for %%x in (%BatchPath%) do set APATH=%%~dpsx
pushd %APATH%
You can iterate over a single value. It just means the set statement is executed once. The ~dps then strips the file name, so that only the directory remains.
The first line performs this action on %0, indeed the path and name of the current script.
The second line performs the same action on a given variable, Now that is the fun part, because if %BatchPath% is empty, nothing gets iterated, so the set statement on that line is not executed at all.
So effectively, it stores a directory, which is the directory of the script by default, but can be overridden by explicitly assigning a path to %BatchPath% before calling this script.
pushd allows you to save a directory, so you can return to it later using popd. It allows the script to jump to another directory an be able to restore the shell to the original directory before it terminates.
%0 is the current batch file.
%%~dpsx gives the current batch file's
short path here its giving the Drive name for eg "D:\"
Pushd Stores the name of the current directory for use by the popd command before changing the current directory to the specified
directory.
APATH is some variable used to store the path.
so basically the script is fetching details about the script file name , its drive location and storing it to be used as location from which last batch file ran or something like that.