-->

Remove extension from filename

2019-09-18 02:19发布

问题:

In folder1 I have a list of files:

file1.exe
ahahdf.exe
ahdfkqkq.exe

I want to run a script and have it create the following files in folder2:

file1.zip
ahahdf.zip
ahdfkqkq.zip

My attempt:

for %%f in (*.*) do "c:\program files\7-zip\7z.exe" a %%f.zip %%f

This creates file1.exe.zip, ... but I need file1.zip, ... (without the .exe extension).

How can I do this?

回答1:

Should be

for %%f in (*.*) do "c:\program files\7-zip\7z.exe" a %%~nf.zip %%f

Note %%~nf - ~n tells command interpreter to pick the filename part without extension.

Also I would enclose parameters into double-quotes ("param") to ensure that filenames that contain spaces are properly handled.