Using full or relative path in a batch file - batch-file

I have created a batch script that requires the location of 2 files to be passed into it. I would like it to work regardless of whether the user gives the full file path or relative file path of the files to be read in.
I've tried the following
set Var1=%~dp0%1
set Var2=%~dp0%2
which only works if relative file path is given. Without %~dp0obviously only works if full path is given.
Is there a way for either the full or relative path to be given and for my batch file to work.
Thanks

Related

Determine end of path with a wildcard in .bat

I'm trying to copy a folder with a variable path to a fixed path using a .bat file. Basically trying to retrieve a folder from a shared server to use it locally. The path to the variable location can mostly be solved by retrieving an user input. However, the user input will only cover a large part of the to retrieve path. At the end there is still a variable component. That's where I'm stuck. Is there any way to use a wildcard in .bat to retrieve a folder? Searching stackoverflow teaches me that it can only be used for files and not folders, is there a workaround?
Code:
#echo off
set /p enddirectory=enter your folder:
set fixedpath=C:\xxx\xxx\folders\
set endpath=%fixedpath%%enddirectory%\ (*wildcard to be added here)
Xcopy /e %endpath% "C:\xxx\localfolder"
For example:
Server path : C/xx/ABC-1234567
User input: ABC
Defining the first path of the server path works, how can I get the full path? (1234567 is variable)

How to get the relative path segment for Apache Camel File2

I'm trying to build a file based integration where files are dumped in one of the subdirectories of a main directory for processing. I need to get the name of the sub-directory to know which client the file is for. So if I have:
/uploads/foo/bar.txt
I need to process that file and know that it's for client "foo". I'm not sure how to get that part and set it as a header for the processor that processes the bar.txt file. I've got it picking up files and processing, now I need to add in this piece.
Anyone have ideas for me?
You can get most of this information in the header of the exchange. In your situation as you are consuming the file the following items are avlable:
CamelFileName: Name of the consumed file as a relative file path with
offset from the starting directory configured on the endpoint.
CamelFileNameOnly: Only the file name (the name with no leading
paths).
CamelFileAbsolute: A boolean option specifying whether the consumed
file denotes an absolute path or not. Should normally be false for
relative paths. Absolute paths should normally not be used but we
added to the move option to allow moving files to absolute paths. But
can be used elsewhere as well.
CamelFileAbsolutePath: The absolute path to the file. For relative
files this path holds the relative path instead.
CamelFilePath: The file path. For relative files this is the starting directory + the relative filename. For absolute files this is the absolute path.
CamelFileRelativePath: The relative path.
CamelFileParent: The parent path.
CamelFileLength: A long value containing the file size.
CamelFileLastModified: A Date value containing the last modified
timestamp of the file.
You can query these headers for the information you are looking for using the following example as guidelines:
<log message ="${header.CamelFileAbsolutePath}"/>
See the file component documents at the Camel website for more details.

Calling batch file reports: "The system cannot find the path specified"

I have one batch file that suppose to call another. I read that call command is used in this case. Although error message appears:
The system cannot find the path specified.
The path is not wrong 100% sure. This is the caller run.bat
#echo off
call xslt\projects\asp-bus\implementation\batch\ant-start.bat
pause
and this is the called ant-start.bat
set ant="../../../../infrastucture/apache-ant-1.10.0/bin/ant.bat"
call %ant%
pause
In each batch file, the paths are relative to the working folder you are running the batch file from, not the folder that contains the batch file itself. You either need to use absolute paths (e.g. starting with C:\), or to make sure that when each batch file is run from a working folder where the relative paths make sense.
If you're launching run.bat from a Windows shortcut, you can set the "Start In" folder from the shortcut's Properties dialog. When ant-start.bat is called, it will run from the same folder as run.bat.
To fix the problem, you might need to change ant-start.bat to
set ant="infrastucture/apache-ant-1.10.0/bin/ant.bat"
call %ant%
pause
Alternatively, you could put a cd command in one of the batch files, to force it to use an appropriate working folder.
Bear in mind that if you set the %ant% variable to a relative path as above, using the variable will only work from a folder where that relative path makes sense.

How do I check if a file exists inside a certain directory in Batch?

How would I go about making a program that scans for a certain file inside a certain directory, not on the C:\ drive?
I know that IF EXIST does find a certain file, but I want it to to only try to find a certain file inside a certain directory, not on the C:\ as a whole.
Add the full path and filename to the IF EXIST and it will only check in that specified folder.
IF EXIST "C:\folder\folder2\file.ext" ...
Edited by Magoo - added quotes which are required if the full path contains spaces or some other characters

How do I resolve a relative path to an absolute path in C?

I have a C console app where I need to output the absolute path to a file given a (possibly) relative path. What is the best way to do this in C in a Windows environment?
I think you are looking for _fullpath().
GetFullPathName should help you on Windows.
GetFullPathName merges the name of the current drive and directory
with a specified file name to determine the full path and file name of
a specified file.

Resources