Remove extension from filename

2019-09-18 02:08发布

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条回答
对你真心纯属浪费
2楼-- · 2019-09-18 02:17

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.

查看更多
登录 后发表回答