Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 230 Vote(s) - 3.35 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Batch files: Get absolute path

#1
I receive a file that contains the following:

\direcotry1\directory2\directory3\file1
\direcotry1\file2
\direcotry1\directory2\directory3\directory4\file3
\direcotry1\file4
\direcotry1\directory2\file5
file6

The amount of files in the file and the amount of directories are variable.

What I need is the path only.

\direcotry1\directory2\directory3\ for file1
\direcotry1\ for file2
\direcotry1\directory2\directory3\directory4\ for file3
\direcotry1\ for file4
\direcotry1\directory2\ for file5
and nothing for file6

I used the variable %%~pi which works for all except for the last one. For the last one it returns \cft\
I guess the \cft\ is returned as the program that is calling the bat file launches it from there.

FOR /F %%i in (test.txt) DO (
echo %%~pi
command1
command2
)

Does anyone know how I can avoid that the batfile returns \cft\? I want the bat to return nothing when there's no path.

Thanks a lot in advance for your help.
Reply

#2
It seems this is due to the missing backslash in front of file6.
Isn't it possible to change your inputs to always have a leading backslash?

Your input would then become

\directory1\directory2\directory3\file1
\directory1\file2
\directory1\directory2\directory3\directory4\file3
\directory1\file4
\directory1\directory2\file5
\file6
Reply

#3
You can store the directory you want to ignore in a variable:

FOR %%i in (file) do set ignoredir=%%~pi
FOR /F %%i in (test.txt) DO (
if not %%~pi == %ignoredir% echo %%~pi
)
set ignoredir=

This works as expected. Output:

\direcotry1\directory2\directory3\
\direcotry1\
\direcotry1\directory2\directory3\directory4\
\direcotry1\
\direcotry1\directory2\
Reply

#4
**Update2:**

As Lieven noted, if you can enforce that everything starts with "\" in your test.txt file, then you can use this code:

@echo off
FOR /F %%i in (test.txt) DO (
if not "%%~pi" == "\" echo %%~pi
)

**Update:**

Actually, after reading your question again, I noticed that what you need is an EXIST statement..

FOR /F %%i in (test.txt) DO ( if exist %%i echo %%~pi command1 command2 )

Is this what you need?

**My original post...**

When you call the batch file, simply do:

yourbatchfile.bat >nul 2>nul

Anything going to stdout and stderr (your screen display) will be piped to no where, and no appear. Note that absolutely nothing will be output from that batch file...
Reply

#5
After doing some testing, it appears that `"%%~pi"` will prefix the current directory to every element that does not start with `"\"`. I gather that it assumes this is the case since that would be the file you'd open if you just used "file" - similarly "x\y" would be the file "<current-directory>\x\y".

For example, the following script:

@echo off
for /f %%i in (test.txt) DO (
echo %%~pi
)

when run on the following file:

\directory1\directory2\directory3\file1
\directory1\file2
\directory1\directory2\directory3\directory4\file3
\directory1\file4
\directory1\directory2\file5
file6
x\y
\z

will produce (I'm in the \Documents and Settings\Administrator directory):

\directory1\directory2\directory3\
\directory1\
\directory1\directory2\directory3\directory4\
\directory1\
\directory1\directory2\
\Documents and Settings\Administrator\
\Documents and Settings\Administrator\x\
\

So the answer is simple. Detect first those lines that don't begin with `"\"` and treat them specially. The following script:

@echo off
setlocal enableextensions enabledelayedexpansion
for /f %%i in (test.txt) DO (
set ch0=%%i
set ch0=!ch0:~0,1!
if not "!ch0!"=="\" (
echo.
) else (
echo.%%~pi
)
)
endlocal

generates your desired output, as follows:

\directory1\directory2\directory3\
\directory1\
\directory1\directory2\directory3\directory4\
\directory1\
\directory1\directory2\


\

The setlocal/endlocal is something I put in most of my scripts nowadays since it prevents environment variables from leaking up one level and delayed expansion is brilliant (using `"!"` instead of `"%"`).

I use a ch0 temporary variable to get the first character of the file (`"!ch0:~0,1!"` is a substring operator, "get one character at offset 0 of ch0 variable", and you can use the `"%"` version as well if you're not doing delayed expansion).

Then I compare it to `"\"`. If it is a slash, I echo the `"%%~pi"`, otherwise I just output a blank line as per your spec.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through