Remove file extension in batch
Solved
joujou07
Posted messages
257
Registration date
Status
Membre
-
merzouki -
merzouki -
Hello,
Here’s what I would like to do in batch (MS-DOS command line):
I have a file machin.mpg
Running the command line
I end up with a file machin
Just like that.
Here’s what I would like to do in batch (MS-DOS command line):
I have a file machin.mpg
Running the command line
I end up with a file machin
Just like that.
8 réponses
Good evening,
Regarding the delayed expansion of variables, I can only direct you to the online help (HELP SET, 2nd screen)
For the use of the command, it's as I've written it, or at least I think so, as I haven't used it with a variable %%X in this case. Moreover, while searching, I just realized that there is a simpler way:
SET "name_without_extension=%%~nX" (Here X is the name of your variable in the FOR loop) At each iteration of your FOR loop, the variable name_without_extension will be populated with the file names without the extension.
You will find the explanations in the online help (HELP FOR 2nd screen), I'm reproducing part of it:
Furthermore, the substitution of FOR variable references has been improved.
You can now use the following optional syntax:
--
Sincerely.
Christian.
Regarding the delayed expansion of variables, I can only direct you to the online help (HELP SET, 2nd screen)
For the use of the command, it's as I've written it, or at least I think so, as I haven't used it with a variable %%X in this case. Moreover, while searching, I just realized that there is a simpler way:
SET "name_without_extension=%%~nX" (Here X is the name of your variable in the FOR loop) At each iteration of your FOR loop, the variable name_without_extension will be populated with the file names without the extension.
You will find the explanations in the online help (HELP FOR 2nd screen), I'm reproducing part of it:
Furthermore, the substitution of FOR variable references has been improved.
You can now use the following optional syntax:
%~I - expands %I by removing quotes (") %~fI - expands %I to a recognized path name %~dI - expands %I to the drive letter only %~pI - expands %I to the path only %~nI - expands %I to the file name only %~xI - expands %I to the file extension only %~sI - extended path containing only short names %~aI - expands %I to file attributes %~tI - expands %I to file date/time %~zI - expands %I to file size %~$PATH:I - searches the directories in the PATH environment variable and expands %I to the name of the first recognized file found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to an empty string You can combine modifiers to achieve compound results: %~dpI - expands %I to the drive letter and path only %~nxI - expands %I to the file name and extension only %~fsI - expands %I to the full path name with only short names %~dp$PATH:i - searches the directories listed in the PATH environment variable for %I and expands to the drive letter of the first found. --
Sincerely.
Christian.
I made a little something to illustrate my points ...............
--
Best regards.
Cchristian.
SETLOCAL ENABLEdelayedExpansion SETLOCAL ENABLEextensions CLS echo. CD C:\Documents and Settings\Christian\My Documents\BATCH_files rem for my test: FOR %%X IN (aaa.txt) DO ( FOR %%X IN (*.*) DO ( SET "name_without_extension=%%~nX" REN "%%X" "!name_without_extension!.flv" ) ENDLOCAL
--
Best regards.
Cchristian.
Good evening,
This command chaineZ:~0,-%n% should solve your problem:
Example:
In your case, try this. Be careful with delayed expansion to enable (!!):
Best regards.
Cchristian.
This command chaineZ:~0,-%n% should solve your problem:
Example:
SET chaineZ=abcABC123ABCabc echo --------------------------------------------------------------------------- . echo Command: "echo %%chaineZ:~0,-%n%%%" echo Extracting all the characters from the string %chaineZ% except the last %n%. echo The result is: !chaineZ:~0,-%n%! echo --------------------------------------------------------------------------- . Result: --------------------------------------------------------------------------- . Command: "echo %chaineZ:~0,-4%" Extracting all the characters from the string abcABC123ABCabc except the last 4. The result is: abcABC123AB --------------------------------------------------------------------------- .
In your case, try this. Be careful with delayed expansion to enable (!!):
SETLOCAL ENABLEdelayedExpansion @for %%X in (*.*) do ( SET var_temp=%%%X:~0,-4% ren "%%X" !var_temp! ENDLOCAL
Best regards.
Cchristian.
Yes, I see, but here is the part of my program concerned
@for %%X in (*.*) do (
ren "%%X" "%%X.flv"
)
I use a FOR loop, so if I use your command, it will be like
ren machin.mpg machin.mpg.flv
So there is always this .mpg, otherwise is there a solution to remove it even if it has already been renamed to flv?
@for %%X in (*.*) do (
ren "%%X" "%%X.flv"
)
I use a FOR loop, so if I use your command, it will be like
ren machin.mpg machin.mpg.flv
So there is always this .mpg, otherwise is there a solution to remove it even if it has already been renamed to flv?
I'm having trouble understanding this; can you clarify exactly how I should use it? Thanks anyway ^^