read from a .txt file in BATCH, without using literal path - batch-file

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.

Related

Escape characters of a file path argument for a batch file

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!

Escape the content of a variable

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:\=\\%

Batch File - Attrib +r %Var1%

I have a undoubtedly silly problem. I need to change the attribute of a file to read only. I know to use...
atrrib +r c:\somefile.txt
And it works. However in my program I want to use a variable in place of the path to be built up beforehand. Now if I write...
set File=c:\somefile.txt
attrib +r %File%
Then I get an error saying 'attrib' is not recognized as an internal or external command etc.
However if I echo %File% beforehand then I know the path to the file is correct and being read properly.
What is my error? Thanks a lot!!!
Edit:
set File=Main.xaml
set Folder=C:\Users\yef03111\Desktop\His0164\WINDOW\ALS026-01~EDF
set Path=%Folder%\%File%
echo %Path%
However if I change the echo to attrib +r and nothing else...
attrib +r %Path%
I get the 'attrib' not recognized error. This is the current example that is not working. Hope you can spot something from it!
The problem is that you are setting an environment variable called PATH. This is overwriting the system PATH variable which contains the location of the executable files such as attrib. The way it works is that in order to find a program to run, the OS looks up the PATH variable and searches in the folders listed there for executable files with the name of the program you are trying to run. When you change the PATH variable, the OS can no longer find the attrib command.
Change the name of your variable from path to filepath and it will work.
Path is a system variable that tells CMD, Explorer, and CreateProcess where to look for commands.
As you are trashing the system variable CMD no longer looks in system32 for commands like attrib.
set Path=%Folder%\%File%
As a general rule avoid likely names used by the system in naming your own things. A lot of people will do MyFile or ProgPath.
Also commands like attrib will always be found if the current directory is System32. The current directory is always searched first for commands (programs). I suspect RunAs is setting the current directory to System32.
I have tested your script using Cmd on windows 7. Works a treat, so I cant recreate what you are seeing.
I did quite a lot of batch scripting at one point, and I used to get random errors using standard windows notepad. Tell tail signs of issues in the script were whitespace characters. If you are using notepad, switch to using notepad++ to write this and see if you still get errors.

Could someone help to understand this batch script?

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.

Make a batch that detects its own filepath

I need to make a batch file that detects what drive and directory it is in. When I run the the file normally, it is in the correct directory/drive already. But when it is run as admin, it starts in system32. Is there a command that goes to the directory or drive the batch came from?
You could use
Pushd "%~dp0"
This changes the current directory to the path of the batch file.
Quoting the argument makes it safe against special characters in the pathname like "C:\Documents & Settings"
well a workaround is to use \ before the path to give absolute path.
So, if you need to run a file c:\temp\xyz.exe and even if you are in directory c:\winodws\system32 still when you do cd \temp\xyz.exe still the file will run properly

Resources