I have this var (source) which looks like this:
\\xxxxx\xxxx\xxx\Companies\test\in\Th FIle Test - Copy - Copy.mp4
I would like to remove everything before "\" character. So it would look like this:
Th FIle Test - Copy - Copy.mp4
I looked at the trim function but not joy. I am using Batch, so I have a bat file.
I believe you can just use regular expressions. I'm unsure of the exact syntax for whatever program you're using, but the regex to search and replace should be relatively easy.
Replace .*\\ with '' (nothing)
https://regex101.com/r/hQ4hB9/1 <-- you can play around with this. Good website for testing regular expressions.
The easiest way to get the leaf of the path is by using enhanced for variable references.
set "var=\\xxxxx\xxxx\xxx\Companies\test\in\Th FIle Test - Copy - Copy.mp4"
for %%I in ("%var%") do set "filename=%%~nxI"
echo %filename%
See the last couple of pages of help for in a console window for more information.
Related
So I'm trying to make a search engine that, when you input a search string, replaces the spaces with a "+", and it'd be helpful if someone pointed out which commands I can use to achieve that.
So far I haven't found anyone that has the same question, which is why I'm posting it here. I've found someone with a way to detect spaces in a variable:
if not "%VAR%"=="%VAR: =%"
but no way to replace them.
Any hints?
P.S.: Please, do not recommend me PowerShell or other scripting languages/methods like I saw some people do, I have my reason for using batch and I'm going to stick to it.
So let me explain how substitution works.
set "var=This is a line with spaces"
set "var=%var: =+%"
echo %var%
in the second set we set var to %var% again, but we use substitution of spaces. Everything after : up to = is the search function and everything after = up to the % is the replace function. So %var: =+% means find all the space and replace with + Hence the outcome of the above code will be:
This+is+a+line+with+spaces
Obviously the variable can be manipulated multiple times:
set "var=This is a line with spaces"
set "var=%var: =+%"
set "var=%var:spaces=pluses%
echo %var%
you can also use the substitution to do comparisons without doing substitution using set:
set "var=This is a line with spaces"
if /i "%var: =+%"=="This+is+a+line+with+spaces" echo Matched!
I suggest you read the help by running set /? from cmdline.
I have created a small batch file for windows, and I am having a small issue:
I defined a variable which contains a string, the path of a source file:
D:\git_repos\ALM_Sandboxs\Sensor_Config\StateManagement\ssm_state.cpp
I ´d like to extract the path without the file name:
D:\git_repos\ALM_Sandboxs\Sensor_Config\StateManagement\
Since the path can change, and so does the file name, I am having difficulties to do this. There are many ways, but I wasn´t able to use any of them:
The first one would be to count charaters of the file name:
set NumberOfCharacter
and do
set FilePath=%FilePathTemp2:~,%NumberOfCharacter%%
But this doesn´t work. I can only use number with this syntax:
set FilePath=%FilePathTemp2:~,-18% ==> This works, but I´d like to read 18 from a variable
I have read that the batch interpreter can´t resolve the % pairs, so I tried
for /l %%x in (1,1,%strlength%) do set %FilePathTemp2%=%FilePathTemp2:~,-1%
It doesn´t work either, because the for loop seems not able to change FilePathTemp2 with a global scope.
I would be thankfull for any help
for %%a in (D:\git_repos\ALM_Sandboxs\Sensor_Config\StateManagement\ssm_state.cpp) do echo %%~dpa
(this is batch file syntax. To use it directly on command line, replace both %% with a single %)
The usage of modifiers (like %%~dpa) and which modifiers are possible is explained in for /?
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!
Learned batch a while ago in school, haven't ever used it until now b/c I had an idea. Just for fun I wanted to mess around with powercfg batteryreport with my new laptop, but I want to archive. I was going to try and figure out how to have the file powercfg batteryreport spits out changed in some sort of numerical order but I don't even know where to begin, so I decided to just make a new line that takes the current file created and adds the date. All of this is taking place inside of a special folder I created, so pathing isnt necessary.
#echo off
powercfg batteryreport
rename "battery-report.html" "batteryreport %date%.html"
This exact script works without the date variable, but never with it in, but of course I need a variable present in order to have multiple reports saved, as opposed to it writing over itself every time. I've tried messing with all spacings, quotes vs no quotes, no luck. Help (or a better way, preferably with explanation) would be greatly appreciated.
In all likelihood, your date format contains / which is illegal in a filename.
Use %date:/=-% in place of %date%. this converts each / to - (see set /? from the prompt for docco)
Equally, you could use %time::=.% o convert the time to a version usable in a filename.
To remove dayname, you need to use an intermediate variable:
set "dateonly=%date:* =%"
ren ... "...%dateonly:/=-%..."
The * means "all of the characters up to and including the first space" and this is replaced by nothing (the string following the =)
see set /? from the prompt for details.
Sorry to ask a question which will be an easy one for many of you I guess...
I'm a student in a CG School, but for the need of my project I need to code a simple .bat file to run on many computers. Everything works pretty fine, but now I need to simplify it to gain extra time setting up the .bat.
We have ALWAYS a Path like that :
D:\Project\Production\Shot\Render\Room\maya\scenes\Room_01.mb
I need two variables which store :
project : D:\Project\Production\Shot\Render\Room\maya
scene : Room_01
I think I need a loop which analyzes the path backward and sets the path after seeing 2 \ in order to avoid the file name, and the scenes folder. And I need an other thing to store all the file name ######.mb removing the .mb
I tried many things with substring forum, but it's kinda black magic for me...
So if anyone could help, I would be so grateful to you ! :)
Thank you in advance.
An easy solution is to use a for command. It will allow you to retrieve different properties of a file system reference.
set "fileReference=D:\Project\Production\Shot\Render\Room\maya\scenes\Room_01.mb"
for %%a in ("%fileReference%") do set "scene=%%~na"
Where %%~na is the name of the element being referenced by the for replaceable parameter %%a
for %%a in ("%fileReference%\..\..") do set "project=%%~fa"
Where %%~fa is the full path of the element being referenced by the for replaceable parameter %%a
A for loop is typically needed here. The first for loop grabs the last element of the string when split by a backslash. The path is then modified to have the file name removed from it, and then another for loop grabs the end off that string in the same manner.
#echo off
set my_path=D:\Project\Production\Shot\Render\Room\maya\scenes\Room_01.mb
for %%F in ("%my_path%") do set file=%%~nxF
echo.%file%
call set modded_path=%%my_path:\%file%=%%
for %%F in ("%modded_path%") do set folder=%%~nxF
echo.%folder%