Get filename from string-path? - string

How do I get the filename from this string?
"C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
output:
"test.txt"
I really tried to find this one before posting, but all the results were contaminated, they talk about getting filenames from current dir (I must work with strings only)

Method 1
for %%F in ("C:\Documents and Settings\Usuario\Escritorio\hello\test.txt") do echo %%~nxF
Type HELP FOR for more info.
Method 2
call :sub "C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
exit /b
:sub
echo %~nx1
exit /b
Type HELP CALL for more info.

if your path comes as a parameter, just use:
%~nx1 (1 is for the first param and we suppose it's the first one)
echo %~nx1 would return directly test.txt

Assuming you need the the names of files under the "c:\temp" directory tree (including sub-directories):
FOR /R c:\temp %i in (*.*) do echo %~nxi

Related

Search for a file and open the path in CMD

I would like to achieve the following in CMD:
Will search for a specific filename - This I know how to do, with dir /s filename
Once found it will bring me to that path.
Example: I am now on C:\, if that file was found in C:\test then it will open C:\test in command prompt.
Alternatively, I would like just to copy the found file to a path I will specify. I just don't know how to do it, since I don't know the path where the file will be stored (it's different every time).
Thanks in advance!
I'm a liitle confused with your question but I will try to walk you through a batch code I wrote to help you with this.
dir /b /s "test.txt" > "%userprofile%/Desktop/result.txt"
::find the file path to the file you want to find and insert it in a .txt file
::you made called result.txt (also %userprofile% is a variable that brings you to
::your user directory ex C:/users/admin)
for /F "tokens=*" %%A in (%userprofile%/desktop/result.txt) do (
set var1=%%A
)
::set the variable var1 equal to the first (well... only) line in the file.
::I could explain what that for loop means in detail but just remember that %%A
::is a variable set from what was found when looping through the result.txt.
xcopy /s "%var1%" "C:/wherever/you/want/it/to/go"
did this help??

How to show a string that appears after specific string in a file with a batch?

I need to create a batch file that would show me a string printed after a specific string in some log file.
For example: I have a log file with a line that ends with the string "Calculated number: XX". I want to create a batch file that would go to that log, find this string and print only XX part to the screen (XX is some number that changes every now and then). Any ideas what is the best way to do that?
Help will be much appreciated, thanks in advance!
The format of the string is what comes before and after the part you are interested in - as it can matter how the line is parsed.
This code is robust and will print the number at the end of the string, if it is indeed at the end.
It uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
#echo off
type "file.log" | repl ".*Calculated number: (.*)" "$1" a
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (q23040271.txt) DO (
ECHO "%%a"|FIND "Calculated number: " >NUL 2>nul
IF NOT ERRORLEVEL 1 SET "line=%%a"&GOTO found
)
ECHO target NOT found&GOTO :eof
:found
ECHO %line:~-2%
GOTO :EOF
I used a file named q23040271.txt containing junk text and sample data for my testing.
This relies heavily on the assumption that the very first occurrence of Calculated number:
will be the required ite, and that it will be at the end of the line - no checking is done that it is actually at the end of the line.
replacing
ECHO "%%a"|FIND "Calculated number: " >NUL 2>nul
with
ECHO "%%a"|FINDSTR /e /R /c:"Calculated number: ..." >NUL 2>nul
would perform that end-of-line check (in theory - I haven't checked it) - note the three consecutive dots.
This will be agonisngly slow if there are millions of lines - unless the target string is very early in the file.
The more information you provide, the better a solution that can be devised.

Echo a single number to all .dll files in a directory

In a folder named "drops" I have 3 .dll files. I am looking for a piece of code that can echo the number 0 to each .dll file without having to echo them individually. Basically, I don't want to have to open up each file and change the number to 0... laziness ensues!
for /f %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO echo 0
I have tried using for. Is my code wrong? Thanks in advance.
Edit: Title didn't have anything to do with my question :D
Easy
forfiles /p "C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops" /m "*.dll" /c "cmd.exe /c echo 0 > #path"
That will do it. Also, in ou code, add > %%G Thats the only mistake I found.
Mona
Drop /F, use
FOR %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO ECHO 0
/F will try to open file *.dll.

Batch file: Find if substring is in string (not in a file) Part 2 - Using Variables

In a Windows batch file, I have a string 'abcdefg'. I want to check if 'bcd' is in the string, but I also want each to be in a variable, or pass in a parameter for the string.
This solution comes close, but uses constants rather than variables.
Batch file: Find if substring is in string (not in a file)
try one:
set "var=abcdefg"
set "search=bcd"
CALL set "test=%%var:%search%=%%"
if "%test%"=="%var%" (echo %search% is not in %var%) else echo %search% in %var% found
set "var=abcdefg"
set "search=bcd"
echo %var%|findstr /lic:"%search%" >nul && echo %search% found || echo %search% not found
The solution is to use FindStr and the NULL redirect, >nul.
SET var=%1
SET searchVal=Tomcat
SET var|FINDSTR /b "var="|FINDSTR /i %searchVal% >nul
IF ERRORLEVEL 1 (echo It does't contain Tomcat) ELSE (echo It contains Tomcat)
Save as test.bat and execute with the parameter to be searched, as follows: test Tomcat7
C:\>test Tomcat9
It contains Tomcat

How to set a string from a txt to a variable with cmd

I have a txt file to a specific path that contains one or more line just like
the following:
Directory of c:\folderA\folderB
Directory of c:\folderC\folderD
these are the paths of a program example.exe. I want automatically to find the path that example.exe exists in my computer and to run it from a .cmd script. So far i have succeed to log the paths of existence to a txt. How can I set a variable with only the path of
the first line?
The final result should be var_path=c:\folderA\folderB
A quick and dirty solution is:
#echo off
Call :getFirstDirectory
::do your stuff with %var_path%. For example:
#echo %var_path%
exit /b
:getFirstDirectory
for /f "tokens=3" %%a in (dirs.txt) do (
set var_path=%%a
exit /b
)
Which assumes that dirs.txt is the file containing:
Directory of c:\folderA\folderB
Directory of c:\folderC\folderD

Resources