Reputation: 1
I need to create a script, that will remove first six characters from a huge amount of files (with different names). I tried this example from another question, but I want to understand it better:
@echo off
setlocal enabledelayedexpansion
set X=3
for %%f in (*) do if %%f neq %~nx0 (
set "filename=%%~nf"
set "filename=!filename:~%X%,-%X%!"
ren "%%f" "!filename!%%~xf"
)
popd
I can see, that modifying the X in -%X%! I actually trim the X number of first characters from all the files in the folder. I don't know what the ~%X%, is - I can only see that if it is not a number higher than 0, the script won't run. I also don't know what set X=3 is - I can only see that there is no difference whether it is present in the bat file or not. Could anyone please explain to me the syntax of this file?
Thanks in advance!
Upvotes: 0
Views: 8180
Reputation: 20474
That method is called Substring.
You can see a lot of examples and explanation here: http://ss64.com/nt/syntax-substring.html
The first number is the start index, and the second number is the last index of.
Example:
@echo off
Set "Filename=TestFile.txt"
Set "Filename=%Filename:~0,-4%"
Echo %FILENAME%
pause
In that code we start reading from index "0" (the first letter of the string), and stop reading at "-4", then we substract from 0 to -4 so the result is: "TestFile"
I hope this helps.
Upvotes: 1